Link Search Menu Expand Document (external link)

How to conduct a mixed designs ANOVA

Description

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:

Using pandas and pingouin, in Python

View this solution alone.

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:

  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.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Solution, in R

View this solution alone.

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
subject    <- as.factor(c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10))
music      <- c('Classical','Rock','Classical','Rock','Classical','Rock','Classical',
                'Rock','Classical','Rock','Classical','Rock','Classical','Rock','Classical',
                'Rock','Classical','Rock','Classical','Rock')
room.type  <- c('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 <- c(78,60,85,75,99,94,75,84,100,76,90,109,99,94,113,92,91,88,89,90)
df <- data.frame(subject,music,room.type,heart.rate)
head(df)
1
2
3
4
5
6
7
  subject music     room.type heart.rate
1 1       Classical Light     78        
2 2       Rock      Light     60        
3 3       Classical Light     85        
4 4       Rock      Light     75        
5 5       Classical Light     99        
6 6       Rock      Light     94        

We conduct a two-way mixed-design ANOVA as shown below. The specific parameters have these meanings:

  • The dependent variable is heart.rate.
  • The within-group factor is room.type.
  • The between-group factor is music.
  • The Error() term is critical in differentiating between a between subjects and within subjects model. It tells R that there is one observation per subject for each level of room.type.
1
2
aov_mixed <- aov(heart.rate ~ room.type*music + Error(subject/room.type), data=df)
summary(aov_mixed)
1
2
3
4
5
6
7
8
9
10
11
12
Error: subject
          Df Sum Sq Mean Sq F value Pr(>F)
music      1  162.4   162.4   1.587  0.243
Residuals  8  819.0   102.4               

Error: subject:room.type
                Df Sum Sq Mean Sq F value Pr(>F)  
room.type        1  832.1   832.1   6.416 0.0351 *
room.type:music  1   76.0    76.0   0.586 0.4658  
Residuals        8 1037.4   129.7                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The output informs us that, on average, the subjects that listened to classical music did not significantly differ ($p = 0.243 > 0.05$) from those that listened to rock music. However, there is, on average, a significant difference ($p = 0.0351 < 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.4658 > 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.

Topics that include this task

Opportunities

This website does not yet contain a solution for this task in any of the following software packages.

  • Excel
  • Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.