Two-way ANOVA

Two-way analysis of variance (two-way ANOVA) is an extension of the one-way ANOVA to examine the influence of two different categorical independent variables on one continuous dependent variable. The two-way ANOVA can evaluate not only the main effect of each independent variable but also the potential interaction between them. For example, for the ACTIVE data, we can test whether the four training groups and gender are related to the memory test performance.

Type of effects and hypotheses

In two-way ANOVA with two factors (independent variables) A and B, we can test two main effects and one interaction effect:

  • Main effect of A: whether the population means are the same for different levels (groups) of A? 
    • The null hypothesis: the population means are the same for different levels of A.
  • Main effect of A: whether the population means are the same for different levels (groups) of A?
    • The null hypothesis: the population means are the same for different levels of A.
  • The interaction effect of A and B: whether the difference in the population means for difference level of A depends on the level of B or vice visa?
    • The null hypothesis: the effect of A or B on the outcome does not depend on B or A.

Two-way ANOVA F tests

Like in one-way ANOVA, F test is used in hypothesis testing. The test is constructed based on the decomposition of the sum of squares of the outcome variables. Specifically, we have 

\[ SS_{T}=SS_{A}+SS_{B}+SS_{A \times B}+SS_{W} \]

  • $SS_T$ or $SS_{total}$ is the sum of squares for the outcome variable.
  • $SS_A$ is the sum of squares attributes to the factor A.
  • $SS_B$ is the sum of squares attributes to the factor B.
  • $SS_{A\times B}$ is the sum of squares attributes to the joint effect of A and B.
  • $SS_{W}$ or $SS_{within}$ is the residual sum of squares that cannot be explain by A, B or their interaction.

Let $N$ be the sample size, $J$ is the number of levels in factor A, and $K$ is the number of levels in factor B. With the information, we can construct a source of variance table below:

Source Sum of squares Degree of freedom Mean square F statistic
Factor A $SS_A$ $J-1$ $MS_A=\frac{SS_A}{J-1}$ $F_A = \frac{MS_A}{MS_W}$
Factor B $SS_B$ $K-1$ $MS_B=\frac{SS_B}{K-1}$ $F_B = \frac{MS_B}{MS_W}$
Interaction A*B $SS_{A\times B}$ $(J-1)*(K-1)$ $MS_{A\times B}=\frac{SS_{A\times B}}{(J-1)\times (K-1)}$ $F_{A\times B} = \frac{MS_{A\times B}}{MS_W}$
Within $SS_W$ $N-(J\times K)$ $MS_W = \frac{SS_W}{N-(J\times K)}$  
Total $SS_T$ $N-1$    

It is easy to see that $SS_T = SS_A + SS_B + SS_{A\times B} + SS_W$. For testing the effect of Factor A, we compare the statistic $F_A$ to an F distribution with degrees of freedom $J-1$ and $N-(J\times K)$. For testing the effect of Factor B, we compare the statistic $F_B$ to an F distribution with degrees of freedom $K-1$ and $N-(J\times K)$. To test the interaction effect of Factor A and Factor B, we compare the statistic $F_{A\times B}$ to an F distribution with degrees of freedom $(J-1)*(K-1)$ and $N-(J\times K)$.

Example: Effects of training and gender on the memory test performance

As an example, we test the effect of training and gender on the memory test performance using the ACTIVE data. We want to answer the following questions:

  1. At the population level, are there any difference in memory test performance for the 4 training groups?
  2. At the population level, are there any difference in memory test performance for male and female participants?
  3. Do the effects of training on the memory test performance differ across the two gender groups?

The R code below carries out the two-way ANOVA to answer the above questions.

> usedata("active")
> model<-lm(hvltt2~factor(group)*factor(sex), data=active)
> anova(model)
Analysis of Variance Table

Response: hvltt2
                            Df Sum Sq Mean Sq F value    Pr(>F)    
factor(group)                3    859  286.20 10.2434 1.114e-06 ***
factor(sex)                  1    919  919.28 32.9013 1.161e-08 ***
factor(group):factor(sex)    3     34   11.46  0.4102    0.7457    
Residuals                 1567  43783   27.94                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
> 

Main effect of training group

From the output, the F-statistic for testing the main effect of training group is 10.2434. Based on the F distribution with degrees of freedom 3 and 1567, the corresponding p-value is 1.114e-06, which indicates a significant main effect for training group. Therefore, one may conclude there is significant difference in memory performance among the four groups.

Main effect of sex

From the output, the F-statistic for testing the main effect of sex is 32.9013. Based on the F distribution with degrees of freedom 1 and 1567, the corresponding p-value is 1.161e-08, which indicates a significant main effect sex. Therefore, one may conclude there is significant difference in memory performance between the male and female participants.

Interaction effect between training group and sex

From the output, the F-statistic for testing the interaction effect [factor(group):factor(sex)] is 0.41. Based on the F distribution with degrees of freedom 3 and 1567, the corresponding p-value is 0.7457, which indicates an insignificant interaction effect. Therefore, one may conclude the difference in memory performance among the four training group does not depend on sex.

Plot data

As in one-way ANOVA, we can also plot data with two factors with error bars. The code below can be used.

> library(Rmisc)
Loading required package: lattice
Loading required package: plyr
> library(ggplot2)
> 
> usedata('active')
> stats <- summarySE(active, measurevar="hvltt2", groupvars=c("group","sex"))
> stats
  group sex   N   hvltt2       sd        se        ci
1     1   1  85 24.52941 5.412922 0.5871138 1.1675401
2     1   2 307 26.53746 5.184920 0.2959190 0.5822937
3     2   1  92 23.34783 5.453973 0.5686160 1.1294858
4     2   2 295 24.85763 5.219019 0.3038631 0.5980225
5     3   1  84 23.78571 5.477383 0.5976314 1.1886649
6     3   2 323 25.17957 4.986810 0.2774735 0.5458900
7     4   1 112 22.54464 5.950075 0.5622293 1.1140948
8     4   2 277 24.77256 5.367870 0.3225240 0.6349197
> 
> ggplot(stats, 
+    aes(x = factor(group), y = hvltt2, fill = factor(sex), 
+        ymax=hvltt2+se, ymin=hvltt2-se))  +
+        geom_bar(stat="identity", position = "dodge", width = 0.7) +
+        geom_bar(stat="identity", position = "dodge", 
+                 colour = "black", width = 0.7)  +
+        geom_errorbar(position=position_dodge(width=0.7), 
+                      width=0.2, size=0.5, color="black")  +
+        labs(x = "Group",
+             y = "Memory performance")  +
+       scale_x_discrete(breaks=c("1", "2", "3", "4"),
+         labels=c("Memory", "Reasoning", "Speed", "Control"))
> 

To cite the book, use: Zhang, Z. & Wang, L. (2017-2022). Advanced statistics using R. Granger, IN: ISDSA Press. https://doi.org/10.35566/advstats. ISBN: 978-1-946728-01-2.
To take the full advantage of the book such as running analysis within your web browser, please subscribe.