It might be helpful to have some example data to make it more clear. A simple example comes from table 4 of Winer, Brown, & Michels. The data has the following form:
| person | drug | score |
|---|---|---|
| 1 | 1 | 30 |
| 1 | 2 | 28 |
| 1 | 3 | 16 |
| 1 | 4 | 34 |
| 2 | 1 | 14 |
| 2 | 2 | 18 |
| 2 | 3 | 10 |
| 2 | 4 | 22 |
and so on for 5 persons.
In this data, drugs are repeatedly measured within the same person(s). So you could say that person is a random factor and drugs are a fixed, repeated factor. I tend to work on mixed modeling problems, and I would analyze this data as follows:
library(haven)
library(dplyr)
dat <- read_dta("https://www.stata-press.com/data/r16/t43.dta")
dat <- dat %>% mutate(drug = as.factor(drug), person = as.factor(person))
library(lme4)
library(lmerTest)
# Random effects ANOVA ignoring drug
m0 <- lmer(score ~ 1 + (1|person), data = dat)
summary(m0, ddf = "Kenward-Roger")
fit.person_ran.aov <- aov(score ~ Error(person), data = dat)
summary(fit.person_ran.aov, ddf = "Kenward-Roger")
Error: person
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 4 680.8 170.2
Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 15 811 54.07
Now, let's add the repeated measure, drug. I use the Kenward-Roger degrees of freedom correction given that we are working with a tiny sample:
# Random effect with repeated measures (drug)
m1 <- lmer(score ~ 1 + drug + (1|person), data = dat)
summary(m1,ddf = "Kenward-Roger")
anova(m1, ddf=c("Kenward-Roger"))
Type III Analysis of Variance Table with Kenward-Roger's method
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
drug 698.2 232.73 3 12 24.759 1.993e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Just to be sure, let's use aov() to see if things line up:
fit.person_drug.aov <- aov(score ~ drug + Error(person), data = dat)
summary(fit.person_drug.aov, ddf = "Kenward-Roger")
Error: person
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 4 680.8 170.2
Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
drug 3 698.2 232.7 24.76 1.99e-05 ***
Residuals 12 112.8 9.4
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1