1

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
    }
}
0

3 Answers 3

7

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)
Sign up to request clarification or add additional context in comments.

5 Comments

The widely used package numpy provides the median function (numpy.median) so don't waste time in reinventing the wheel !
Am i missing something? Assuming x is a list of numbers, (x - median(x)), Python won't do vectorized math.
@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])
thanks, btw, is there functions called Maclist and MLEintvl in R? could not find it...
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.
3

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)

Comments

2
import numpy
# x is the input array
x = numpy.array( [1,2,4,3,1,6,7,5,4,6,7], float ) }
# mad = median( | x - median(x) | )
mad =  numpy.median( numpy.abs( ( x - numpy.median( x ) ) )

1 Comment

add some description to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.