Chapter 3 Wilcoxon Signed-Rank Test

3.1 Usage

Signed Ranks are a method of ranking matched pairs data while accounting for the positive or negative nature of the differences. In other words, we can take into account if the treatment or control was higher, without caring about how much higher it was.

3.2 Procedure

For each pair of observations, we’ll take the difference as after - before. We’ll then rank these differences by absolute value, smallest to largest, but retain the sign of the rank from the actual difference. Our test statistic, \(SR_+\), is the sum of positive signed ranks.

More visually:

       Obs 1 Obs 2 Obs 3 Obs 4 Obs 5
before    31    38    46    54    45
after     39    49    55    57    43

becomes

          Obs 1 Obs 2 Obs 1 Obs 4 Obs 5
before       31    38    46    54    45
after        39    49    55    57    43
diff          8    11     9     3    -2
sign.rank     3     5     4     2    -1

Here the the test statistic is found as \(SR_+ = 3+5+4+2 \implies SR_+=14\).
In order to find the p-value, we want to ask how likely is it that we found this test statistic just by random chance, assuming the null to be true? In other words, we’ll find every possible permutation of the sign-ranks, and calculate \(SR_+^*\) for each one. The p-value is just the fraction of ranks sums as or more extreme than what we observed.

P-value Formula

\[ \begin{aligned} P_{\text {upper tail}} &=\frac{\text {number of } SR_+ \text{'s} \geq SR_+}{2^{n}} \\ P_{\text {lower tail}} &=\frac{\text {number of } SR_+ \text{'s} \leq SR_+}{2^{n}} \end{aligned} \]

A brief comment on ties:
Details

Note: When we have multiple pairs with the same difference, we can apply the average rank to the tied observations.

Note: If the difference between a pair is 0, we either omit them from the sample, or ignore them (as they don’t count towards \(SR_+\)). You’d typically keep them since it gives similar results, and if there many zeros, you’d lose plenty of power from the lower sample size.

3.2 R

library(stats)

pre  <- c(1180, 1210, 1300, 1080, 1120, 1240, 1360, 980)
post <- c(1230, 1280, 1310, 1140, 1150, 1200, 1340, 1100)
diff <- post-pre

wilcox.test(diff, alternative="greater")

3.2 Python

# under construction