Link Search Menu Expand Document (external link)

How to do a Wilcoxon signed-rank test for matched pairs (in R)

See all solutions.

Task

Assume we have two samples of data that come in matched pairs, x1,x2,x3,xk and x1,x2,x3,xk, which we might pair up as (x1,x1),(x2,x2),,(xk,xk). The two samples may be from different populations. Also assume that the sample sizes are small or the populations are not normally distributed.

Consider measuring the difference in each pair, x1x1,x2x2,,xkxk. We want to perform tests that compare the median of those differences, mD, to a hypothesized value (equal, greater, or less). One method is the Wilcoxon Signed-Rank Test for Matched Pairs.

Related tasks:

Solution

The method we will use is equivalent to subtracting the two samples and then performing the signed-rank test. See how to do a Wilcoxon signed-rank test to compare the two methods.

We’re going to use fake data for illustrative purposes, but you can replace our fake data with your real data.

1
2
3
# Replace sample1 and sample2 with your data
sample1 <- c(156, 133, 90, 176, 119, 120, 40, 52, 167, 80)
sample2 <- c(45, 36, 78, 54, 12, 25, 39, 48, 52, 70)

We choose a value, 0α1, as the Type I Error Rate. We’ll let α be 0.05.

Two-tailed test

To test the null hypothesis H0:mD=0, we use a two-tailed test:

1
wilcox.test(sample1, sample2, alternative = "two.sided", mu = 0, paired = TRUE)
	Wilcoxon signed rank exact test

data:  sample1 and sample2
V = 55, p-value = 0.001953
alternative hypothesis: true location shift is not equal to 0

Our p-value, 0.00195, is less than α=0.05, so we have sufficient evidence to reject the null hypothesis. The median difference is significantly different from zero.

Right-tailed test

To test the null hypothesis H0:mD0, we use a right-tailed test:

1
wilcox.test(sample1, sample2, alternative = "greater", mu = 0, paired = TRUE)
	Wilcoxon signed rank exact test

data:  sample1 and sample2
V = 55, p-value = 0.0009766
alternative hypothesis: true location shift is greater than 0

Our p-value, 0.0009766, is less than α=0.05, so we have sufficient evidence to reject the null hypothesis. The median difference is significantly greater than zero.

Left-tailed test

To test the null hypothesis H0:mD0, we use a left-tailed test:

1
wilcox.test(sample1, sample2, alternative = "less", mu = 0, paired = TRUE)
	Wilcoxon signed rank exact test

data:  sample1 and sample2
V = 55, p-value = 1
alternative hypothesis: true location shift is less than 0

Our p-value, 1.0, is greater than α, so we do not have sufficient evidence to reject the null hypothesis. We should continue to assume that the mean difference may be less than (or equal to) zero.

NOTE: If there are ties in the data and there are fewer than 50 observations in each sample, then R will compute a p-value using the normal approximation, and there will be an error message indicating that the exact p-value cannot be calculated.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)