How to conduct a mixed designs ANOVA (in Python, using pandas and pingouin)
Task
When you have a dataset that includes the responses of a mixed design test, where one factor is a within-subjects factor and the other is a between-subjects factor, and you wish check if there is a significant difference for both factors, this requires a Mixed Design ANOVA. How can we conduct one?
Related tasks:
- How to do a one-way analysis of variance (ANOVA)
- How to do a two-way ANOVA test with interaction
- How to do a two-way ANOVA test without interaction
- How to compare two nested linear models using ANOVA
- How to conduct a repeated measures ANOVA
- How to perform an analysis of covariance (ANCOVA)
Solution
We create the data for a hypothetical $2\times2$ mixed design with the following attributes.
- Between-subjects treatment factor: Type of music played (classical vs. rock)
- Within-subjects treatment factor: Type of room (light vs. no light)
- Outcome variable: Heart rate of subject
1
2
3
4
5
6
7
8
9
10
11
12
import pandas as pd
df = pd.DataFrame( {
'Subject' : [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10],
'Music' : ['Classical','Rock','Classical','Rock','Classical','Rock','Classical',
'Rock','Classical','Rock','Classical','Rock','Classical','Rock','Classical',
'Rock','Classical','Rock','Classical','Rock'],
'Room Type' : ['Light','Light','Light','Light','Light','Light','Light','Light','Light',
'Light','No Light','No Light','No Light','No Light','No Light','No Light',
'No Light','No Light','No Light','No Light'],
'Heart Rate' : [78,60,85,75,99,94,75,84,100,76,90,109,99,94,113,92,91,88,89,90]
} )
df.head()
Subject | Music | Room Type | Heart Rate | |
---|---|---|---|---|
0 | 1 | Classical | Light | 78 |
1 | 2 | Rock | Light | 60 |
2 | 3 | Classical | Light | 85 |
3 | 4 | Rock | Light | 75 |
4 | 5 | Classical | Light | 99 |
We will use the pingouin
statistics package to conduct a two-way mixed-design ANOVA. The parameters are as follows:
dv
: name of the column containing the dependant variablewithin
: name of the column containing the within-group factorbetween
: name of the column containing the between-group factorsubject
: name of the column identifying each subjectdata
: the pandas DataFrame containing all the data
1
2
import pingouin as pg
pg.mixed_anova( dv='Heart Rate', within='Room Type', between='Music', subject='Subject', data=df )
Source | SS | DF1 | DF2 | MS | F | p-unc | np2 | eps | |
---|---|---|---|---|---|---|---|---|---|
0 | Music | 162.45 | 1 | 8 | 162.45 | 1.586813 | 0.243288 | 0.165520 | NaN |
1 | Room Type | 832.05 | 1 | 8 | 832.05 | 6.416426 | 0.035088 | 0.445077 | 1.0 |
2 | Interaction | 76.05 | 1 | 8 | 76.05 | 0.586466 | 0.465781 | 0.068301 | NaN |
The output informs us that, on average, the subjects that listened to classical music did not significantly differ ($p = 0.243288 > 0.05$) from those that listened to rock music. However, there is, on average, a significant difference ($p = 0.035088 < 0.05$) between each of the subject’s heart rate when put in a room with or without light. Additionally, since the interaction term is not significant ($p = 0.465781 > 0.05$), we can use the additive (no interaction) model.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Krtin Juneja (KJUNEJA@falcon.bentley.edu)