# How to conduct a mixed designs ANOVA (in R)

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
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)

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.