tags:

views:

190

answers:

2

Hi, 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) 
        return(NA)
    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 + 
            1)])/2
    }
}
+3  A: 

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]
    else:
        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)
wok
The widely used package numpy provides the median function (numpy.median) so don't waste time in reinventing the wheel !
Elenaher
Am i missing something? Assuming x is a list of numbers, (x - median(x)), Python won't do vectorized math.
Mark
@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])
Elenaher
You are right. Well, I believe this is the idea.
wok
thanks, btw, is there functions called Maclist and MLEintvl in R? could not find it...
ligwin
If you are satisfied with the answer, then accept it. And if you have other questions, then the best way is to ask them using the usual form and the right tags.
wok
+1  A: 

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')

stats.mad(x)
lgautier