In the story from the previous section, Beta encountered a result that a simpler analysis would have missed entirely. The effect of gender on MobilityScore was not significant when examined alone. The effect of age was significant. But neither effect told the complete story, only the interaction between them revealed the structure underneath. This is the central insight of two-way ANOVA: main effects can be misleading when an interaction is present, so we need methods to detect and model interactions.
The Model
In the case of one-way analysis of variance, we examined relationships between groups of objects defined by one categorical variable. Two-way analysis of variance allows exmining the relationship between a quantitative variable \(y\) and two categorical variables, which we’ll denote A and B in what follows. We can consider different models for such a relationship; below we’ll describe the most popular ones: additive main effects with and without interaction.
Assume that variable A can occur at \(k\) levels and variable B at \(r\) levels. The analysis of variance model is as follows:
\[
y_{ijm} \sim \mathcal{N}(\mu_{i,j}, \sigma^2), \quad 1 \leq i \leq k, \quad 1 \leq j \leq r, \quad 1 \leq m \leq n_{i,j}.
\tag{6.1}\]
where response variable \(y|A=i,B=j\) has a normal distribution with variance \(\sigma^2\) and mean \(\mu_{i,j}\), and \(n_{i,j}\) is the number of observations for which variable A takes value \(i\) and variable B takes value \(j\).
If \(n_{i,j}\) is the same in each group, we speak of a balanced design; otherwise, an unbalanced design.
Hypotheses posed in two-way analysis of variance concern the values of means \(\mu_{i,j}\), of which there are \(k r\). Let’s present them in a table together with marginal means.
\[
\begin{array}{llll|l}
\mu_{1,1} & \mu_{1,2} & \cdots & \mu_{1,r} & \mu_{1,.} \\
\mu_{2,1} & \mu_{2,2} & \cdots & \mu_{2,r} & \mu_{2,.} \\
\cdots & \cdots & \cdots & \cdots & \cdots \\
\mu_{k,1} & \mu_{k,2} & \cdots & \mu_{k,r} & \mu_{k,.} \\ \hline
\mu_{.,1} & \mu_{.,2} & \cdots & \mu_{.,r} & \mu \\
\end{array}
\tag{6.2}\]
If the design is balanced, that is, counts in each group are equal to each other, then marginal means are arithmetic means of means in corresponding groups:
\[
\mu_{i,.} = \sum_{j=1}^r \mu_{i,j} / r,
\]
and
\[
\mu_{.,j} = \sum_{i=1}^k \mu_{i,j} / k.
\]
In analysis of variance, different parameterizations are usually used in which means \(\mu_{i,j}\) are expressed by effects \(\mu\), \(\alpha_1\), \(\alpha_2\), …, \(\alpha_k\), \(\beta_1\), …, \(\beta_r\), \(\gamma_{1,1}\), …, \(\gamma_{k,r}\). These effects are called respectively: grand mean, main additive effects of the first variable, main additive effects of the second variable, and interaction effects (each group of effects is denoted by a different Greek letter).
Using these effects, model Equation 6.1 can be written as:
\[
y_{ijk} = \mu + \alpha_i + \beta_j + (\alpha\beta)_{ij} + \varepsilon_{ijk}
\tag{6.3}\]
where:
- \(\mu\) is the grand mean,
- \(\alpha_i\) is the main effect of factor \(A\) at level \(i\),
- \(\beta_j\) is the main effect of factor \(B\) at level \(j\),
- \((\alpha\beta)_{ij}\) is the interaction effect for pair of factors \(i,j\),
- \(\varepsilon_{ijk} \sim \mathcal{N}(0, \sigma^2)\) is the random noise for observation \(k\) in cell \((i,j)\).
The interaction term \((\alpha\beta)_{ij}\) captures what cannot be explained by the main effects alone, the degree to which the effect of factor \(A\) depends on the level of factor \(B\), and vice versa.
Note, that the number of parameters in the description Equation 6.3 (that is \((k+1)*(r+1)\)) is larger than number of observed values (that is \(k*r\) means), some constraints must be imposed on the new parameters to make them identifiable. A one possible choice is
\[
\sum_{i=1}^k \alpha_i = 0,\quad
\sum_{j=1}^r \beta_j = 0,\quad
\forall_i \quad \sum_{j=1}^r \gamma_{i,j} = 0,\quad
\forall_j \quad \sum_{i=1}^k \gamma_{i,j} = 0.
\]
\[\begin{equation}
\alpha_1=0, \quad \beta_1=0, \quad \forall_i \quad \gamma_{i,1}=0, \quad \forall_j \quad \gamma_{1,j}=0.
\end{equation}\]
The latter is used by default in many statistical packages so we will work with this one. With such constraints, the matrix of means Equation 6.2 can be presented in a new parameterization unambiguously.
By interaction, we mean the deviation of mean \(\mu_{i,j}\) from the additive influence of group \(i\) of the first variable and group \(j\) of the second variable. A model without interaction differs from a model with interaction in that in the case of a model without interaction, it’s assumed that all values \(\forall_{i,j} \gamma_{i,j}=0\). The occurrence of an interaction effect doesn’t mean the model isn’t linear.
Translating the above description into the language of linear models, we’ll consider a linear model of the form:
\[
y = X \beta + \varepsilon,
\]
in which vector \(\beta = (\mu, \alpha_2, \ldots, \alpha_k, \beta_2, \ldots, \beta_r, \gamma_{2,2}, \ldots, \gamma_{kr})\), and matrix \(X\) is a matrix of indicators of individual factors.
For example, assume we have a set of twelve observations and three variables: one quantitative \(y\) and two categorical \(x_1\) and \(x_2\). Assume that variable \(x_1\) occurs at two levels: C or D, and the number of occurrences of each level is balanced and equals six occurrences. In other words, the variable \(x_1=c(C, C, C, C, C, C, D, D, D, D, D, D)\) describes two six-element groups. Assume that variable \(x_2\) occurs at three equally frequent levels: Q, R, or S. In other words, the variable \(x_2=c(Q, Q, R, R, S, S, Q, Q, R, R, S, S)\) describes three four-element groups.
The formula y~x1+x2 describes a two-way analysis of variance model without interaction. The corresponding matrix notation of the model is as follows:
\[
\begin{bmatrix}
y_{1,1,1} \\
y_{1,1,2} \\
y_{1,2,1} \\
y_{1,2,2} \\
y_{1,3,1} \\
y_{1,3,2} \\
y_{2,1,1} \\
y_{2,1,2} \\
y_{2,2,1} \\
y_{2,2,2} \\
y_{2,3,1} \\
y_{2,3,2}
\end{bmatrix} =
\begin{bmatrix}
1 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 \\
1 & 0 & 1 & 0 \\
1 & 0 & 1 & 0 \\
1 & 0 & 0 & 1 \\
1 & 0 & 0 & 1 \\
1 & 1 & 0 & 0 \\
1 & 1 & 0 & 0 \\
1 & 1 & 1 & 0 \\
1 & 1 & 1 & 0 \\
1 & 1 & 0 & 1 \\
1 & 1 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
\mu\\
\alpha_2\\
\beta_2\\
\beta_3
\end{bmatrix} +
\begin{bmatrix}
\varepsilon_{1,1,1} \\
\varepsilon_{1,1,2} \\
\varepsilon_{1,2,1} \\
\varepsilon_{1,2,2} \\
\varepsilon_{1,3,1} \\
\varepsilon_{1,3,2} \\
\varepsilon_{2,1,1} \\
\varepsilon_{2,1,2} \\
\varepsilon_{2,2,1} \\
\varepsilon_{2,2,2} \\
\varepsilon_{2,3,1} \\
\varepsilon_{2,3,2}
\end{bmatrix}.
\tag{6.4}\]
The formula with interaction y~x1*x2 is equivalent to the formula y~x1+x2+x1:x2. Both describe a two-way analysis of variance model with interaction. The corresponding matrix notation is as follows:
\[
\begin{bmatrix}
y_{1,1,1} \\
y_{1,1,2} \\
y_{1,2,1} \\
y_{1,2,2} \\
y_{1,3,1} \\
y_{1,3,2} \\
y_{2,1,1} \\
y_{2,1,2} \\
y_{2,2,1} \\
y_{2,2,2} \\
y_{2,3,1} \\
y_{2,3,2}
\end{bmatrix} =
\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 1 & 0 & 0 & 0 \\
1 & 0 & 1 & 0 & 0 & 0 \\
1 & 0 & 0 & 1 & 0 & 0 \\
1 & 0 & 0 & 1 & 0 & 0 \\
1 & 1 & 0 & 0 & 0 & 0 \\
1 & 1 & 0 & 0 & 0 & 0 \\
1 & 1 & 1 & 0 & 1 & 0 \\
1 & 1 & 1 & 0 & 1 & 0 \\
1 & 1 & 0 & 1 & 0 & 1 \\
1 & 1 & 0 & 1 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
\mu\\
\alpha_2\\
\beta_2\\
\beta_3\\
\gamma_{2,2}\\
\gamma_{2,3}
\end{bmatrix} +
\begin{bmatrix}
\varepsilon_{1,1,1} \\
\varepsilon_{1,1,2} \\
\varepsilon_{1,2,1} \\
\varepsilon_{1,2,2} \\
\varepsilon_{1,3,1} \\
\varepsilon_{1,3,2} \\
\varepsilon_{2,1,1} \\
\varepsilon_{2,1,2} \\
\varepsilon_{2,2,1} \\
\varepsilon_{2,2,2} \\
\varepsilon_{2,3,1} \\
\varepsilon_{2,3,2}
\end{bmatrix},
\tag{6.5}\]
where \((y_{1,1,1}, y_{1,1,2}, \ldots, y_{2,3,1}, y_{2,3,2})\) is a twelve-element vector with measurements of trait \(y\), and \((\varepsilon_{1, 1,1}, \varepsilon_{1, 1,2}, \ldots, \varepsilon_{2,3,1}, \varepsilon_{2,3,2})\) is a twelve-element vector corresponding to random disturbance \(\varepsilon \sim \mathcal{N}(0, I_{12\times 12}\sigma^2)\).