Two-Way ANOVA in R

One-Way ANOVA is great when you want to investigate the influence of one factor on a numerical outcome variable. But what if you have two—and want to know whether they have an effect together or even in combination?

Then you need a two-way ANOVA, also called Two-Way ANOVA.


Example: Learning Method and Exam Type

Research question:
Do the learning method (flashcards vs. videos) and the type of exam (multiple choice vs. essay) affect the scores achieved?

And additionally:
Do the two factors interact? In other words: Does the effectiveness of the learning method depend on which type of exam was chosen?


1. Creating Example Data

# Two factors: method (2 levels), exam type (2 levels)
set.seed(42)
lerndaten2 <- expand.grid(
  methode = c("Flashcards", "Videos"),
  pruefungsart = c("MC", "Essay"),
  teilnehmende = 1:20
)

# Simulate scores
lerndaten2$punkte <- with(lerndaten2,
  ifelse(methode == "Flashcards" & pruefungsart == "MC", rnorm(20, 80, 5),
  ifelse(methode == "Flashcards" & pruefungsart == "Essay", rnorm(20, 75, 5),
  ifelse(methode == "Videos" & pruefungsart == "MC", rnorm(20, 70, 5),
         rnorm(20, 68, 5)))))

# Convert to factors
lerndaten2$methode <- factor(lerndaten2$methode)
lerndaten2$pruefungsart <- factor(lerndaten2$pruefungsart)

2. First overview

boxplot(punkte ~ methode * pruefungsart, data = lerndaten2,
        col = "lightblue", main = "Scores by method & exam type",
        ylab = "Score", xlab = "Group")

This gives you a first visual idea of whether and where differences exist.


3. Check assumptions

a) Normality of the residuals

modell2 <- aov(punkte ~ methode * pruefungsart, data = lerndaten2)
shapiro.test(residuals(modell2))
qqnorm(residuals(modell2))
qqline(residuals(modell2), col = "red")

b) Homogeneity of variance

library(car)
leveneTest(punkte ~ methode * pruefungsart, data = lerndaten2)

Note: If assumptions are violated → consider a transformation or more robust methods (e.g., welch.test() from the rstatix package).


4. Estimate the model: Two-Way ANOVA

# Model with main effects and interaction
modell2 <- aov(punkte ~ methode * pruefungsart, data = lerndaten2)
summary(modell2)

Example output:

rCopyEdit                       Df Sum Sq Mean Sq F value   Pr(>F)    
methode                 1   760     760   18.3     0.0001 ***
pruefungsart            1   420     420   10.1     0.0025 **
methode:pruefungsart    1    95      95    2.3     0.14    
Residuals              76  3150      41

5. Interpretation

Definition (Box):
Main effect: The influence of one factor independent of the other
Interaction: The effect of one factor depends on the other

In our example:

  • Both main effects are significant → Method and exam type affect scores
  • No significant interaction → The effect of the learning method does not change depending on the exam type

6. Visualizing the interaction

library(ggplot2)

ggplot(lerndaten2, aes(x = methode, y = punkte, fill = pruefungsart)) +
stat_summary(fun = mean, geom = "bar", position = "dodge") +
stat_summary(fun.data = mean_se, geom = "errorbar",
position = position_dodge(width = 0.9), width = 0.2) +
theme_minimal() +
labs(title = "Average score",
y = "Scores", x = "Learning method")

Interpretation:

  • Parallel bars → No interaction
  • Crossing bars → Possible interaction (check visually!)

7. Post-hoc tests (optional)

If you have more than 2 levels per factor, you need post-hoc tests (e.g., TukeyHSD(), emmeans). However, they are not necessary here because each variable has only two groups.


Summary: Two-Way ANOVA in R

StepWhat you do
1.Prepare the dataset (two factors)
2.Test the assumptions (normality, Levene’s test)
3.Calculate the model with aov()
4.Interpret main effects and interactions
5.Visualize with ggplot2

Q&A – Test Your Knowledge

Question 1: When do you need a two-way ANOVA?
Answer: When you want to examine the effects of two categorical predictors on a metric outcome.

Question 2: What is an interaction?
Answer: When the effect of one factor changes depending on the level of the other factor.

Question 3: Do you always have to conduct post-hoc tests?
Answer: Only when there are more than two groups per factor and the main effects are significant.

Question 4: What does Levene’s test tell you?
Answer: Whether the groups have homogeneous variances—a prerequisite for ANOVA.

Question 5: Which function do you use to run a Two-Way ANOVA in R?
Answer: aov(punkte ~ faktor1 * faktor2, data = datensatz)

Alles klar?

Ich hoffe, der Beitrag war für dich soweit verständlich. Wenn du weitere Fragen hast, nutze bitte hier die Möglichkeit, eine Frage an mich zu stellen!

Stelle Dominik eine Frage