Home » How to Perform a Repeated Measures ANOVA in Python

How to Perform a Repeated Measures ANOVA in Python

by Erma Khan

repeated measures ANOVA 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 conduct a one-way repeated measures ANOVA in Python.

Example: Repeated Measures ANOVA in Python

Researchers want to know if four different drugs lead to different reaction times. To test this, they measure the reaction time of five patients on the four different drugs. Since each patient is measured on each of the four drugs, we will use a repeated measures ANOVA to determine if the mean reaction time differs between drugs.

Use the following steps to perform the repeated measures ANOVA in Python.

Step 1: Enter the data.

First, we’ll create a pandas DataFrame to hold our data:

import numpy as np
import pandas as pd

#create data
df = pd.DataFrame({'patient': np.repeat([1, 2, 3, 4, 5], 4),
                   'drug': np.tile([1, 2, 3, 4], 5),
                   'response': [30, 28, 16, 34,
                                14, 18, 10, 22,
                                24, 20, 18, 30,
                                38, 34, 20, 44, 
                                26, 28, 14, 30]})

#view first ten rows of data 
df.head[:10]


	patient	drug	response
0	1	1	30
1	1	2	28
2	1	3	16
3	1	4	34
4	2	1	14
5	2	2	18
6	2	3	10
7	2	4	22
8	3	1	24
9	3	2	20	   

Step 2: Perform the repeated measures ANOVA.

Next, we will perform the repeated measures ANOVA using the AnovaRM() function from the statsmodels library:

from statsmodels.stats.anova import AnovaRM

#perform the repeated measures ANOVA
print(AnovaRM(data=df, depvar='response', subject='patient', within=['drug']).fit())

              Anova
==================================
     F Value Num DF  Den DF Pr > F
----------------------------------
drug 24.7589 3.0000 12.0000 0.0000
==================================

Step 3: Interpret the results.

A repeated measures ANOVA uses the following null and alternative hypotheses:

The null hypothesis (H0): µ1 = µ2 = µ3 (the population means are all equal)

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

In this example, the F test-statistic is 24.7589 and the corresponding p-value is 0.0000. Since this p-value is less than 0.05, we reject the null hypothesis and conclude that there is a statistically significant difference in mean response times between the four drugs.

Step 4: Report the results.

Lastly, we will report the results of our repeated measures ANOVA. Here is an example of how to do so:

A one-way repeated measures ANOVA was conducted on 5 individuals to examine the effect that four different drugs had on response time.

 

Results showed that the type of drug used lead to statistically significant differences in response time (F(3, 12) = 24.75887, p

Related Posts