2025

Two-way ANOVA

  • Two-way ANOVA is used to determine if there are any differences between the means of at least two (but usually three or more) independent groups based on two categorical independent variables.

  • Like one-way ANOVA, it’s a test for equality of means that uses variances under the hood for inference.

  • All ANOVAs are omnibus tests. If the test is significant, we know that at least one group is different from the others, but NOT which groups are different.

Example

  • Study investigating the effect of sleep deprivation and calorie restriction on cognitive performance.

  • Independent Variable 1: Amount of sleep (categorical with three levels, such as “Full 8 hours,” “4 hours,” and “No sleep”).

  • Independent Variable 2: Amount of calorie restriction (categorical with two levels, such as “less than 1000 calories per day” and “more than 2000 calories per day”).

  • Dependent Variable: Performance on a cognitive task measured by scores on a memory test or reaction times (measurement needs to be continuous).

Two-way ANOVA Intuition

  • If between-group variation is much greater than within-group variation then different groups have different means.

  • Two-way ANOVA isn’t as simple as one-way ANOVA because there are many groups for observations to fall between or within.

  • The key is to see that a two-way ANOVA is actually testing three null hypotheses simultaneously.

Two-way ANOVA: Hypotheses

  1. Do different levels of factor 1 lead to different outcomes?

  2. Do different levels of factor 2 lead to different outcomes?

  3. Does the effect of factor 1 depend on the level of factor 2?

Factor A Hypotheses

  • Null Hypothesis (\(H_0\)): The means across all levels of factor A are equal:

    \[ \mu_{A1} = \mu_{A2} = \mu_{A3} = \ldots \]

  • Alternative Hypothesis (\(H_1\)): At least one level mean of factor A is different from the others.

Factor B Hypotheses

  • Null Hypothesis (\(H_0\)): The means across all levels of factor B are equal.

    \[ \mu_{B1} = \mu_{B2} = \mu_{B3} = \ldots \]

  • Alternative Hypothesis (\(H_1\)): At least one level mean of factor B is different from the others.

Interaction Hypotheses

  • The interaction test evaluates whether the effect of one factor depends on the level of the other factor.

  • Null Hypothesis (\(H_0\)): There is no interaction between Factor A and Factor B. That is, the effect of Factor A is the same at all levels of Factor B, and vice versa.

  • This means the mean response for each combination of A and B can be explained by adding together the main effects of A and B:

\[ \mu_{A_i, B_j} = \mu_{A_i} + \mu_{B_j} \quad \text{for all } i, j \]

  • Alternative Hypothesis (\(H_1\)): There is an interaction. The effect of Factor A changes depending on the level of Factor B.

Significant main effect of factor A

Significant main effect of factor B

Significant Interaction between A and B

Summary: Main effect and interaction

  • Significant main effects: Group by the factor of interest, average over the other factor, and look for differences in the resulting means.

  • Significant interaction: Group by both factors and look for non-parallel lines.

Two-way ANOVA using ezANOVA

##               y      A      B subject
##           <num> <fctr> <fctr>   <int>
##  1:  1.88694474      1      1       1
##  2:  1.36492952      1      1       2
##  3: -0.08115386      1      1       3
##  4:  4.24272238      1      1       4
##  5:  2.83496984      1      1       5
##  6:  5.21274486      1      1       6
##  7:  3.47051621      1      1       7
##  8: -2.51145118      1      1       8
##  9:  2.71839962      1      1       9
## 10:  3.68580586      1      1      10
## 11:  0.95457162      1      2      11
## 12: -0.30936741      1      2      12
## 13:  3.63590984      1      2      13
## 14:  2.09733016      1      2      14
## 15:  3.48437168      1      2      15
## 16:  2.38487794      1      2      16
## 17:  0.91114139      1      2      17
## 18:  4.58216888      1      2      18
## 19:  3.13008976      1      2      19
## 20:  0.71991074      1      2      20
## 21:  3.85068723      2      1      21
## 22:  5.32197092      2      1      22
## 23:  1.07069816      2      1      23
## 24:  4.08214433      2      1      24
## 25: -0.81685223      2      1      25
## 26:  5.06190552      2      1      26
## 27:  6.76930785      2      1      27
## 28:  1.53959702      2      1      28
## 29:  3.71358553      2      1      29
## 30:  3.65890348      2      1      30
## 31:  4.12194143      2      2      31
## 32:  0.28253539      2      2      32
## 33:  1.52652742      2      2      33
## 34: -1.45648308      2      2      34
## 35:  2.52146621      2      2      35
## 36: -0.13924052      2      2      36
## 37:  4.38310024      2      2      37
## 38: -0.87391797      2      2      38
## 39:  0.15971151      2      2      39
## 40:  1.24343709      2      2      40
##               y      A      B subject

dd[, A := factor(A)]
dd[, B := factor(B)]
dd[, subject := factor(subject)]
ezANOVA(data=dd, dv=y, wid=subject, between=.(A, B))
## $ANOVA
##   Effect DFn DFd          F         p p<.05          ges
## 1      A   1  36 0.01559755 0.9013053       0.0004330776
## 2      B   1  36 3.40313410 0.0733128       0.0863670919
## 3    A:B   1  36 2.73199993 0.1070523       0.0705359893
## 
## $`Levene's Test for Homogeneity of Variance`
##   DFn DFd       SSn      SSd         F         p p<.05
## 1   3  36 0.7209457 61.54506 0.1405693 0.9350293

Reporting a two-way ANOVA

  Effect DFn DFd           F            p p<.05          ges
1      A   1  36  0.39357521 0.5343852052       0.0108144147
2      B   1  36  0.03322773 0.8563818866       0.0009221413
3    A:B   1  36 16.15655987 0.0002847749     * 0.3097704279

Reporting a two-way ANOVA

A two-way analysis of variance was conducted to examine the effects of factors A and B, as well as their interaction (A:B), on the dependent variable. The analysis yielded a non-significant main effect of factor A, \(F(1, 36) = 0.394, p = .534\), with a very small effect size (\(\eta^2_p = 0.011\)). Similarly, the main effect of factor B was also non-significant, \(F(1, 36) = 0.033, p = .856\), with a negligible effect size (\(\eta^2_p = 0.001\)). However, the interaction effect between factors A and B was found to be statistically significant, \(F(1, 36) = 16.157, p < .001\), indicating a substantial effect (\(\eta^2_p = 0.310\)). This suggests that the effect of factor A on the dependent variable depends significantly on the level of factor B.

Assumptions: One-Way vs Two-Way ANOVA

Assumption One-Way ANOVA Two-Way ANOVA
Independence Observations are independent Same
Normality of residuals Equivalent to normality within each group Equivalent to normality within each cell
Homogeneity of variance Equal variances across groups Equal variances across all cells
Residual definition \(Y_{ij} - \bar{Y}_i\) \(Y_{ijk} - \text{Fitted}_{ij}\)
  • We will come back to define \(\text{Fitted}_{ij}\) in a later lecture.