Link Search Menu Expand Document (external link)

How to do a Wilcoxon signed-rank test

Description

Assume we a sample of data, $x_1, x_2, x_3, \ldots x_k$ and either the sample size is small or the population is not normally distributed. But we still want to perform tests that compare the sample median to a hypothesized value (equal, greater, or less). One method is the Wilcoxon Signed-Rank Test.

Related tasks:

Using SciPy, in Python

View this solution alone.

We’re going to use fake data for illustrative purposes, but you can replace our fake data with your real data. Say our sample, $x_1, x_2, x_3, \ldots x_k$, has median $m$.

1
2
3
import numpy as np
# Replace the next line with your data
sample = np.array([19, 4, 23, 16, 1, 8, 30, 25, 13])

We choose a value, $0 \le \alpha \le 1$, as the Type I Error Rate. We’ll let $\alpha$ be 0.05. In the examples below, we will be comparing the median $m$ to a hypothesized value of $a=10$, but you can use any value for $a$.

Two-tailed test

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

1
2
3
4
from scipy import stats
from scipy.stats import wilcoxon
a = 10  # or your chosen value for comparison
wilcoxon(sample - a)
1
WilcoxonResult(statistic=10.0, pvalue=0.1640625)

Our p-value, 0.1640625, is greater than $\alpha=0.05$, so we do not have sufficient evidence to reject the null hypothesis. We may continue to assume the population median is equal to 10.

Right-tailed test

To test the null hypothesis $H_0: m\ge a$, we use a right-tailed test:

1
wilcoxon(sample - a, alternative = 'less')
1
WilcoxonResult(statistic=35.0, pvalue=0.935546875)

Our p-value, 0.935546875, is greater than $\alpha=0.05$, so we do not have sufficient evidence to reject the null hypothesis. We may continue to assume the population median is less than (or equal to) 10.

Left-tailed test

To test the null hypothesis $H_0: m\le a$, we use a left-tailed test:

1
wilcoxon(sample - a, alternative = 'greater')
1
WilcoxonResult(statistic=35.0, pvalue=0.08203125)

Our p-value, 0.08203125, is greater than $\alpha$, so we do not have sufficient evidence to reject the null hypothesis. We may continue to assume the population median is greater than (or equal to) 10.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Solution, in R

View this solution alone.

We’re going to use fake data for illustrative purposes, but you can replace our fake data with your real data. Say our sample, $x_1, x_2, x_3, \ldots x_k$, has median $m$.

1
2
# Replace the next line with your data
sample <- c(19, 4, 23, 16, 1, 8, 30, 25, 13)

We choose a value, $0 \le \alpha \le 1$, as the Type I Error Rate. We’ll let $\alpha$ be 0.05. In the examples below, we will be comparing the median $m$ to a hypothesized value of $a=10$, but you can use any value for $a$.

Two-tailed test

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

1
2
a <- 10
wilcox.test(sample, mu = a, alternative = "two.sided")
1
2
3
4
5
6
7
8
9
10
11
Warning message in wilcox.test.default(sample, mu = a, alternative = "two.sided"):
“cannot compute exact p-value with ties”




	Wilcoxon signed rank test with continuity correction

data:  sample
V = 35, p-value = 0.1544
alternative hypothesis: true location is not equal to 10

Our p-value, 0.1544, is greater than $\alpha=0.05$, so we do not have sufficient evidence to reject the null hypothesis. We may continue to assume the population median is equal to 10.

Right-tailed test

To test the null hypothesis $H_0: m\ge a$, we use a right-tailed test:

1
wilcox.test(sample, mu = a, alternative = "less")
1
2
3
4
5
6
7
8
9
10
11
Warning message in wilcox.test.default(sample, mu = a, alternative = "less"):
“cannot compute exact p-value with ties”




	Wilcoxon signed rank test with continuity correction

data:  sample
V = 35, p-value = 0.9386
alternative hypothesis: true location is less than 10

Our p-value, 0.9386, is greater than $\alpha=0.05$, so we do not have sufficient evidence to reject the null hypothesis. We may continue to assume the population median is less than (or equal to) 10.

Left-tailed test

To test the null hypothesis $H_0: m\le a$, we use a left-tailed test:

1
wilcox.test(sample, mu = a, alternative = "greater")
1
2
3
4
5
6
7
8
9
10
11
Warning message in wilcox.test.default(sample, mu = a, alternative = "greater"):
“cannot compute exact p-value with ties”




	Wilcoxon signed rank test with continuity correction

data:  sample
V = 35, p-value = 0.0772
alternative hypothesis: true location is greater than 10

Our p-value, 0.0772, is greater than $\alpha$, so we do not have sufficient evidence to reject the null hypothesis. We may continue to assume the population median is greater than (or equal to) 10.

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.