How to conduct a mixed designs ANOVA (in R)
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
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 persubject
for each level ofroom.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.
Contributed by Krtin Juneja (KJUNEJA@falcon.bentley.edu)