Take the 2-minute tour ×

I got the following R code and I need to convert it to python and run it in python environment, basically I have done this with rpy2 module, but it looks kind of dull with python doing the same things, so could someone find a better way to rewrite the following R code to an equivalent python script with the rpy2 module?

mymad <- function (x) 
    center <- median(x)
    y <- abs(x - center)
    n <- length(y)
    if (n == 0) 
    half <- (n + 1)/2
    1.4826 * if (n%%2 == 1) {
        sort(y, partial = half)[half]
    else {
        sum(sort(y, partial = c(half, half + 1))[c(half, half + 
share|improve this question
add comment

2 Answers

You could have stated the purpose of your function, which is Median Absolute Deviation. What you call mymad is an approximation of the standard deviation of the population, based on the assumption of large samples of normally distributed variables.

According to this website:

def median(pool):
    copy = sorted(pool)
    size = len(copy)
    if size % 2 == 1:
        return copy[(size - 1) / 2]
        return (copy[size/2 - 1] + copy[size/2]) / 2

So, you want a function mad which would verify :

mad(x) == median(abs(x-median(x)))

Thanks to Elenaher (give his comment credits), here is the code:

def mad(x):
    return median([abs(val-median(x)) for val in x])

And then, I believe your are computing:

def mymad(x):
    return 1.4826*mad(x)
share|improve this answer
The widely used package numpy provides the median function (numpy.median) so don't waste time in reinventing the wheel ! –  ThR37 Aug 5 '10 at 16:15
Am i missing something? Assuming x is a list of numbers, (x - median(x)), Python won't do vectorized math. –  Mark Aug 5 '10 at 16:16
@Mark yes but numpy does it ! If x is a numpy array you can write x-np.median(x). Otherwise you can use list comprehension : median([abs(val-median(x)) for val in x]) –  ThR37 Aug 5 '10 at 16:17
You are right. Well, I believe this is the idea. –  Wok Aug 5 '10 at 16:21
thanks, btw, is there functions called Maclist and MLEintvl in R? could not find it... –  ligwin Aug 5 '10 at 16:29
show 1 more comment

Probably a little slower than a numpy/Python written one, but certainly faster to implement (as no wheel gets reinvented):

# requires rpy2 >= 2.1
from rpy2.robjects.packages import importr
stats = importr('stats')

share|improve this answer
add comment

Your Answer


