How to Perform the Friedman Test in Python

The Friedman Test is a non-parametric alternative to the Repeated Measures ANOVA. It is used to determine whether or not there is a statistically significant difference between the means of three or more groups in which the same subjects show up in each group.

This tutorial explains how to perform the Friedman Test in Python.

Example: The Friedman Test in Python

A researcher wants to know if the reaction times of patients is equal on three different drugs. To test this, he measures the reaction time (in seconds) of 10 different patients on each of the three drugs.

Use the following steps to perform the Friedman Test in Python to determine if the mean reaction time differs between drugs.

Step 1: Enter the data.

First, we’ll create three arrays that contain the response times for each patient on each of the three drugs:

group1 = [4, 6, 3, 4, 3, 2, 2, 7, 6, 5]
group2 = [5, 6, 8, 7, 7, 8, 4, 6, 4, 5]
group3 = [2, 4, 4, 3, 2, 2, 1, 4, 3, 2]

Step 2: Perform the Friedman Test.

Next, we’ll perform the Friedman Test using the friedmanchisquare() function from the scipy.stats library:

from scipy import stats

#perform Friedman Test
stats.friedmanchisquare(group1, group2, group3)

(statistic=13.3514, pvalue=0.00126)

Step 3: Interpret the results.

The Friedman Test uses the following null and alternative hypotheses:

The null hypothesis (H0): The mean for each population is equal.

The alternative hypothesis: (Ha): At least one population mean is different from the rest.

In this example, the test statistic is 13.3514 and the corresponding p-value is p = 0.00126. Since this p-value is less than 0.05, we can reject the null hypothesis that the mean response time is the same for all three drugs.

In other words, we have sufficient evidence to conclude that the type of drug used leads to statistically significant differences in response time.

Featured Posts

2 Replies to “How to Perform the Friedman Test in Python”

  1. Since Friedman uses rank to calculate the test statistic shouldn’t we use ‘median’ instead of mean in the null hypothesis?
    Let me know if it makes sense.
    Thanks for the post!

  2. Hi, I would like to know how can I interpret the test statistic parameter and how to use it. Thank you very much.

Leave a Reply

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