I have experiment data with multiple independent variables:
shape- 2 factorscolor- 2 factorssize- 3 factors
Each subject provides a response (the dependent variable) for each combination of those independent variables many times.
Here is example data:
library(tidyverse)
data = expand_grid(
subject = paste("S", 1:11),
shape = c("circle", "square"),
color = c("red", "blue"),
size = c("small", "medium", "large"),
replicate = 1:50 # many replicate responses
) %>%
mutate(response = rnorm(n()))
ANOVA with afex
The afex library lets you describe the variables and provides the results of an F-test:
afex::aov_ez(
id = "subject",
dv = "response",
data = data,
within = c("shape", "color", "size"),
anova_table=list(correction = "none") # turn off sphericity correction
)
Response: response
Effect df MSE F ges p.value
1 shape 1, 10 0.01 0.09 <.001 .768
2 color 1, 10 0.01 1.43 .008 .260
3 size 2, 20 0.01 7.88 ** .054 .003
4 shape:color 1, 10 0.03 1.19 .015 .302
5 shape:size 2, 20 0.02 1.68 .030 .211
6 color:size 2, 20 0.02 3.32 + .064 .057
7 shape:color:size 2, 20 0.03 2.31 .050 .125
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Equivalent with lmer?
Is it possible to specify an equivalent model via lmer() and get a similar output? If so, how?
These attempts don't seem right, as the degrees of freedom seem way off:
lmer(response ~ shape*color*size + (1 | subject), data) %>%
report::report()
lmer(response ~ shape*color*size + (1 | subject) + (1 | subject:shape) + (1 | subject:color) + (1 | subject:size), data) %>%
report::report()
Edit:
It might be easier to match the afex output by aggregating first
data_aggregated = data %>%
group_by(subject, shape, color, size) %>%
summarise(response = mean(response), .groups = "drop")