In many psychological studies, the same people are measured multiple times – e.g., before, during, and after an intervention. You want to know: Is there an effect over time?
Then you need the Repeated Measures ANOVA, also known as Repeated Measures ANOVA.
Example: Mindfulness Training and Stress
Research question: Does participants’ stress level change over time—that is, before, during, and after mindfulness training?
1. Generate Example Data
# Simulated data: 15 people, 3 measurement points
set.seed(123)
daten <- data.frame(
id = factor(1:15),
vor = rnorm(15, mean = 75, sd = 5),
waehrend = rnorm(15, mean = 70, sd = 5),
nach = rnorm(15, mean = 65, sd = 5)
)
# Convert data to long format
library(tidyr)
daten_long <- pivot_longer(daten, cols = vor:nach,
names_to = "zeitpunkt", values_to = "stress")
daten_long$zeitpunkt <- factor(daten_long$zeitpunkt, levels = c("vor", "waehrend", "nach"))
2. Initial visualization
library(ggplot2)
ggplot(daten_long, aes(x = zeitpunkt, y = stress, group = id)) +
geom_line(alpha = 0.5) +
stat_summary(aes(group = 1), fun = mean, geom = "line", color = "red", size = 1.2) +
theme_minimal() +
labs(title = "Stress level over time", y = "Stress", x = "Measurement time point")
Interpretation: The red line shows the mean across all participants—if it decreases, the training is having an effect!
3. Calculate the model with aov()
modell_rm <- aov(stress ~ zeitpunkt + Error(id/zeitpunkt), data = daten_long)
summary(modell_rm)
Example output:
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 14 2850 203.57
Error: id:zeitpunkt
Df Sum Sq Mean Sq F value Pr(>F)
zeitpunkt 2 750 375.0 18.34 0.00012 ***
Residuals 28 572 20.4
4. Interpretation
Repeated-measures ANOVA tests whether the means of a group change significantly across different time points—while accounting for the dependence of the measurements (the same people).
- The F-value is 18.34
- The p-value is very small ($p < 0{,}001$)
Conclusion: The stress level has changed significantly—it probably decreased!
5. Check the assumptions
a) Normal distribution of the differences
ANOVA assumes that the differences between time points are normally distributed. One simple option is:
with(daten, shapiro.test(vor - waehrend))
with(daten, shapiro.test(waehrend - nach))
$p > 0{,}05$ → Normal distribution is acceptable
b) Sphericity (only with ≥ 3 time points)
Sphericity means that the variances of the differences between all time points are equal.
It is tested using Mauchly’s test.
# Install package:
install.packages("ez")
library(ez)
# Repeated-measures ANOVA with Mauchly's test
ezANOVA(data = daten_long, dv = .(stress), wid = .(id),
within = .(zeitpunkt), detailed = TRUE)
If sphericity is violated, ezANOVA() automatically provides the Greenhouse–Geisser correction as well.
6. Post-hoc tests: Which time points differ from one another?
pairwise.t.test(daten_long$stress, daten_long$zeitpunkt,
paired = TRUE, p.adjust.method = "bonferroni")
Interpretation: You can see all pairwise comparisons (before–during, before–after, during–after) and identify which differences are significant.
Summary: Repeated Measures ANOVA in R
| Step | What you do |
|---|---|
| 1. | Create the dataset in wide format and convert it to long format |
| 2. | Visualize with ggplot2 |
| 3. | aov() with Error(id/zeitpunkt) |
| 4. | Check the assumptions (normality, sphericity) |
| 5. | pairwise.t.test() for post-hoc comparisons |
Q&A – Test Your Knowledge
Question 1: When do you need a repeated-measures ANOVA?
Answer: When you measure the same people multiple times (e.g., before, during, and after an intervention).
Question 2: What does sphericity mean?
Answer: That the variance of the differences between time points is equal.
Question 3: Which function do you use for repeated measures in R?
Answer: aov(... + Error(id/zeitpunkt))
Question 4: How do you visualize changes over time in R?
Answer: With ggplot2 and lines for each person plus the average.
Question 5: What does a significant post-hoc test tell you?
Answer: Which individual time points differ significantly from one another.
