Here is what is covered in this section:

## What is it?

The Wilcoxon signed-rank test is the non-parametric univariate test which is an alternative to the dependent t-test. It also is called the Wilcoxon T test, most commonly so when the statistic value is reported as a T value. Which *scipy.stats.wilcoxon()* uses for it’s calculation. This is the recommended test to use when the data violates the assumption of normality. It’s used to test if there is a significant difference on scores when there is a “before” and “after” condition of some treatment or intervention. An example of this is if you where to collect the blood pressure for an individual before and after some treatment, condition, or time point.

The hypothesis being test is:

**Null hypothesis (H**: The difference between the pairs follows a symmetric distribution around zero._{0})**Alternative hypothesis (H**: The difference between the pairs does not follow a symmetric distribution around zero._{A})

If the p-value is less than what is tested at, most commonly 0.05, one can reject the null hypothesis.

## Wilcoxon signed-rank test Assumptions

In order for the Wilcoxon signed-rank test results to be trusted, the following assumptions need to be met:

- The dependent variable (DV) must be continuous which is measured on an ordinal or continuous scale
- The paired observations are randomly and independently drawn
- The paired observations come from the same population

If any of these assumptions are violated, a different test should be used.

##### Data used in this example

The data used in this example can be found on our GitHub page. The data set is fictitious and contains blood pressure readings before and after an intervention. These are variables “bp_before” and “bp_after”.

Let’s import pandas as pd, the data, and then take a look at the data!

import pandas as pd df = pd.read_csv("blood_pressure.csv") df[['bp_before','bp_after']].describe()

bp_before | bp_after | |
---|---|---|

count | 120.00 | 120.00 |

mean | 156.450000 | 151.358333 |

std | 11.389845 | 14.177622 |

min | 138.000000 | 125.000000 |

25% | 147.000000 | 140.750000 |

50% | 154.500000 | 149.500000 |

75% | 164.000000 | 161.000000 |

max | 185.000000 | 185.000000 |

## Wilcoxon sign ranked test example

First thing we need to do is import the Scipy stats library.

from scipy import stats

This test has no assumptions about the distribution of the data so there is nothing to check in that regard. This test works similarly to dependent t-test, in that the test is based on the differences between the two conditions that are being paired.

There are two ways to go about this using the *scipy.stats.wilcoxon(*) method. The first is to calculate the differences between the two conditions and pass that through the method, and the second is much simpler where one enters the two conditions and let’s Python take care of everything. This page will show both.

Going the first way of calculating the difference between the two conditions. If there is a difference of 0, these scores need to be excluded from the ranking. Let’s calculate this and see if there are any differences that are equal to 0.

df['bp_difference'] = df['bp_before'] - df['bp_after'] df['bp_difference'][df['bp_difference']==0]

41 | 0 |

74 | 0 |

103 | 0 |

115 | 0 |

Since there are differences of 0, these scores need to be excluded from the ranking process. Although this is an easy process to do using Python, the test method itself will automatically exclude any 0 difference from the calculation so there is nothing more that needs to be done. The data is now ready.

To conduct the test, one needs to use the *stats.wilcoxon* method. The first example code will show how to use the method using a variable that contains the difference between the conditions, and the second example code will show how to use the method using both variables.

stats.wilcoxon(df['bp_difference'])

stats.wilcoxon(df['bp_before'], df['bp_after'])

As you can see, conducting this test either way will yield the exact same results. The findings are statistically significant! One can reject the null hypothesis in support of the alternative.

## Interpretation of the Results

A Wilcoxon T test was used to analyze the blood pressure before and after the intervention to test if the intervention had a significant affect on the blood pressure. The blood pressure before the intervention was higher (M= 156.45 ± 11.39 units) compared to the blood pressure post intervention (M= 151.36 ± 14.18 units); there was a statistically significant decrease in blood pressure (t=2,234.5, p= 0.0014).

Note: Reported as Wilcoxon T test since *Scipy.stats.wilcoxon(*) method reports the T value and not the W value. Full documentation on the method can be found here.