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:

H0The mpg is equal between the two groups

HAThe 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.

Leave a Reply

Your email address will not be published. Required fields are marked *