Home Â» How to Conduct a Wilcoxon Signed-Rank Test in Python

# How to Conduct a Wilcoxon Signed-Rank Test in Python

The Wilcoxon Signed-Rank Test is the non-parametric version of the paired samples t-test.

It is used to test whether or not there is a significant difference between two population means when the distribution of the differences between the two samples cannot be assumed to be normal.

This tutorial explains how to conduct a Wilcoxon Signed-Rank Test in Python.

### Example: Wilcoxon Signed-Rank Test in Python

Researchers want to know if a new fuel treatment leads to a change in the average mpg of a certain car. To test this, they measure the mpg of 12 cars with and without the fuel treatment.

Use the following steps to perform a Wilcoxon Signed-Rank Test in Python to determine if there is a difference in the mean mpg between the two groups.

Step 1: Create the data.

First, weâ€™ll create two arrays to hold the mpg values for each group of cars:

group1 = [20, 23, 21, 25, 18, 17, 18, 24, 20, 24, 23, 19]
group2 = [24, 25, 21, 22, 23, 18, 17, 28, 24, 27, 21, 23]

Step 2: Conduct a Wilcoxon Signed-Rank Test.

Next, weâ€™ll use theÂ wilcoxon() functionÂ from the scipy.stats library to conduct a Wilcoxon Signed-Rank Test, which uses the following syntax:

wilcoxon(x, y, alternative=â€™two-sidedâ€™)

where:

• x:Â an array of sample observations from group 1
• y:Â an array of sample observations from group 2
• alternative:Â defines the alternative hypothesis. Default is â€˜two-sidedâ€™ but other options include â€˜lessâ€™ and â€˜greater.â€™

Hereâ€™s how to use this function in our specific example:

import scipy.stats as stats

#perform the Wilcoxon-Signed Rank Test
stats.wilcoxon(group1, group2)

(statistic=10.5, pvalue=0.044)

The test statistic isÂ 10.5Â and the corresponding two-sided p-value isÂ 0.044.

Step 3: Interpret the results.

In this example, the Wilcoxon Signed-Rank Test uses the following null and alternative hypotheses:

H0:Â The mpg is equal between the two groups

HA:Â The mpg isÂ notÂ equal between the two groups

Since the p-value (0.044) is less than 0.05, we reject the null hypothesis. We have sufficient evidence to say that the true mean mpg is not equal between the two groups.