# How to conduct a mixed designs ANOVA (in Python, using pandas and pingouin)

See all solutions.

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?

## 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]
} )

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:

1. dv: name of the column containing the dependant variable
2. within: name of the column containing the within-group factor
3. between: name of the column containing the between-group factor
4. subject: name of the column identifying each subject
5. data: 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.