Solved – Test for median difference

hypothesis testingmedianstatistical significance

Given samples of two distributions I am looking for a test for median difference (I.e. reject null in favor of evidence that medians are different.) I do not want to assume anything about both distributions. Is there any standard test for this situation?

I know Mood's median test, but I believe it assumes that the distributions are shifted. $F_2(t) = F_1(t-a)$ for some $a \in \mathbb{R}$. I back this claim with these sources:

Buthmann, A. (2017). "Understanding the Uses for Mood’s Median Test". I Six Sigma blog post.

Taylor, A. D. (2012). "Mood’s Median Test". Handout (PDF link via Wayback Machine). 2 pages.

Glen, S. (2016). "Mood’s Median Test: Definition, Run the Test and Interpret Results". Statistics How To: Elementary statistics for the rest of us blog post.

Best Answer

You could consider a permutation test.

median.test <- function(x,y, NREPS=1e4) {
  z <- c(x,y)
  i <- rep.int(0:1, c(length(x), length(y)))
  v <- diff(tapply(z,i,median))
  v.rep <- replicate(NREPS, {
    diff(tapply(z,sample(i),median))
  })
  v.rep <- c(v, v.rep)
  pmin(mean(v < v.rep), mean(v>v.rep))*2
}

set.seed(123)
n1 <- 100
n2 <- 200
## the two samples
x <- rnorm(n1, mean=1)
y <- rexp(n2, rate=1)
median.test(x,y)

enter image description here

Gives a 2 sided p-value of 0.1112 which is a testament to how inefficient a median test can be when we don't appeal to any distributional tendency.

If we used MLE, the 95% CI for the median for the normal can just be taken from the mean since the mean is the median in a normal distribution, so that's 1.00 to 1.18. The 95% CI for the median for the exponential can be framed as $\log(2)/\bar{X}$, which by the delta method is 0.63 to 0.80. Therefore the Wald test is statistically significant at the 0.05 level but the median test is not.