The Wilcoxon Rank-Sum Test
The Wilcoxon rank-sum test (also called the Mann-Whitney U test) is the nonparametric alternative to the two-sample t-test. Instead of comparing means and requiring (approximate) normality, it compares the ranks of observations across two independent groups—testing whether one group tends to produce systematically larger (or smaller) values than the other.
Conditions:
- Random samples from each population
- Independent samples
- (No normality assumption needed—this is the whole point of a nonparametric test!)
Motivating example: In a small pilot study, researchers recorded the number of days it took each patient’s blood pressure to reach a “controlled” level, comparing 10 patients on Drug A to 10 patients on Placebo. This “days to control” outcome is a count that’s likely to be right-skewed (most patients respond within a week or two, but a few take much longer)—a good candidate for a rank-based test rather than a t-test. The data are in pilot2, with columns days_to_control and group.
Looking at the Data
Fill in the code below to create a boxplot of days_to_control by group.
# boxplot of days to control, by group
pilot2 %>%
ggplot(aes(x = , y = , color = group)) +
geom_boxplot() +
geom_jitter(width = 0.1) +
theme_bw() +
theme(legend.position = "none")
# boxplot of days to control, by group
pilot2 %>%
ggplot(aes(x = group, y = days_to_control, color = group)) +
geom_boxplot() +
geom_jitter(width = 0.1) +
theme_bw() +
theme(legend.position = "none")The Drug A group’s values should cluster noticeably lower than the Placebo group’s—Drug A patients seem to reach controlled blood pressure faster.
Ranking the Combined Data
The rank-sum test works by combining both groups into one pool, ranking all observations together (smallest = rank 1), and then looking at whether one group’s ranks tend to be systematically higher or lower than the other’s.
Fill in the code below to rank all 20 values of days_to_control together (ignoring group), and add the ranks as a new column.
# rank all observations together
pilot2 = pilot2 %>%
mutate(rank_all = rank())
pilot2
# rank all observations together
pilot2 = pilot2 %>%
mutate(rank_all = rank(days_to_control))
pilot2Notice some tied values get tied (averaged) ranks—for example, two patients with days_to_control = 4 would each get the average of the two ranks they’d otherwise occupy.
Calculating the Test Statistic
Fill in the code below to calculate the sum of the ranks within each group.
# sum of ranks by group
pilot2 %>%
group_by() %>%
summarise(rank_sum = sum())
# sum of ranks by group
pilot2 %>%
group_by(group) %>%
summarise(rank_sum = sum(rank_all))Drug A’s rank sum should be much lower than Placebo’s (around 56 vs. 155, out of a total of 210 across all 20 ranks)—exactly what we’d expect if Drug A patients tend to reach controlled blood pressure faster.
If the two groups had identical distributions (no real difference), roughly what would you expect the two rank sums to look like? How does that compare to what we found?
Using wilcox.test()
Fill in the code below to run the Wilcoxon rank-sum test comparing days_to_control between the two levels of group, using the formula interface. We’ll set correct = F to turn off the continuity correction (matching our discussion of the test above).
# Wilcoxon rank-sum test
wilcox.test( ~ , data = pilot2, correct = F)
# Wilcoxon rank-sum test
wilcox.test(days_to_control ~ group, data = pilot2, correct = F)W = 0.5, p-value = 0.0001786
Because there are tied values in this data (e.g., two patients with days_to_control = 4), R can’t compute an exact p-value and will print a warning saying it’s using a normal approximation instead—that’s expected and fine here. The p-value is tiny, so we’d conclude there’s a significant difference in days-to-control between the two groups.
This test gave us a very small p-value with only 10 patients per group. Why might a rank-based test like this be a safer choice than a two-sample t-test when you have a small sample and a skewed outcome like “days to reach an event”?