📚 Trusted by students, educators & researchers since 2017.
If you are tracking the exact same subjects across two different points in time—like a patient’s blood pressure before and after a new medication—you typically use a Paired T-Test.
But just like the standard t-test, the paired t-test assumes your data follows a normal distribution. If your data is heavily skewed or ordinal (e.g., pain scale ratings from 1 to 10), you must use the Wilcoxon Signed-Rank Test.
What is the Wilcoxon Signed-Rank Test?
The Wilcoxon Signed-Rank test is the non-parametric equivalent of the paired t-test. It is used to determine if there is a statistically significant median difference between paired observations.
How it Works (The Math)
Because this test looks at paired data, it doesn’t care about the raw scores of the two groups. It only cares about the difference between each pair.
- Calculate the Differences: Subtract the pre-test score from the post-test score for every single pair.$$d_i = y_i – x_i$$
- Discard Zeroes: If a pair has a difference of exactly zero (meaning no change occurred), that pair is entirely removed from the analysis.
- Rank the Absolute Differences: The differences are stripped of their negative/positive signs and ranked from smallest to largest.
- Restore the Signs and Sum: The positive or negative signs are given back to the ranks. The test then sums all the positive ranks ($W^+$) and sums all the negative ranks ($W^-$).
The final test statistic ($W$) is the smaller of those two sums:
$$W = \min(\vert{}W^+\vert{}, \vert{}W^-\vert{})$$
If the intervention had no effect, the sum of the positive ranks and negative ranks should be roughly equal. If one is drastically smaller than the other, it indicates a significant shift occurred.
How to Run a Wilcoxon Signed-Rank Test in Python
You can execute this test using the scipy.stats library. Note that your two arrays must be exactly the same length.
import scipy.stats as stats
# Example Paired Data (e.g., Before and After)
pre_test = [10, 15, 12, 9, 14, 16]
post_test = [14, 15, 16, 12, 18, 20]
# Run the Wilcoxon Signed-Rank test
w_stat, p_value = stats.wilcoxon(pre_test, post_test, alternative='two-sided')
print(f"W-Statistic: {w_stat}")
print(f"P-Value: {p_value:.4f}")
How to Run a Wilcoxon Signed-Rank Test in R
In R, you use the exact same function as the Mann-Whitney test, but you simply change the paired argument to TRUE.
# Example Paired Data
pre_test <- c(10, 15, 12, 9, 14, 16)
post_test <- c(14, 15, 16, 12, 18, 20)
# Run the test (Note: paired = TRUE)
result <- wilcox.test(pre_test, post_test, paired = TRUE)
# Print the results
print(result)
(Tip: Don’t want to write code? You can calculate your results instantly using our free Wilcoxon Signed-Rank Calculator.)
