19  Repeated-Measures and Mixed ANOVA

NoteWhat you’ll be able to do

By the end of this chapter you will be able to:

  1. Recognize repeated-measures and mixed designs — and not confuse three repeated measurements with three independent groups.
  2. Choose between a paired t-test and repeated-measures ANOVA by the number of time points.
  3. Run a repeated-measures ANOVA with afex and read the output, including η_G² and pairwise comparisons.
  4. Explain why ordinary aov() on repeated-measures data is wrong even when it “works.”

Measure a competitor’s hormone levels before, during, and after a match, and you have three numbers from the same body. Those measurements are not independent — someone whose baseline runs high will likely run high at all three time points — and standard ANOVA’s math assumes they are. Designs like this need machinery that respects who the data came from.

When the same subjects are measured under multiple conditions, you have a repeated-measures (or within-subjects) design. When you combine that with a between-subjects factor — different groups, each measured across multiple conditions — you have a mixed design.

These designs are common in behavioral research: pre/mid/post measurements, time-series within a trial, hormone levels at several time points, and so on. They require slightly different statistical machinery than between-subjects ANOVA, because observations within the same person are not independent.

This chapter covers both designs using the afex package, which makes them tractable.

19.1 Why these designs need special handling

The standard ANOVA assumes observations are independent. When the same person is measured three times, those three measurements are not independent — people who score high once tend to score high again.

If you ignore this and run a between-subjects ANOVA on repeated data, you get the wrong error term, and which way it goes wrong depends on the factor. For a within-subject factor (time), the stable person-to-person differences get dumped into the error term, so the test becomes far too conservative and misses real effects. For a between-subject factor (group), each person’s repeated scores get counted as if they were separate people, which over-counts the sample and inflates Type I error (false positives). Repeated-measures ANOVA fixes both by partitioning out subject-level variability.

19.2 When to use each design

Design When
One-way repeated-measures Same subjects, one within-subject factor (e.g., time: pre/mid/post)
Factorial repeated-measures Same subjects, two+ within-subject factors
Mixed ANOVA Between-subject factor (e.g., group) × within-subject factor (e.g., time)

Count the time points first. With two, use the paired t-test (Chapter 16). With three or more, use repeated-measures ANOVA; running a paired t-test for every pair of time points multiplies your chances of a false alarm.

The Cambridge fellowship dataset (covered in Chapter 32) is a classic mixed design: subjects are between on win/loss, and within on three hormone measurement times.

19.3 Setup

You’ll need the afex and emmeans packages:

install.packages(c("afex", "emmeans"))
library(afex)
library(emmeans)

afex provides a clean interface for repeated-measures designs that handles many of the technical issues automatically (sphericity correction, type III sums of squares, proper error terms).

19.4 Data format: long, not wide

For repeated-measures ANOVA, your data should be in long format — one row per observation, not one row per subject. If your data are in wide format (one row per subject, columns for each measurement), you need to reshape first. See Chapter 9.

A correctly-shaped dataset for a one-way repeated-measures ANOVA looks like:

subject time hormone
1 t1 12.4
1 t2 18.1
1 t3 15.3
2 t1 10.0
2 t2 14.5
… … …

subject identifies who; time is the within-subject factor; hormone is the outcome.

19.5 One-way repeated-measures ANOVA

Suppose 25 subjects are each measured at three time points, and you want to test whether hormone level changes over time.

library(afex)

# Load the data (already in long format: one row per subject per time point)
data <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/ch19_hormone_rm.csv")

# Make sure time is a factor and subject is an identifier (not factor of interest)
data$time    <- as.factor(data$time)
data$subject <- as.factor(data$subject)

# Fit the model with aov_ez
model <- aov_ez(
  id = "subject",        # the subject identifier column
  dv = "hormone",        # the outcome variable
  within = "time",       # within-subject factor(s)
  data = data
)

# View the ANOVA table
model

Output:

Anova Table (Type 3 tests)

Response: hormone
  Effect          df   MSE         F  ges p.value
1   time 1.89, 45.43 14.15 16.12 *** .207   <.001
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1

Sphericity correction method: GG 
  • df = 1.89, 45.43 — the test’s degrees of freedom, after the Greenhouse-Geisser correction. Uncorrected they would be 2 and 48; afex multiplies both by the estimated sphericity ε (here 0.95), which is why they aren’t whole numbers.
  • F = 16.12 — the F-statistic.
  • ges = .207 — generalized eta-squared, the recommended effect size for repeated-measures designs.
  • p.value < .001 — significant.
  • Sphericity correction method: GG — confirms the correction was applied.

summary(model) gives the long version: the uncorrected test, Mauchly’s test of sphericity, and both the Greenhouse-Geisser and Huynh-Feldt corrections.

NoteSphericity and corrections

Repeated-measures ANOVA assumes sphericity — that the variances of the differences between all pairs of conditions are equal. When sphericity is violated (often the case in real data), the p-value is too small.

afex applies the Greenhouse-Geisser correction by default to every within-subject factor with three or more levels — whether or not sphericity looks violated (in the example above, Mauchly’s test was not significant, p = .512, and the correction was still applied). That’s a deliberately safe default: when sphericity holds, ε is close to 1 and the correction barely changes anything. You don’t have to do anything; report the corrected df and p-value from the output.

19.5.1 Follow-up pairwise comparisons

emmeans(model, pairwise ~ time, adjust = "tukey")

This gives you all pairwise comparisons of time-point means with Tukey-style correction.

19.6 Factorial repeated-measures ANOVA

If you have two within-subject factors (e.g., time × treatment, both varying within subject):

model <- aov_ez(
  id = "subject",
  dv = "score",
  within = c("time", "condition"),
  data = data
)
model

The output gives you main effects of time and condition plus the interaction.

19.7 Mixed ANOVA (between × within)

NoteFor reference

You won’t run a mixed ANOVA in this course. It’s here so you know the tool exists: if your own project ever compares groups that are each measured over time, this is the analysis, and it uses the same aov_ez() you already know with one more argument (between =).

When you have a between-subjects factor (group) and a within-subjects factor (time), specify both:

data$group <- as.factor(data$group)

model <- aov_ez(
  id = "subject",
  dv = "score",
  between = "group",       # between-subject factor
  within  = "time",        # within-subject factor
  data    = data
)
model

The output has three effects:

  • Main effect of group (between)
  • Main effect of time (within)
  • Group × Time interaction

The interaction is typically the most interesting term: it tests whether the time course differs across groups.

19.7.1 Follow-up

# Pairwise comparisons of time within each group
emmeans(model, pairwise ~ time | group, adjust = "tukey")

# Or pairwise comparisons of group within each time
emmeans(model, pairwise ~ group | time, adjust = "tukey")
ImportantCommon Misconception: running aov() on repeated-measures data is fine if it gives a significant result

Using aov() (between-subjects ANOVA) on repeated-measures data violates the independence assumption. Measurements from the same person are correlated — people who score high once tend to score high again. Standard ANOVA does not account for this correlation, so it uses the wrong error term. For a within-subject factor the test becomes badly underpowered; for a between-subject factor measured repeatedly, it treats one person’s scores as separate people and produces false positives. Either way, the p-value belongs to the wrong model. Use aov_ez() from afex — it partitions subject-level variability properly and applies sphericity corrections automatically.

19.8 Assumptions

  1. Independence between subjects. Different subjects are independent of each other.
  2. Normality of residuals. Same as other ANOVAs.
  3. Sphericity (for repeated-measures factor with 3+ levels). afex handles this automatically with Greenhouse-Geisser.
  4. Homogeneity of variance across between-subject groups (for mixed designs).

19.9 Effect size: generalized eta-squared

For repeated-measures designs, generalized eta-squared (\(\eta_G^2\), reported as ges by afex) is preferred over partial eta-squared because it’s comparable across designs.

# Already shown as ges when you print model
# Or directly:
library(effectsize)
eta_squared(model, generalized = TRUE)

Same conventions: 0.01 small, 0.06 medium, 0.14 large.

19.10 Reporting

19.10.1 One-way repeated-measures

A one-way repeated-measures ANOVA was conducted to compare hormone levels across three time points (t1, t2, t3) in 25 subjects, with a Greenhouse-Geisser correction for sphericity. There was a significant effect of time, F(1.89, 45.43) = 16.12, p < .001, \(\eta_G^2\) = .21 (large effect). Pairwise comparisons (Tukey-adjusted) indicated that hormone levels at t2 (M = 18.28) were significantly higher than at t1 (M = 12.42; p < .001) and t3 (M = 14.96; p = .018); t1 and t3 did not differ significantly (p = .061).

19.10.2 Mixed ANOVA

From the real Cambridge data (Chapter 32), all participants with cortisol at all three time points (n = 247):

A 2 (outcome: win, loss) × 3 (time: t1, t2, t3) mixed ANOVA was conducted on cortisol levels, with a Greenhouse-Geisser correction for sphericity. There was a significant main effect of time, F(1.48, 363.32) = 143.34, p < .001, \(\eta_G^2\) = .07: cortisol declined across the session. Neither the main effect of outcome, F(1, 245) = 1.06, p = .304, \(\eta_G^2\) < .01, nor the outcome × time interaction, F(1.48, 363.32) = 1.50, p = .227, \(\eta_G^2\) < .01, was significant. Tukey-adjusted follow-ups showed cortisol fell from each time point to the next in both winners and losers (all p < .001).

A non-significant interaction is a real and reportable result: here, winning or losing did not change the cortisol time course.

19.11 Common mistakes

Wrong Right
Running standard aov() on repeated-measures data Use aov_ez() from afex
Wide-format data fed to aov_ez() Reshape to long format first (Chapter 9)
Subject column treated as a factor of interest Subject is an ID, not a fixed factor
Reporting partial eta-squared without noting design Use generalized eta-squared for repeated-measures
Ignoring sphericity entirely afex corrects automatically; trust the output

19.12 A note on the older aov() syntax

You may have seen code like:

aov(score ~ time + Error(subject/time), data = data)

This is the classical base-R syntax for repeated-measures ANOVA. It works, but it’s fussy about balanced data, doesn’t apply sphericity corrections automatically, and produces output that’s hard to parse. afex::aov_ez() is the modern preferred approach for this course.

19.13 Quick reference

Element R code
Load packages library(afex); library(emmeans)
One-way repeated-measures aov_ez(id, dv, within, data)
Mixed (between × within) aov_ez(id, dv, between, within, data)
Two within-subject factors aov_ez(id, dv, within = c("A", "B"), data)
ANOVA table (corrected df, ges) model
Mauchly’s test and ε summary(model)
Pairwise comparisons emmeans(model, pairwise ~ factor, adjust = "tukey")
Cell means emmeans(model, ~ factor)
Non-parametric (within) friedman.test(score ~ condition | subject, data)
TipReporting checklist for repeated-measures / mixed ANOVA
NoteInterpret the Output

Read this output carefully. The R code that produced it is not shown.

Anova Table (Type 3 tests)

Response: cortisol
     num Df den Df    MSE      F     ges    Pr(>F)    
time      2     48 1.9704 11.336 0.12649 9.287e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  1. What package produced this output? How do you know?
  2. How many time points are being compared? How many subjects are in the study? (Hint: for a one-way repeated-measures ANOVA, num Df = k − 1 and den Df = (n − 1)(k − 1). With k = 3 and den Df = 48, solve for n.)
  3. What effect size is reported? How do you interpret it?
  4. Is sphericity assumed in this output? How would you know if a Greenhouse-Geisser correction had been applied?
  5. The result is significant. What is the next appropriate analysis?
  6. Write one APA 7 sentence reporting this result.
  7. A student says “I could have just run three paired t-tests comparing each pair of time points.” What is wrong with this approach?

19.14 The same test, different disciplines

Repeated-measures and mixed ANOVA apply wherever the same unit is measured across multiple conditions or time points.

Discipline Typical design Within-subject factor
Psychology Same participants across conditions, time points, or stimuli Cognitive performance across three experimental conditions; mood at pre, mid, and post-intervention
Biology Same organism measured longitudinally or across treatments Hormone level at multiple time points; physiological response under repeated stimulation
Sociology Same panel respondents tracked across survey waves Attitude or income change across years; longitudinal health or behavior tracking

Next: relationships between continuous variables.


TipChapter Summary

Core idea: When the same participants are measured multiple times, observations within a person are correlated — not independent. Repeated-measures ANOVA partitions out this within-person consistency, reducing error variance and increasing power compared to a between-subjects design.

You should understand:

  • Why you cannot use standard aov() on repeated-measures data
  • What sphericity is and why afex handles it automatically
  • The difference between a repeated-measures design (all factors within-subject) and a mixed design (one between, one within)
  • Why generalized eta-squared (\(\eta_G^2\)) is preferred for repeated-measures designs

You should be able to do in R:

  • One-way repeated-measures: aov_ez(id = "subject", dv = "outcome", within = "time", data = data)
  • Mixed ANOVA: aov_ez(id = "subject", dv = "outcome", between = "group", within = "time", data = data)
  • Follow-up comparisons: emmeans(model, pairwise ~ time, adjust = "tukey")

Plain-language check: A mixed ANOVA gives a significant group × time interaction. A classmate says “the time effect is significant too, so time matters on its own.” What needs to be added to that conclusion?

The one mistake to avoid: Running repeated-measures data through aov() without the Error() term (or aov_ez()). The result uses the wrong error term: within-subject effects get badly underpowered tests, between-subject effects get inflated false-positive rates, and the effect sizes are wrong either way.

Key terms from this chapter are defined in the Glossary.

NoteRepeated-Measures ANOVA Practice

Practice the skills from this chapter using simulated practice data: Repeated-Measures ANOVA Practice: Reaction Time Across the Day.