There are a lot of answers to this question. Here's one that you probably won't see elsewhere so I'm including it here because I believe it's pertinent to the topic. People often believe that because the median is considered a robust measure with respect to outliers that it's also robust to most everything. In fact, it's also considered robust to bias in skewed distributions. These two robust properties of the median are often taught together. One might note that underlying skewed distributions also tend to generate small samples that look like they have outliers and conventional wisdom is that one use medians in such situations.
#function to generate random values from a skewed distribution
rexg <- function (n, m, sig, tau) {
rexp(n, rate = 1/tau) + rnorm(n, mean = m, sd = sig)
}
(just a demonstration that this is skewed and the basic shape)
hist(rexg(1e4, 0, 1, 1))
Now, let's see what happens if we sample from this distribution various sample sizes and calculate median and mean to see what the differences between them are.
#generate values with various n's
N <- 1e4
ns <- 2:30
y <- sapply(ns, function(x) mean(apply(matrix(rexg(x*N, 0, 1, 1),
ncol = N), 2, median)))
plot(ns,y, type = 'l', ylim = c(0.85, 1.03), col = 'red')
y <- sapply(ns, function(x) mean(colMeans(matrix(rexg(x*N, 0, 1,
1), ncol = N))))
lines(ns,y)
As can be seen from the above plot the median (in red) is much more sensitive to the n than the mean. This is contrary to some conventional wisdom regarding using medians with low ns, especially if the distribution might be skewed. And, it reinforces the point that the mean is a known value while the median is sensitive to other properties, one if which being the n.
This analysis is similar to
Miller, J. (1988). A warning about median reaction time. Journal of Experimental Psychology: Human Perception and Performance, 14(3):539–543.
REVISION
Upon thinking about the skew issue I considered that the impact on the median might just be because in small samples you have a greater probability that the median is in the tail of the distribution, whereas the mean will almost always be weighted by values closer to the mode. Therefore, perhaps if one was just sampling with a probability of outliers then maybe the same results would occur.
So I thought about situations where outliers may occur and experimenters may attempt to eliminate them.
If outliers happened consistently, such as one in every single sampling of data, then medians are robust against the effect of this outlier and the conventional story about the use of medians holds.
But that's not usually how things go.
One might find an outlier in very few cells of an experiment and decide to use median instead of mean in this case. Again, the median is more robust but it's actual impact is relatively small because there are very few outliers. This would definitely be a more common case then the one above but the effect of using a median would probably be so small that it wouldn't matter much.
Perhaps more commonly outliers might be a random component of the data. For example, the true mean and standard deviation of the population may be about 0 but there's a percentage of the time we sample from an outlier population where the mean is 3. Consider the following simulation, where just such a population is sampled varying the sample size.
# generate n samples N times with an outp probability
# of an outlier.
rout <- function (n, N, outp) {
outPos <- sample(0:1,n*N, replace = TRUE,
prob = c(1-outp, outp))
numOutliers <- sum(outPos)
y <- matrix( rnorm(N*n), ncol = N )
y[which(outPos==1)] <- rnorm(numOutliers, 4)
return(y)
}
outp <- 0.1
N <- 1e4
ns <- 3:30
yMed <- sapply(ns, function(x) mean(apply(rout(x,N,outp), 2,
median)))
var(yMed)
yM <- sapply(ns, function(x) mean(colMeans(rout(x, N, outp))))
var(yM)
plot(ns,yMed, type = 'l', ylim = range(c(yMed,yM)), ylab = 'Y',
xlab = 'n', col = 'red')
lines(ns,yM)
The median is in red and mean in black. This is a similar finding to that of a skewed distribution.
In a relatively practical example of the use of medians to avoid the effects of outliers one can come up with situations where the estimate is affected by n much more when the median is used than when the mean is used.