How to do a Wilcoxon signed-rank test (in Python, using SciPy)
Task
Assume we a sample of data,
Related tasks:
- How to do a Kruskal-Wallis test
- How to do a Wilcoxon rank-sum test
- How to do a Wilcoxon signed-rank test for matched pairs
Solution
We’re going to use fake data for illustrative purposes,
but you can replace our fake data with your real data.
Say our sample,
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,
Two-tailed test
To test the null hypothesis
1
2
3
4
from scipy import stats
from scipy.stats import wilcoxon
a = 10 # or your chosen value for comparison
wilcoxon(sample - a)
WilcoxonResult(statistic=10.0, pvalue=0.1640625)
Our p-value, 0.1640625, is greater than
Right-tailed test
To test the null hypothesis
1
wilcoxon(sample - a, alternative = 'less')
WilcoxonResult(statistic=35.0, pvalue=0.935546875)
Our p-value, 0.935546875, is greater than
Left-tailed test
To test the null hypothesis
1
wilcoxon(sample - a, alternative = 'greater')
WilcoxonResult(statistic=35.0, pvalue=0.08203125)
Our p-value, 0.08203125, is greater than
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)