Link Search Menu Expand Document (external link)

How to do a Wilcoxon signed-rank test for matched pairs

Description

Assume we have two samples of data that come in matched pairs, $x_1, x_2, x_3, \ldots x_k$ and $x’_1, x’_2, x’_3, \ldots x’_k$, which we might pair up as $(x_1,x’_1),(x_2,x’_2),\ldots,(x_k,x’_k)$. 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, $x_1-x’_1,x_2-x’_2,\ldots,x_k-x’_k$. We want to perform tests that compare the median of those differences, $m_D$, to a hypothesized value (equal, greater, or less). One method is the Wilcoxon Signed-Rank Test for Matched Pairs.

Related tasks:

Using SciPy, in Python

View this solution alone.

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
4
import numpy as np
# Replace sample1 and sample2 with your data
sample1 = np.array([156, 133, 90, 176, 119, 120, 40, 52, 167, 80])
sample2 = np.array([45, 36, 78, 54, 12, 25, 39, 48, 52, 70])

We choose a value, $0 \le \alpha \le 1$, as the Type I Error Rate. We’ll let $\alpha$ be 0.05.

Two-tailed test

To test the null hypothesis $H_0: m_D = 0$, we use a two-tailed test:

1
2
3
from scipy import stats
from scipy.stats import wilcoxon
wilcoxon(sample1 - sample2)
1
WilcoxonResult(statistic=0.0, pvalue=0.001953125)

Our p-value, 0.001953125, is less than $\alpha=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 $H_0: m_D \le 0$, we use a right-tailed test:

1
wilcoxon(sample1 - sample2, alternative = 'greater')
1
WilcoxonResult(statistic=55.0, pvalue=0.0009765625)

Our p-value, 0.0009765625, is less than $\alpha=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 $H_0: m_D \ge 0$, we use a left-tailed test:

1
wilcoxon(sample1 - sample2, alternative = 'less')
1
WilcoxonResult(statistic=55.0, pvalue=1.0)

Our p-value, 1.0, is greater than $\alpha$, 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.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Solution, in R

View this solution alone.

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 \le \alpha \le 1$, as the Type I Error Rate. We’ll let $\alpha$ be 0.05.

Two-tailed test

To test the null hypothesis $H_0: m_D = 0$, we use a two-tailed test:

1
wilcox.test(sample1, sample2, alternative = "two.sided", mu = 0, paired = TRUE)
1
2
3
4
5
	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 $\alpha=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 $H_0: m_D \le 0$, we use a right-tailed test:

1
wilcox.test(sample1, sample2, alternative = "greater", mu = 0, paired = TRUE)
1
2
3
4
5
	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 $\alpha=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 $H_0: m_D \ge 0$, we use a left-tailed test:

1
wilcox.test(sample1, sample2, alternative = "less", mu = 0, paired = TRUE)
1
2
3
4
5
	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 $\alpha$, 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.

Topics that include this task

Opportunities

This website does not yet contain a solution for this task in any of the following software packages.

  • Excel
  • Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.