The signed-rank test (Chapter 5) compares paired measurements. Often the groups have no pairing. The rank-sum test compares two independent samples. Frank Wilcoxon developed it in 1945 for equal sample sizes. Henry Mann and Donald Whitney gave the general version, the U test, in 1947 (Chapter 1). The two tests are the same test, so the name is often “Wilcoxon–Mann–Whitney U test”.
6.1 The problem
Six tortoises and six hares run a race finishing in this order:
The finishing positions are already ranks: 1 is the fastest. A mean or a standard deviation of a finishing position (rank) has little meaning, so a t-test is not a natural choice.
Is the tortoise faster than the hare?
Show code
ggplot(race, aes(position, animal, colour = animal)) +geom_point(size =5) +scale_colour_manual(values =c(H ="firebrick", T ="steelblue"), guide ="none") +scale_x_continuous(breaks =1:12) +labs(x ="finishing position (1 = first)", y =NULL)
Figure 6.1: The finishing order of the race. Position 1 is the winner. Five hares finish in positions 2 to 6, and five tortoises in positions 7 to 11.
6.2 Intuitive solution
Add the positions of each animal. This is the rank sum.
The two rank sums always add to \(1 + 2 + \dots + 12 = 78\). If the two animals are equally fast, each sum is 39 on average. The tortoises have the larger sum (46), so they finished later on average.
Now suppose that the animal has no effect on the finishing position. Then the labels “T” and “H” are arbitrary. Any set of 6 positions out of 12 is equally likely to belong to the tortoises. We can shuffle the labels, recalculate the tortoise rank sum, and repeat 10,000 times. The animated Wilcoxon rank-sum test of the course shows the same idea step by step.
tibble(R = shuffled) |>mutate(extreme =abs(R - centre) >=abs(R_T - centre)) |>ggplot(aes(R, fill = extreme)) +geom_bar(width =0.8) +scale_fill_manual(values =c(`FALSE`="steelblue", `TRUE`="firebrick"), guide ="none") +geom_vline(xintercept = R_T, linetype ="dashed", colour ="grey40") +annotate("text", x = R_T, y =Inf, label ="observed", vjust =1.5, hjust =-0.1, colour ="grey30") +labs(x ="tortoise rank sum after a shuffle", y ="count")
Figure 6.2: The tortoise rank sum after 10,000 random shuffles of the labels T and H. The red bars are the sums at least as far from the centre (39) as the observed sum of 46.
With 12 positions there are \(\binom{12}{6} = 924\) ways to choose the tortoise positions. We can list all of them and get the exact answer.
all_sums <-colSums(combn(12, 6)) # every possible tortoise rank sumc(extreme =sum(abs(all_sums - centre) >=abs(R_T - centre)), arrangements =length(all_sums))
extreme arrangements
286 924
mean(abs(all_sums - centre) >=abs(R_T - centre))
[1] 0.3095238
Of all the possible arrangements, 31% are as extreme as the race. The finishing order is usual under “no difference”. The race does not show that either animal is faster.
6.3 Formal solution
6.3.1 Assumptions and hypotheses
The two samples must be independent, and the observations within each sample must be independent. The test uses only the order of the observations, so the scale must be at least ordinal.
\(H_0\): the two samples come from the same distribution. \(H_1\): one distribution tends to give larger values than the other, \(P(X > Y) \ne \tfrac12\).
6.3.2 The procedure
The U statistic counts pairs. Take each observation in sample 1 and count the observations in sample 2 that are smaller. The total is \(U_1\). A tie counts ½. \(U_2\) is the same count with the samples exchanged. Each of the \(n_1 n_2\) pairs is counted once, so \(U_1 + U_2 = n_1 n_2\).
You can also get \(U\) from the rank sums of the pooled sample:
The subtracted term is the smallest possible rank sum for a sample of that size. For a printed table, use \(U = \max(U_1, U_2)\), with \(n\) the larger and \(n'\) the smaller sample size.
sum(outer(tortoise, hare, ">")) # pairs where the tortoise finished later
[1] 25
The pair count and the rank formula agree. In 25 of the 36 tortoise–hare pairs, the hare finished ahead of the tortoise.
The exact null distribution of \(U\) comes from the \(\binom{N}{n_1}\) equally likely arrangements, as in the intuitive solution. R has it in dwilcox() and pwilcox(). The printed tables give critical values.
U_crit <- n1 * n2 -qwilcox(0.025, n1, n2) +1# smallest U with two-sided p <= 0.05c(critical_value = U_crit, exact_p_at_critical =2*pwilcox(n1 * n2 - U_crit, n1, n2))
For \(n = n' = 6\), the two-sided 5% critical value is 31. Our \(U = \max(25, 11) = 25\) is well below it.
6.3.3 The U test in R
wilcox.test(tortoise, hare, conf.int =TRUE)
Wilcoxon rank sum exact test
data: tortoise and hare
W = 25, p-value = 0.3095
alternative hypothesis: true location shift is not equal to 0
95.9 percent confidence interval:
-3 7
sample estimates:
difference in location
4
Read the output line by line:
Wilcoxon rank sum exact test: R uses the exact distribution when both samples have fewer than 50 observations.
W = 25: R’s \(W\) is \(U_1\) for the first sample in the call, here the tortoises. It is not \(\max(U_1, U_2)\). If you exchange the two samples, \(W\) becomes 11.
p-value = 0.3095: the exact two-sided p-value. It equals our count of arrangements.
95.9 percent confidence interval: -3 7: an interval for the shift in position between the two animals. With 6 per group, R cannot get exactly 95%, so it gives the smallest achievable level above 95%.
difference in location 4: the Hodges–Lehmann estimate of the shift. The section on what to report explains it.
With the data in one data frame, use the formula interface. Here the outcome is position and the group is animal.
wilcox.test(position ~ animal, data = race, exact =FALSE)
Wilcoxon rank sum test with continuity correction
data: position by animal
W = 11, p-value = 0.298
alternative hypothesis: true location shift is not equal to 0
The formula orders the groups alphabetically, so H is the first sample and \(W = 11\). The argument exact = FALSE asks for the normal approximation.
6.3.4 The normal approximation
Under \(H_0\), the rank sum \(R_1\) has mean \(n_1(n_1 + n_2 + 1)/2\) and variance \(n_1 n_2 (n_1 + n_2 + 1)/12\). For larger samples, \(R_1\) is approximately normal. With a continuity correction of ½, the statistic is
\(U_1\) and \(R_1\) differ by a constant. So the same formula works with \(U_1\) in place of \(R_1\) and \(n_1 n_2 / 2\) in place of the mean. Ties make the variance smaller. If \(t_j\) is the size of the \(j\)th tied group, replace \((n_1 + n_2 + 1)\) with
The approximate p-value (0.30) is close to the exact value (0.31), and it matches the formula output above.
6.4 What the test detects
The null hypothesis says that the two samples come from the same distribution. The test detects a difference in \(P(X > Y)\) from ½. It is a test of medians only under a pure shift, when the two distributions have the same shape and spread.
A simulated example shows the difference. Group X comes from a skewed distribution with median 0. Group Y comes from a normal distribution with median 0. The medians are equal, but the shapes are different.
set.seed(1947)x <-rexp(500) -log(2) # simulated, skewed, true median 0y <-rnorm(500) # simulated, symmetric, true median 0c(median_x =median(x), median_y =median(y), P_x_greater =mean(outer(x, y, ">")))
The sample medians are both close to 0, but \(\hat P(X > Y) = 0.56\) and \(p = 3.8\times 10^{-4}\). A small p-value from this test does not prove that the medians differ.
Report two estimates with the p-value:
\(\hat P = U / (n_1 n_2)\), the estimated probability that a value from one group is larger than a value from the other. This is the area under the ROC curve (AUC).
The Hodges–Lehmann shift: the median of all \(n_1 n_2\) differences \(x_i - y_j\), with its confidence interval.
For the race, the hare is ahead in 25 of 36 pairs, \(\hat P = 0.69\). The Hodges–Lehmann estimate is that the tortoises finish 4 places later (95.9% CI -3 to 7), with \(p = 0.31\). The estimate favours the hare, but 6 animals per group are too few to tell.
6.5 Choosing a test for skewed data
The choice of test can change the conclusion. The data below are simulated. Two post-operative pain protocols, A and B, are each given to 30 patients. The outcome is the recovery time in days, and the times are right-skewed (gamma distribution).
ggplot(recovery, aes(protocol, days, fill = protocol)) +geom_violin(alpha =0.35, colour =NA) +geom_boxplot(width =0.15, fill ="white", outlier.shape =NA) +geom_jitter(width =0.06, size =1, alpha =0.6) +scale_fill_manual(values =c(A ="firebrick", B ="steelblue"), guide ="none") +labs(x ="protocol", y ="recovery time (days)")
Figure 6.3: Simulated recovery times for two protocols, 30 patients each. The violins show the right skew. The boxes show the median and quartiles.
We do three tests. The z-test uses the sample standard deviations as if they were the true values. Welch’s t-test uses the same statistic with a t distribution. The Wilcoxon rank-sum test uses only the ranks.
three_tests <-function(a, b) { se <-sqrt(var(a) /length(a) +var(b) /length(b)) z <- (mean(a) -mean(b)) / setibble(test =c("z-test", "Welch t-test", "Wilcoxon rank-sum"),p_value =c(2*pnorm(-abs(z)), t.test(a, b)$p.value, wilcox.test(a, b)$p.value))}res30 <-three_tests(protocol_A, protocol_B)knitr::kable(res30, digits =3)
test
p_value
z-test
0.050
Welch t-test
0.056
Wilcoxon rank-sum
0.187
The z-test gives \(p = 0.050\), on the 5% line. Welch’s t-test gives 0.056, and the rank-sum test gives 0.187. The z-test and the t-test have the same statistic. The t distribution has thicker tails, so its p-value is larger. With 46 degrees of freedom, the difference is small. The long right tail of protocol A does not affect the rank-sum test, and its p-value is much larger.
The difference between z and t becomes large in a small pilot study. Here the simulation has a small number of patients per arm, from normal distributions.
set.seed(951)n_pilot <-5# patients per armpilot_A <-rnorm(n_pilot, mean =6, sd =1.5) # simulatedpilot_B <-rnorm(n_pilot, mean =4, sd =1.5) # simulatedres5 <-three_tests(pilot_A, pilot_B)knitr::kable(res5, digits =3)
Figure 6.4: The pilot study (5 per arm). The same test statistic (dashed lines) compared with the standard normal (red) and with the t distribution with Welch degrees of freedom (blue). The t distribution has thicker tails, so its p-value is larger.
At \(n =\) 5 per arm, the z-test gives \(p = 0.045\) and calls the result significant. Welch’s t-test gives 0.10, with only 5.0 degrees of freedom. The rank-sum test gives 0.22. With 5 per arm, the smallest possible two-sided p-value of the exact rank-sum test is 0.008. Use of the z-test with small samples makes false positives more frequent.
6.6 Summary
The rank-sum test pools the two samples, ranks them, and compares the rank sum of one sample with its value under random labels.
The statistic \(U_1 = R_1 - n_1(n_1+1)/2\) counts the pairs in which sample 1 has the larger value. R’s \(W\) is \(U_1\) for the first sample.
The test detects \(P(X > Y) \ne \tfrac12\). It is a test of medians only when the two distributions have the same shape.
A full report gives \(\hat P(X > Y)\) and the Hodges–Lehmann shift with its confidence interval, together with the p-value.