Enjoy R

A practical demonstration of linearity of MAD

We know that if we have a variable named X, to which we apply a linear transformation like

Y = a X + b

we can demonstrate that the computation of indexes related to Y may be easily deduced from the indexes we have obtained for X.

This way of proceeding is not standard, because we have to consider what kind of index we are dealing with:

  1. for a measure of central tendency, we directly use the same transformation we applied to the X variable;
  2. for a measure of dispersion, we have to drop the b coefficient, paying attention to the a coefficient, because it may need an “adjustment”;
  3. for a measure of simmetry, no transformation should be applied to the index (it does not vary).

The Median Absolute Deviation (MAD) is a measure of variability, thus it is included in case number 2 and in particular:

MAD(Y) = |a| * MAD(X)

# Demonstration:

# unordered data
x1 x2 ... xn

# ordered data
x(1) x(2)... x(n)

# get the median
x(1) x(2) ... x(me) ... x(n)

# get the absolute deviations from median
|x(1)-x(me)| |x(2)-x(me)| ... 0 ... |x(n)-x(me)|

# sort them and find the central one
0 |x[1]-x(me)| |x[2]-x(me)| ... |x[MAD]-x(me)| ... |x[n]-x(me)|

# re-write all differently
0 s1(X) s2(X) ... MAD(X) ... sn(X)

# get and sort the Y values
y(1)=ax(1)+b y(2)=ax(2)+b ... y(me)=ax(me)+b ... y(n)=ax(n)+b

# get the absolute deviations from median
|y(1)-y(me)|=|ax(1)+b-ax(me)-b| |y(2)-y(me)|=|ax(2)+b-ax(me)-b| ... 0 ... |y(n)-y(me)|=|ax(n)+b-ax(me)-b|

# make computations in each modulus
|y(1)-y(me)|=|a|*|x(1)-x(me)| |y(2)-y(me)|=|a|*|x(2)-x(me)| ... 0 ... |y(n)-y(me)|=|a|*|x(n)-x(me)|

# sort the absolute deviations and find the central one
0 |y[1]-y(me)|=|a|*|x[1]-x(me)| |y[2]-y(me)|=|a|*|x[2]-x(me)| ... |y[MAD]-y(me)|=|a|*|x[MAD]-x(me)| ... |y[n]-y(me)|=|a|*|x[n]-x(me)|

# re-write all differently
0 s1(Y)=|a|*s1(X) s2(Y)=|a|*s2(X) ... MAD(Y)=|a|*MAD(X) ... sn(Y)=|a|*sn(X)

Let's make a practical example:

unordered.X <- sample(1:1000, 20)  # vector of 20 random numbers
unordered.X
##  [1] 238 954 381 524 891 122 723 345 873 339 556 690  13 793 140 862 868
## [18] 573  31 764

ordered.X <- sort(unordered.X)  # sort the vector
ordered.X
##  [1]  13  31 122 140 238 339 345 381 524 556 573 690 723 764 793 862 868
## [18] 873 891 954

median.X <- mean(ordered.X[10:11])  # get the median
median.X
## [1] 564.5

ordered.abs.dev.median.X <- sort(abs(ordered.X - median.X))  # get the absolute deviations from the median and then sort them
ordered.abs.dev.median.X
##  [1]   8.5   8.5  40.5 125.5 158.5 183.5 199.5 219.5 225.5 228.5 297.5
## [12] 303.5 308.5 326.5 326.5 389.5 424.5 442.5 533.5 551.5

MAD.X <- mean(ordered.abs.dev.median.X[10:11])
MAD.X
## [1] 263

print(a <- rnorm(1, -5, 0.1))  # set and print a
## [1] -5.069
print(b <- runif(1))  # set and print b
## [1] 0.02088

unordered.Y <- a * unordered.X + b
unordered.Y
##  [1] -1206.42 -4835.86 -1931.29 -2656.17 -4516.51  -618.40 -3664.91
##  [8] -1748.81 -4425.27 -1718.39 -2818.38 -3497.63   -65.88 -4019.75
## [15]  -709.65 -4369.51 -4399.93 -2904.55  -157.12 -3872.74

ordered.Y <- sort(unordered.Y)  # sort the vector
ordered.Y
##  [1] -4835.86 -4516.51 -4425.27 -4399.93 -4369.51 -4019.75 -3872.74
##  [8] -3664.91 -3497.63 -2904.55 -2818.38 -2656.17 -1931.29 -1748.81
## [15] -1718.39 -1206.42  -709.65  -618.40  -157.12   -65.88

median.Y <- mean(ordered.Y[10:11])  # get the median
median.Y
## [1] -2861

ordered.abs.dev.median.Y <- sort(abs(ordered.Y - median.Y))  # get the absolute deviations from the median and then sort them
ordered.abs.dev.median.Y
##  [1]   43.09   43.09  205.30  636.17  803.45  930.17 1011.28 1112.66
##  [9] 1143.07 1158.28 1508.05 1538.46 1563.81 1655.05 1655.05 1974.40
## [17] 2151.82 2243.06 2704.34 2795.59

MAD.Y <- mean(ordered.abs.dev.median.Y[10:11])
MAD.Y
## [1] 1333

MAD.Y/MAD.X  # should be equal to |a|
## [1] 5.069
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: