tomR
tomR

Reputation: 31

stats::mad gone mad?

First time using mad() Median Absolute Deviation on a vector, mostly 4 and 5, some NA. Am I mistaken, but why would the mad function not return a positive value for

mad(c(4,5,5,5))

? Instead, I get a 0. I would only expect 0 if all values in the vector are the same, not if there is variation in value.

Please enlighten me. I am using R version 4.05.

Upvotes: 3

Views: 192

Answers (1)

MrFlick
MrFlick

Reputation: 206506

According to the documentation the function will return the "median of the absolute deviations from the median"

So in the case of

x <- c(4,5,5,5)

The median is

median(x)
# [1] 5

and the differences are

x-median(x)
# [1] -1  0  0  0

which have absolute value

abs(x-median(x))
# [1] 1 0 0 0

And the median value of the vector is just 0

median(abs(x-median(x)))
[1] 0

It seems your expectation is just off. You don't get 0 only when all values are the same, you can get 0 when the median difference is 0. That happens when more than half of your values are actually the median value. Another vector that returns 0 is

mad(c(1, 7, 7, 7, 100))
[1] 0

Upvotes: 3

Related Questions