How to run a Wilcoxon signed-rank test
- Copy the before and after columns so that the same subject is on the same row.
- Select before as the value 1 column and after as the value 2 column. The difference is computed as 'after - before'.
- Press Analyze and check the p-value, the median difference and the location shift estimate (HL) in the results table.
Worked example with sample data
The sample data are the noise levels (dB) of 15 machines measured before and after a soundproofing improvement. Select the before column as value 1 and the after column as value 2.
In the results table, the median difference (after - before) is -3.2 dB, the positive rank sum W+ is 4 and the negative rank sum W- is 116. Only 2 machines got louder, and by just 0.4 and 0.6 dB - the smallest changes - so W+ is very small. The p-value of 0.0004272 shows a significant improvement.
The location shift estimate (HL) is -2.775 dB (confidence interval -3.85 to -1.55), so the noise typically dropped by about 2.8 dB after the improvement. With no ties or zero differences and no more than 50 pairs, the p-value was computed from the exact distribution.
In the before-after chart, 13 of the 15 lines slope downward, so the effect was not limited to a few machines but appeared on most of them. The 2 machines that got louder (M05 and M12) were already among the quieter ones, so it is worth checking whether the improvement suited them.
How it is calculated
For each row the difference d = after - before is computed (rows where either value is blank are dropped), the reference difference is subtracted, and the absolute differences are ranked to get the positive rank sum W+ and the negative rank sum W-.
For a two-sided test the statistic is the smaller of the two, and the p-value is computed the same way as SciPy's wilcoxon (exact distribution, permutation distribution or normal approximation chosen automatically).
W+ and W-
W+ is the rank sum of pairs that increased and W- of pairs that decreased. When the goal is a reduction, such as noise, defects or time, W- should be larger than W+ for the improvement to show.
What the result shows
The results table, a before-after chart, a dot plot of the differences and a box plot of the differences.
How to read the Wilcoxon signed-rank result
- If the p-value is below the significance level, the before-after difference is significant. Check the sign of the median difference to see whether it is an increase or a decrease.
- The location shift estimate (HL) is a typical value of the differences. If its confidence interval does not contain 0, the conclusion is the same as a significant difference.
- If the differences are roughly normal, the paired t test has more power.
Frequently asked questions
How is it different from the paired t test?
The paired t test looks at the mean of the differences; the Wilcoxon signed-rank test looks at their location through ranks. It is more stable when the differences contain outliers or are skewed.
What happens to pairs with equal before and after values?
The default (wilcox) drops pairs with a zero difference. You can also choose pratt or zsplit as the zero-difference method.
Do W+ and W- depend on the zero-difference method?
No. W+ and W- in the results table are rank sums of the non-zero differences only, while the test statistic and p-value follow the chosen method.