# Two-way analysis of variance

In statistics, the two-way analysis of variance (ANOVA) is an extension of the one-way ANOVA that examines the influence of two different categorical independent variables on one continuous dependent variable. The two-way ANOVA not only aims at assessing the main effect of each independent variable but also if there is any interaction between them.

## History

In 1925, Ronald Fisher mentions the two-way ANOVA in his celebrated book, Statistical Methods for Research Workers (chapters 7 and 8). In 1934, Frank Yates published procedures for the unbalanced case. Since then, an extensive literature has been produced. The topic was reviewed in 1993 by Yasunori Fujikoshi. In 2005, Andrew Gelman proposed a different approach of ANOVA, viewed as a multilevel model.

## Data set

Let us imagine a data set for which a dependent variable may be influenced by two factors which are potential sources of variation. The first factor has $I$ levels ($i\in \{1,\ldots ,I\}$) and the second has $J$ levels ($j\in \{1,\ldots ,J\}$). Each combination $(i,j)$ defines a treatment, for a total of $I\times J$ treatments. We represent the number of replicates for treatment $(i,j)$ by $n_{ij}$, and let $k$ be the index of the replicate in this treatment ($k\in \{1,\ldots ,n_{ij}\}$).
From these data, we can build a contingency table, where $n_{i+}=\sum _{j=1}^{J}n_{ij}$ and $n_{+j}=\sum _{i=1}^{I}n_{ij}$, and the total number of replicates is equal to $n=\sum _{i,j}n_{ij}=\sum _{i}n_{i+}=\sum _{j}n_{+j}$.
The experimental design is balanced if each treatment has the same number of replicates, $K$. In such a case, the design is also said to be orthogonal, allowing to fully distinguish the effects of both factors. We hence can write $\forall i,j\;n_{ij}=K$, and $\forall i,j\;n_{ij}={\frac {n_{i+}\cdot n_{+j}}{n}}$.

## Model

Upon observing variation among all $n$ data points, for instance via a histogram, "probability may be used to describe such variation". Let us hence denote by $Y_{ijk}$ the random variable which observed value $y_{ijk}$ is the $k$-th measure for treatment $(i,j)$. The two-way ANOVA models all these variables as varying independently and normally around a mean, $\mu _{ij}$, with a constant variance, $\sigma ^{2}$ (homoscedasticity):
$Y_{ijk}\,|\,\mu _{ij},\sigma ^{2}\;{\overset {\mathrm {i.i.d.} }{\sim }}\;{\mathcal {N}}(\mu _{ij},\sigma ^{2})$.
Specifically, the mean of the response variable is modeled as a linear combination of the explanatory variables:
$\mu _{ij}=\mu +\alpha _{i}+\beta _{j}+\gamma _{ij}$,
where $\mu$ is the grand mean, $\alpha _{i}$ is the additive main effect of level $i$ from the first factor (i-th row in the contingency table), $\beta _{j}$ is the additive main effect of level $j$ from the second factor (j-th column in the contingency table) and $\gamma _{ij}$ is the non-additive interaction effect of treatment $(i,j)$ for samples $k=1,...,n_{ij}$ from both factors (cell at row i and column j in the contingency table).
Another equivalent way of describing the two-way ANOVA is by mentioning that, besides the variation explained by the factors, there remains some statistical noise. This amount of unexplained variation is handled via the introduction of one random variable per data point, $\epsilon _{ijk}$, called error. These $n$ random variables are seen as deviations from the means, and are assumed to be independent and normally distributed:
$Y_{ijk}=\mu _{ij}+\epsilon _{ijk}{\text{ with }}\epsilon _{ijk}{\overset {\mathrm {i.i.d.} }{\sim }}{\mathcal {N}}(0,\sigma ^{2})$.

## Assumptions

Following Gelman and Hill, the assumptions of the ANOVA, and more generally the general linear model, are, in decreasing order of importance:
* the data points are relevant with respect to the scientific question under investigation;
* the mean of the response variable is influenced additively (if not interaction term) and linearly by the factors;
* the errors are independent;
* the errors have the same variance;
* the errors are normally distributed.

## Parameter estimation

To ensure identifiability of parameters, we can add the following "sum-to-zero" constraints:
$\sum _{i}\alpha _{i}=\sum _{j}\beta _{j}=\sum _{i}\gamma _{ij}=\sum _{j}\gamma _{ij}=0$

## Hypothesis testing

In the classical approach, testing null hypotheses (that the factors have no effect) is achieved via their significance which requires calculating sums of squares.
Testing if the interaction term is significant can be difficult because of the potentially-large number of degrees of freedom.

## Example

The following hypothetical example gives the yields of 15 plants subject to two different environmental variations, and three different fertilisers.
| | Extra CO$_2$ | Extra humidity |
| No fertiliser | 7, 2, 1 | 7, 6 |
| Nitrate | 11, 6 | 10, 7, 3 |
| Phosphate | 5, 3, 4 | 11, 4 |
Five sums of squares are calculated:
| Factor | Calculation | Sum | N |
| Individual | $7^{2}+2^{2}+1^{2}+7^{2}+6^{2}+11^{2}+6^{2}+10^{2}+7^{2}+3^{2}+5^{2}+3^{2}+4^{2}+11^{2}+4^{2}$ | 641 | 15 |
| Fertilizer × Environment | ${\frac {(7+2+1)^{2}}{3}}+{\frac {(7+6)^{2}}{2}}+{\frac {(11+6)^{2}}{2}}+{\frac {(10+7+3)^{2}}{3}}+{\frac {(5+3+4)^{2}}{3}}+{\frac {(11+4)^{2}}{2}}$ | 556.1667 | 6 |
| Fertilizer | ${\frac {(7+2+1+7+6)^{2}}{5}}+{\frac {(11+6+10+7+3)^{2}}{5}}+{\frac {(5+3+4+11+4)^{2}}{5}}$ | 525.4 | 3 |
| Environment | ${\frac {(7+2+1+11+6+5+3+4)^{2}}{8}}+{\frac {(7+6+10+7+3+11+4)^{2}}{7}}$ | 519.2679 | 2 |
| Composite | ${\frac {(7+2+1+11+6+5+3+4+7+6+10+7+3+11+4)^{2}}{15}}$ | 504.6 | 1 |
Finally, the sums of squared deviations required for the analysis of variance can be calculated.
| Factor | Sum | N | Total | Environment | Fertiliser | Fertiliser × Environment | Residual |
| Individual | 641 | 15 | 1 | | | | 1 |
| Fertiliser × Environment | 556.1667 | 6 | | | | 1 | −1 |
| Fertiliser | 525.4 | 3 | | | 1 | −1 | |
| Environment | 519.2679 | 2 | | 1 | | −1 | |
| Composite (correction factor) | 504.6 | 1 | −1 | −1 | −1 | 1 | |
| Squared deviations ($\sigma ^{2}$) | | | 136.4 | 14.668 | 20.8 | 16.099 | 84.833 |
| Degrees of freedom | | | 14 | 1 | 2 | 2 | 9 |
| Mean square variance | | | | 14.668 | 10.4 | 8.0495 | 9.426 |