Analysis of Variance - ANOVA, and of Covariance - ANCOVA

Overview

\(\newcommand{\VS}{\quad \mathrm{VS} \quad}\) \(\newcommand{\and}{\quad \mathrm{and} \quad}\) \(\newcommand{\E}{\mathbb E}\) \(\newcommand{\P}{\mathbb P}\) \(\newcommand{\Var}{\mathbb V}\) \(\newcommand{\Cov}{\mathrm{Cov}}\) \(\newcommand{\1}{\mathbf 1}\)

Terminology

  • ANOVA (Analysis of Variance): All explanatory variables \(X^{(j)}\) are qualitative
  • ANCOVA (Analysis of Covariance): Explanatory variables mix both quantitative and qualitative variables

We’ll see that these situations reduce to the case of the previous chapter.

One-Way ANOVA (One Factor)

One Hot Encoding

\(X\) is qualitative, with \(I\) modalities \(A_1, \dots, A_I\). We use one-hot encoding: \(X_{ki}=\1\{X_k=A_i\}\)

That is, for individual \(k\), \(X_{k\cdot} = (\1\{X_k=A_1\}, \dots, \1\{X_k=A_I\}) \in \{0,1\}^I\)

Or, using previous notations, \(X = (X^{(1)}, \dots, X^{(I)})\) where

\[X^{(i)}= \begin{pmatrix} X^{(i)}_1 \\ \vdots \\ X^{(i)}_n \end{pmatrix}\]

In Other Words

  • If there are \(3\) categories e.g. blue, orange, green, we replace the column \(X\) by \(3\) columns

  • Example with \(I=3\) categories and \(n=5\) individuals \[ \begin{pmatrix} \color{blue}{\mathrm{blue}} \\ \mathrm{green} \\ \color{blue}{\mathrm{blue}} \\ \mathrm{orange} \\ \mathrm{orange} \\ \end{pmatrix} \quad \text{becomes} \quad X=\begin{pmatrix} \color{blue}{\mathrm{1}} & \mathrm{0} & \mathrm{0}\\ \mathrm{0} & \mathrm{0} & \mathrm{1}\\ \color{blue}{\mathrm{1}} & \mathrm{0} & \mathrm{0}\\ \mathrm{0} & \mathrm{1} & \mathrm{0}\\ \mathrm{0} & \mathrm{1} & \mathrm{0}\\ \end{pmatrix} \]

Context, Notations

We seek to explain \(Y\) using a single qualitative variable \(X\).

We observe \((Y, X)\), and define

\[N_i = \sum_{k=1}^n \mathbf 1\{X_k=i\} \and \overline Y_i = \frac{1}{N_i}\sum_{k=1}^n Y_k \mathbf 1\{X_k=i\}\]

Total mean:

\(\overline Y = \frac{1}{n}\sum_{k=1}^n Y_k = \frac{1}{n}\sum_{i=1}^I N_i \overline Y_i\)

The One-Factor Model

For \(k = 1, \ldots, n\)

\[Y_k = \sum_{i=1}^I \mu_i \mathbf{1}\{X_k=A_i\} + \varepsilon_k\]

where \(\E[\varepsilon_k]=0\), \(\Cov(\varepsilon_k,\varepsilon_l)=\sigma^2\1\{k=l\}\)

  • \(Y_k\) is random and has expectation \(\mu_i\) if \(X_k=A_i\)
  • \(\Var(Y_k)=\sigma^2\) is the same regardless of modality \(A_i\)

Questions of Interest

  • Do we have \(\mu_1 = \mu_2 = \ldots = \mu_I\)? (Does factor \(A\) influence \(Y\)?)
  • Does some \(\mu_i\) have more influence on \(Y\)?
  • How do we estimate the \(\mu_i\)’s?

Model, Matrix Form

We rewrite the model \(Y_k = \sum_{i=1}^I \mu_i \mathbf{1}\{X_k=A_i\} + \varepsilon_k\) as

\[Y = X\mu + \varepsilon\]

  • Here, \(X_{k\cdot} = (\1\{X_k=A_1\}, \dots, \1\{X_k=A_I\}) \in \{0,1\}^I\)
  • There is no constant in this model

Model with Constant

If we want to model the constant (intercept) and \(I\) modalities, we assume

\[Y_k = m+\sum_{i=2}^I \alpha_i \mathbf{1}\{X_k=A_i\} + \varepsilon_k\]

Equivalently,

\(Y = m \mathbf{1} + \sum_{i=2}^I \alpha_i\mathbf{1}_{A_i}+\varepsilon\)

Collinearity Issues and Constraints

\(Y_k = m+\sum_{i=2}^I \alpha_i \mathbf{1}\{X_k=A_i\} + \varepsilon_k\)

\(\sum_{i=1}^I \mathbf{1}_{A_i} = \mathbf{1}\)

We have \(I+1\) parameters instead of \(I\). We have to set one constraint:

  • \(m=0\) (model without intercept). Here, \(\alpha_i= \mu_i\)
  • \(\alpha_1 =0\). In this case, we set \(\alpha_i = \mu_i-\mu_1\) (default in R).

In R

with intercept (default)

lm(Y~A) #with intercept

Interpretation: gives expectation \(\E[Y_k] = \mu_1\) and coefficients \(\alpha_i= \mu_i - \mu_1\)

 

without intercept

lm(Y~A-1) #without intercept

Interpretation: gives coefficients \(\alpha_i= \mu_i\)

The data: Loss ~ Exercise

\(Y\) = weight Loss, \(X\) = Exercise programme: \(I = 4\) modalities, \(N_i = 18\) each, \(n = 72\).

5 10 15 20 0 overall mean 12.22 1 2 3 4 exercise programme mean Loss 9.61 16.78 13.50 9.00

Programme 2 looks best, 1 and 4 almost tied.

In R: the intercept is a mean

Data of the example below: Loss ~ Exercise, \(I=4\) groups of \(N_i=18\).

tapply(Loss, Exercise, mean)     # the four empirical means
      1       2       3       4 
 9.6111 16.7778 13.5000  9.0000 

that is \(\overline{Y}_1 = 9.6111\), \(\overline{Y}_2 = 16.7778\), \(\overline{Y}_3 = 13.5\), \(\overline{Y}_4 = 9\).

summary(lm(Loss ~ Exercise))     # with intercept (default)
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   9.6111     0.7992  12.025  < 2e-16 ***
Exercise2     7.1667     1.1303   6.340 2.14e-08 ***
Exercise3     3.8889     1.1303   3.441 0.000996 ***
Exercise4    -0.6111     1.1303  -0.541   0.5905    

Residual standard error: 3.391 on 68 degrees of freedom

R prints no Exercise1: the column \(\mathbf{1}_{A_1}\) was dropped, and \(\hat m = \overline{Y}_1 = 9.6111\) is exactly the mean of that removed modality.

The others are differences to it: \(\hat\alpha_i = \overline{Y}_i - \overline{Y}_1\), e.g. \(16.7778 - 9.6111 = 7.1667\).

Without intercept: the means themselves

summary(lm(Loss ~ Exercise - 1))
          Estimate Std. Error t value Pr(>|t|)    
Exercise1   9.6111     0.7992  12.025  < 2e-16 ***
Exercise2  16.7778     0.7992  20.992  < 2e-16 ***
Exercise3  13.5000     0.7992  16.891  < 2e-16 ***
Exercise4   9.0000     0.7992  11.261  < 2e-16 ***

Residual standard error: 3.391 on 68 degrees of freedom

The four modalities are back, and \(\hat\alpha_i = \overline{Y}_i\) — the same four numbers as tapply.

Both fits are the same model: same \(\widehat{Y}_k = \overline{Y}_i\) for \(X_k = A_i\), same residuals, same \(\hat\sigma = 3.391\) on \(n-I=68\) degrees of freedom. Only the parameterization changes.

So Exercise2 = 7.1667 never means “group 2 has mean \(7.17\)”: it means group 2 sits \(7.17\) above group 1.

Estimation of μ

Whichever the model we choose (with constant or not), estimation of \(\mu_i=\E[Y_k|X_k=A_i]\) is the same. Same for \(\Var(\varepsilon_k)=\sigma^2\).

Proposition

In category \(i\), OLS estimation of \(\mu_i\) leads to:

  • \(\hat{\mu}_i = \frac{1}{N_i}\sum_{k=1}^n Y_k \1\{X_k=A_i\} = \overline{Y}_i\)

An unbiased estimator of \(\sigma^2\) is

  • \(\hat{\sigma}^2 = \frac{1}{n-I} \sum_{k=1}^{n}\sum_{i=1}^I (Y_{k} - \overline{Y}_i)^2\1\{X_k=A_i\}\)

Idea of Proof: Projections

We write \(\mathbf 1_i = (\1\{X_1=A_i\}, \dots, \1\{X_n=A_i\})^T\), and \(E = span(\mathbf 1_1, \dots, \mathbf 1_I)\)

Then, the OLS is exactly

\(\hat \mu = \mathrm{argmin}_{\mu' \in E}\|Y - \mu'\|_2^2 = P_E Y\)

For \(\hat \sigma\), \(\mathbb E[((I_n-P_E)\varepsilon)^T(I_n-P_E)\varepsilon]= \sigma^2 (n-I)\)

Testing for Factor Effect (ANOVA Test)

We want to test \(H_0: \mu_1 = \cdots = \mu_I\).

This is a linear constraints test! (See previous chapters)

Proposition

If \(\varepsilon \sim N(0, \sigma^2 I_n)\), then under \(H_0: \mu_1 = \cdots = \mu_I\):

\[F = \frac{SSB/(I-1)}{SSW/(n-I)} \sim F(I-1, n-I)\]

  • \(SSB = \sum_{i=1}^I N_i (\overline{Y}_i - \overline{Y})^2\)
  • \(SSW = \sum_{i=1}^I \sum_{k=1}^{n} (Y_{k} - \overline{Y}_i)^2\1\{X_k=A_i\}\)

Critical region at level \(\alpha\): \(CR_\alpha = \{F > f_{I-1,n-I}(1-\alpha)\}\)

How the F-test sees the data

One-way ANOVA (a single factor \(A\) with \(I\) groups). Pull the group means apart (separation) or add within-group noise. The partition \(SST = SSB + SSW\) shifts: \(F = \tfrac{SSB/(I-1)}{SSW/(n-I)}\) is large exactly when the between share dominates the within share.

separation 1.20   noise 0.80  
SSB (between)   SSW (within)  ·  F = –   p = –

Idea of proof

Using the previous projection \(P_E = \sum_{i=1}^I \frac{1}{N_i}\mathbf{1}_i\mathbf{1}_i^T\), we have the decomposition

\[Y - P_0Y = Y-P_E Y + P_E Y - P_0 Y\]

What the ANOVA Test Assumes

Warning

The previous analysis of variance test tests equality of means between modalities not equality of variances

It is valid under the assumption \(\varepsilon \sim N(0, \sigma^2 I_n)\).

  • Gaussian assumption: Not critical if \(n\) is large
  • Homoscedasticity: Important assumption

Homoscedasticity Tests

How to test equality of variances in each modality:

  • Levene test
  • Bartlett test

In R: leveneTest or bartlett.test from car library

Post-hoc Analysis: Multiple Testing Problem

If factor \(A\) is significant, we want to know more:
which modality(ies) differ from the others?

We want to perform all tests:

\[H_{0}^{i,j}: \mu_i = \mu_j \quad \text{vs} \quad H_{1}^{i,j}: \mu_i \neq \mu_j\]

for all \(i \neq j\) in \(\{1, \ldots, I\}\), corresponding to \(I(I-1)/2\) tests.

Multiple Testing: Naive Approach

Perform all Student’s t-tests for mean comparison (1 constraint), each at level \(\alpha\).

Problem: Given the number of tests, this would lead to many false positives.

False positives are a well-known problem in multiple testing.

Solution: Apply a correction to the decision rule, e.g.

  • Bonferroni correction
  • Benjamini-Hochberg correction

For one-way ANOVA: Tukey’s test addresses the problem.

Multiple Testing: Tukey’s Test

\[Q = \max_{(i,j)} \frac{|\overline{Y}_i - \overline{Y}_j|}{\hat{\sigma}\sqrt{\tfrac{1}{2}\left(\frac{1}{N_i} + \frac{1}{N_j}\right)}}\]

Distribution of Tukey’s Test Statistic

Under \(H_0: \mu_1 = \cdots = \mu_I\) and \(\varepsilon \sim N(0, \sigma^2 I_n)\), \[Q \sim Q_{I,n-I},\] the Tukey distribution with \((I, n-I)\) degrees of freedom — exactly if the \(N_i\) are equal, approximately otherwise.

Individual Tests

To test each \(H_0^{i,j}: \mu_i = \mu_j\), we use the critical regions:

\[CR_\alpha^{i,j} = \left\{|\overline{Y}_i - \overline{Y}_j| > \frac{\hat{\sigma}}{\sqrt{2}} \sqrt{\frac{1}{N_i} + \frac{1}{N_j}} \cdot Q_{I,n-I}(1-\alpha)\right\}\]

where \(Q_{I,n-I}(1-\alpha)\) denotes the \((1-\alpha)\) quantile of a tukey distribution \(Q(I,n-I)\) of degrees \((I, n-I)\).

Key Properties of Tukey’s Test

The form of the previous \(CR_\alpha^{i,j}\) ensures that: \[\mathbb{P}_{\mu_1 = \cdots = \mu_I}\left(\bigcup_{(i,j)} \{Y \in CR_\alpha^{i,j}\}\right) = \alpha\]

Interpretation: If all null hypotheses \(H_0^{i,j}\) are true (\(\mu_1 = \cdots = \mu_I\)), then the probability of concluding at least one \(H_1^{i,j}\) equals \(\alpha\).

This is the simultaneous Type I error rate.

Comparison with Student’s tests

  • Individual Student’s t-tests \(T^{i,j}\) at level \(\alpha\): only guarantee \(\mathbb{P}_{\mu_i = \mu_j}(T^{i,j}=1) = \alpha\)
  • When cumulated: \(\mathbb{P}_{\mu_1 = \cdots = \mu_I}\left[\cup_{(i,j)}\{T^{i,j}=1\}\right] \approx 1\) → false positives

Advantage

With Tukey’s test, two significantly different means are truly different, not just due to false positives.

In R: TukeyHSD

Why naive pairwise testing fails

All true means are equal, so every colour is a false positive: left, the FWER by Monte-Carlo (hover for the %); middle, the pairs flagged by the naive test (red) and by Tukey (green); right, this experiment’s means against the two thresholds.

data: \( Y_{g,k} = \mu + \varepsilon_{g,k},\ \ \varepsilon_{g,k}\sim\mathcal N(0,\sigma^2) \) — same \(\mu\) for every group (pure noise, no real differences)
groups I 6   α 0.05  
naive false positive   Tukey false positive  ·  this experiment: naive –, Tukey –  (true = 0)

Example of R output

Homoscedasticity Test:

leveneTest(Loss~Exercise)

We get

Levene’s Test for Homogeneity of Variance (center = median)
      Df   F value  Pr(>F)
group 3    0.6527   0.584
      68

Homoscedasticity is ok. ANOVA?

reg=lm(Loss~Exercise) # Exercise has 4 categories
anova(reg)

We get

Response: Loss
         Df Sum Sq Mean Sq F value    Pr(>F)    
Exercise   3 712.56  237.519  20.657 1.269e-09 ***
Residuals 68 781.89   11.498

Results Interpretation

The means are significantly different. We read in particular:

  • \(I - 1 = 3\), \(n - I = 68\)
  • \(SSB = 712.56\), \(SSW = 781.89\)
  • \(F = \frac{SSB/(I-1)}{SSW/(n-I)} = 20.657\)

R Example: Post-hoc Analysis

We finish by analyzing the mean differences more precisely:

TukeyHSD(aov(Loss~Exercise))
Exercise
     diff        lwr        upr     p adj
2-1  7.1666667   4.1897551 10.1435782 0.0000001
3-1  3.8888889   0.9119773  6.8658005 0.0053823
4-1 -0.6111111  -3.5880227  2.3658005 0.9487355
3-2 -3.2777778  -6.2546894 -0.3008662 0.0252761
4-2 -7.7777778 -10.7546894 -4.8008662 0.0000000
4-3 -4.5000000  -7.4769116 -1.5230884 0.0009537

Conclusion: All differences are significant, except between exercise 4 and exercise 1.

Two-Way ANOVA (Two Factors)

Two-Factor Setting

We observe \(Y\) and explanatory variables \(X^{(1)}, X^{(2)}\)

  • \(X^{(1)}_k \in \{A_1, \dots, A_I\}\) (\(I\) modalities)
  • \(X^{(2)}_k \in \{B_1, \dots, B_J\}\) (\(J\) modalities)
  • \(N_{ij} = \sum_{k=1}^n \1\{X^{(1)}_k \in A_i\}\1\{X_k^{(2)} \in B_j\}\) individuals in modality \(A_i\) and \(B_j\)

Model

\[\begin{aligned} Y_k &= m + \sum_{i=1}^I \alpha_i\1\{X^{(1)}_k = A_i\} + \sum_{j=1}^J \beta_j\1\{X^{(2)}_k = B_j\} \\ &+ \sum_{i=1}^I\sum_{j=1}^J \gamma_{ij}\1\{X^{(1)}_k = A_i\}\1\{X^{(2)}_k = B_j\} + \varepsilon_k \end{aligned}\]

In other words, in modality \(A_i\) and \(B_j\),

\(Y_k =m+\alpha_i + \beta_j + \gamma_{ij}+ \varepsilon_k\)

Interpretation

This is a model of the type \(Y_k = \mu_{ij} + \varepsilon_k\).

In modalities \((i,j)\), \(\E[Y_k] = \mu_{ij} = m + \alpha_i + \beta_j + \gamma_{ij}\)

  • \(m\): the average effect of \(Y\) (without considering \(A\) and \(B\))
  • \(\alpha_i = \mu_{i.} - m\): the marginal effect due to \(A\)
  • \(\beta_j = \mu_{.j} - m\): the marginal effect due to \(B\)
  • \(\gamma_{ij} = \mu_{ij} - m - \alpha_i - \beta_j\): the remaining effect, due to interaction between \(A\) and \(B\)

Example 1

\(Y\): employee satisfaction
\(A\): schedule type (flexible or fixed)
\(B\): training level (basic or advanced)

We can imagine:

  • Effect due to \(A\): satisfaction is greater with flexible schedules
  • Effect due to \(B\): satisfaction is higher with advanced training
  • No particular interaction between A and B

Example 2

\(Y\): plant yield
\(A\): fertilizer type (1 or 2)
\(B\): water quantity (low, medium, high)

We can imagine:

  • Effect due to \(A\): yield differs according to fertilizer used
  • Effect due to \(B\): yield is better when there is lots of water
  • Interaction: fertilizer 1 is better with water, and vice versa for fertilizer 2

Maybe interaction is so strong that the effect due to A seems absent!

Constraints on the Parameters

In modalities \((i,j)\), \(\E[Y_k] = \mu_{ij} = m + \alpha_i + \beta_j + \gamma_{ij}\)

Initial two-factor ANOVA problem: \(I \times J\) parameters \(\mu_{ij}\).

Now: \(1 + I + J + IJ\) parameters (\(m\), \(\alpha_i\), \(\beta_j\), \(\gamma_{ij}\)).

Therefore, we need \(1 + I + J\) constraints for identifiability:

\(\sum_{i=1}^{I} \alpha_i = 0 \and \sum_{j=1}^{J} \beta_j = 0\)

\(\sum_{i=1}^{I} \gamma_{ij} = 0 \and \sum_{j=1}^{J} \gamma_{ij} = 0\)

These constraints ensure model identifiability by removing the redundant parameters that cause multicollinearity issues.

Implementation in R

The complete model (with interaction) is launched with the command:

lm(Y ~ A + B + A:B) ## or equivalently, lm(Y ~ A*B)

With these constraints, the parameter interpretation is as follows:

Intercept: \(m = \mu_{11}\)
Main effect A: \(\alpha_i = \mu_{i1} - \mu_{11}\)
Main effect B: \(\beta_j = \mu_{1j} - \mu_{11}\)
Interaction: \(\gamma_{ij} = \mu_{ij} - \mu_{i1} - \mu_{1j} + \mu_{11}\)

Important Note on Interpretation

From

lm(Y ~ A + B + A:B) ## or equivalently, lm(Y ~ A*B)

Intercept: \(m = \mu_{11}\)
Main effect A: \(\alpha_i = \mu_{i1} - \mu_{11}\)
Main effect B: \(\beta_j = \mu_{1j} - \mu_{11}\)
Interaction: \(\gamma_{ij} = \mu_{ij} - \mu_{i1} - \mu_{1j} + \mu_{11}\)

Be Cautious on the Interpretation of the Coefficients

To impose the constraints from the previous approach (sum-to-zero constraints):

lm(Y ~ A*B, contrasts = list(A = contr.sum, B = contr.sum))

Estimation

The choice of constraints does not affect the estimation of the expectation of \(Y\) in each crossed modality \(A_i \cap B_j\).

Proposition

Whatever the linear constraints chosen, the OLS leads to, for all \(i = 1, \ldots, I\), \(j = 1, \ldots, J\) and \(k = 1, \ldots, N_{ij}\), if \(X^{(1)}_{k}=A_i\) and \(X_k^{(2)}=B_j\):

\[\widehat{Y}_{k} = \overline{Y}_{ij}:= \frac{1}{N_{ij}} \sum_{k=1}^{n} Y_{k}\1\{X_k^{(1)}=A_i \and X_k^{(2)}=B_j\}\]

and to the estimation of the residual variance:

\[\hat{\sigma}^2 = \frac{1}{n - IJ} \sum_{i=1}^I\sum_{j=1}^J\sum_{k=1}^n (Y_{k} - \overline{Y}_{ij})^2\1\{X_k^{(1)}=A_i \and X_k^{(2)}=B_j\}\]

Reading a crossed modality in R

summary(lm(yield ~ fertilizer * water))
                        Estimate Std. Error t value Pr(>|t|)    
(Intercept)               12.000     0.8573  13.997 1.09e-14 ***
fertilizer2                6.000     1.2124   4.949 2.69e-05 ***
watermedium                4.000     1.2124   3.299  0.00250 ** 
waterhigh                 10.000     1.2124   8.248 3.31e-09 ***
fertilizer2:watermedium   -3.000     1.7146  -1.750  0.09041 .  
fertilizer2:waterhigh    -11.000     1.7146  -6.415 4.39e-07 ***

Residual standard error: 2.1 on 30 degrees of freedom

Everything is read against the cell \((A_1, B_1)\): \(\hat m = \overline{Y}_{11} = 12\), \(\hat\alpha_2 = \overline{Y}_{21} - \overline{Y}_{11} = 6\), \(\hat\beta_3 = \overline{Y}_{13} - \overline{Y}_{11} = 10\), \(\hat\gamma_{23} = \overline{Y}_{23} - \overline{Y}_{21} - \overline{Y}_{13} + \overline{Y}_{11} = -11\).

Summing them back gives the crossed modality:

\(\hat\mu_{23} = \hat m + \hat\alpha_2 + \hat\beta_3 + \hat\gamma_{23} = 12 + 6 + 10 - 11 = 17 = \overline{Y}_{23}\)

The same fit with sum-to-zero constraints

summary(lm(yield ~ fertilizer*water,
           contrasts = list(fertilizer = contr.sum, water = contr.sum)))
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)         17.3333     0.3500  49.524  < 2e-16 ***
fertilizer1         -0.6667     0.3500  -1.905  0.06644 .  
water1              -2.3333     0.4950  -4.714 5.22e-05 ***
water2               0.1667     0.4950   0.337  0.73870    
fertilizer1:water1  -2.3333     0.4950  -4.714 5.22e-05 ***
fertilizer1:water2  -0.8333     0.4950  -1.684  0.10260    

\(\hat m = 17.33\) is now the grand mean; the last level of each factor is not printed, being minus the sum of the others: \(\hat\alpha_2 = +0.6667\), \(\hat\beta_3 = +2.1667\), \(\hat\gamma_{23} = -3.1667\). Same fit, same \(\hat\sigma = 2.1\):

\(\hat\mu_{23} = 17.3333 + 0.6667 + 2.1667 - 3.1667 = 17 = \overline{Y}_{23}\)

Interaction 1: pure synergy

Yield (q/ha) in hydroponics, \(E\) = fertiliser, \(W\) = water, \(40\) per cell. Truth: \(Y = 50\,EW + \varepsilon\).

summary(lm(yield ~ fert * water))
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    -0.23      0.791  -0.291  0.77149    
fert            0.21      1.118   0.188  0.85125    
water           0.45      1.118   0.402  0.68787    
fert:water     49.83      1.581  31.515  < 2e-16 ***

Residual standard error: 5 on 156 degrees of freedom
\(\overline{Y}_{ij}\) no water water
no fertiliser −0.23 0.22
fertiliser −0.02 50.26

The two factors work better together than the sum of their separate effects.

Interaction 2: substitutes

Loneliness score (0–40), \(C\) = dog, \(H\) = cat, \(60\) per cell. Truth: \(Y = 40 - 20\,C - 20\,H + 20\,CH + \varepsilon\), i.e. \(Y = 40 - 20\max(C,H)\).

summary(lm(score ~ dog * cat))
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    39.70      1.033  38.439  < 2e-16 ***
dog           -19.73      1.461 -13.508  < 2e-16 ***
cat           -19.41      1.461 -13.289  < 2e-16 ***
dog:cat        19.78      2.066   9.576  < 2e-16 ***

Residual standard error: 8 on 236 degrees of freedom
\(\overline{Y}_{ij}\) no cat cat
no dog 39.70 20.29
dog 19.97 20.34

The two factors do the same job, so having one makes the other less useful.

Interaction 3: crossover

Survival (months), \(T\) = treatment, \(G\) = variant carrier, \(50\) per cell. Truth: \(Y = 24 - 12\,T - 12\,G + 24\,TG + \varepsilon\).

summary(lm(surv ~ treat * variant))
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)      23.75      0.849  27.990  < 2e-16 ***
treat           -11.77      1.200  -9.808  < 2e-16 ***
variant         -11.51      1.200  -9.592  < 2e-16 ***
treat:variant    23.81      1.697  14.030  < 2e-16 ***

Residual standard error: 6 on 196 degrees of freedom
\(\overline{Y}_{ij}\) non-carrier carrier
placebo 23.75 12.24
treatment 11.98 24.28

Crossover: the effect of one factor changes direction depending on the other.

Interaction plot — drag the cell means

Drag a cell mean (solid \(\mu_{ij}\), dashed \(\hat Y_{ij}\) with noise); i / j select the active cell, constraints switches the coding. The table holds the whole decomposition, each \(\gamma_{ij}\) green at \(0\). In R: interaction.plot(B, A, Y).

noise σ 2.00   n/cell 6  
cell  i   j  ·  constraints
– = – + – + – + –
\(\gamma_{ij}\) \(j=1\) \(j=2\) \(j=3\) \(\alpha_i\)
\(i=1\) – – – –
\(i=2\) – – – –
\(\beta_j\) – – – \(m\) –

Significance Tests for Effects

  • Is the effect due to the interaction between A and B significant?
  • Is the marginal effect due to A significant?
  • Is the marginal effect due to B significant?

We test for interaction first. Under \(H_0^{(AB)}\), the model reduces to the additive model \(Y_{ijk} = m + \alpha_i + \beta_j + \varepsilon_{ijk}\)


Do we have \(\gamma_{ij} = 0\) for all \(i, j\)?

Testing Strategy: Interaction First

We want to test for the presence of an interaction:

\[H_0^{(AB)}: \gamma_{ij} = 0 \text{ for all } i, j\]

If we conclude \(H_0^{(AB)}\) (accept the null hypothesis), we then want to test the marginal effects:

\[H_0^{(A)}: \alpha_i = 0 \text{ for all } i \and H_0^{(B)}: \beta_j = 0 \text{ for all } j\]

When the Interaction Is Significant

\[H_0^{(AB)}: \gamma_{ij} = 0 \text{ for all } i, j\]

Warning

If we reject \(H_0^{(AB)}\), it makes no sense to test whether A or B have an effect: they have one through their interaction.

These tests reduce to constraint tests in the regression model.

Balanced Design Analysis of Variance

We assume that “the design is balanced”: this means that \(N_{ij}:=N\) does not depend on \(i\) or \(j\). (Otherwise, everything becomes complicated).

In this case, we have the analysis of variance formula:

ANOVA Formula

\[S_T^2 = S_A^2 + S_B^2 + S_{AB}^2 + S_R^2\]

  • \(S_T^2 = \sum_{k=1}^{n} (Y_{k} - \overline{Y})^2\): total sum of squares

  • \(S_A^2 = \sum_{i=1}^{I} \sum_{j=1}^{J} N_{ij} (\overline{Y}_{i.} - \overline{Y})^2\): \(S^2_{between}\) in the case of one-factor ANOVA where the factor is \(A\)

  • \(S_B^2 = \sum_{i=1}^{I} \sum_{j=1}^{J} N_{ij} (\overline{Y}_{.j} - \overline{Y})^2\): \(S^2_{between}\) in the case of one-factor ANOVA where the factor is \(B\)

  • \(S_{AB}^2 = \sum_{i=1}^{I} \sum_{j=1}^{J} N_{ij} (\overline{Y}_{ij} - \overline{Y}_{i.} - \overline{Y}_{.j} + \overline{Y})^2\) quantifies the interaction

  • \(S_R^2 = \sum_{i=1}^{I} \sum_{j=1}^{J} \sum_{k=1}^{N_{ij}} (Y_{k} - \overline{Y}_{ij})^2\1\{X^{(1)}_k=A_i \and X^{(2)}_k=B_j\}\): \(S_{within}\) in one-factor ANOVA

Testing the Interaction: Constraint Test

For testing the interaction, \(H_0^{(AB)}: \gamma_{ij} = 0\) for all \(i, j\)

We use the linear constraint test statistic \(F = \frac{n-p}{q} \frac{(SSR_c - SSR)}{SSR}\)

where \(SSR_c\) corresponds to the sum of squares of the residuals in the space \(\gamma_{ij}=0\) for all \((i,j)\).

Testing the Interaction: F Statistic

\[F^{(AB)} = \frac{S_{AB}^2/\big((I-1)(J-1)\big)}{S_R^2/(n-IJ)}\]

When \(\varepsilon \sim \mathcal N(0, \sigma^2 I_n)\), \(F^{(AB)} \sim \mathcal F((I-1)(J-1), n-IJ)\)

Testing the Effect of A

For testing the main effect of \(A\), \(H_0^{(A)}: \alpha_i = 0\) for all \(i\):

We use \(F = \frac{n-p}{q} \frac{(SSR_c - SSR)}{SSR}\)

where \(SSR_c\) corresponds to the sum of squares of the residuals in the space \(\alpha_i=0\) for all \(i\).

\[F^{(A)} = \frac{S_A^2/(I-1)}{S_R^2/(n-IJ)}\]

When \(\varepsilon \sim \mathcal{N}(0, \sigma^2 I_n)\), \(F^{(A)} \sim \mathcal{F}(I-1, n-IJ)\)

Testing the Effect of B

For testing the main effect of \(B\), \(H_0^{(B)}: \beta_j = 0\) for all \(j\):

We use \(F = \frac{n-p}{q} \frac{(SSR_c - SSR)}{SSR}\)

where \(SSR_c\) corresponds to the sum of squares of the residuals in the space \(\beta_j=0\) for all \(j\).

\[F^{(B)} = \frac{S_B^2/(J-1)}{S_R^2/(n-IJ)}\]

When \(\varepsilon \sim \mathcal{N}(0, \sigma^2 I_n)\), \(F^{(B)} \sim \mathcal{F}(J-1, n-IJ)\)

R outputs

In software, these tests are summarized in a table as shown below.

In R: anova(lm(Y ~ A*B))

Source df Sum Sq Mean Sq F value Pr(>F)
\(A\) \(I-1\) \(S_A^2\) \(S_A^2/(I-1)\) \(F^{(A)}\) …
\(B\) \(J-1\) \(S_B^2\) \(S_B^2/(J-1)\) \(F^{(B)}\) …
\(A:B\) \((I-1)(J-1)\) \(S_{AB}^2\) \(S_{AB}^2/((I-1)(J-1))\) \(F^{(AB)}\) …
Residuals \(n-IJ\) \(S_R^2\) \(S_R^2/(n-IJ)\)

Two-Way ANOVA in Practice

Assumptions

Fisher tests are based on the assumption \(\varepsilon \sim N(0, \sigma^2 I_n)\)

  • Normality is not critical, but homoscedasticity is
  • We can test equality of variances in each modality of A (or B), or in each crossed modality if the \(n_{ij}\) are sufficiently large
  • This can be done with Levene’s test or Bartlett’s test

Practical Procedure

  1. Test equality of variances

  2. Form the ANOVA table (independent of chosen constraints)

  3. If the \(AB\) interaction is significant: don’t change anything

  4. If the interaction is not significant: analyze the marginal effects of A and B

  • If they are significant: the model is additive: lm(Y ~ A + B)
  • Otherwise: we can remove \(A\) (or \(B\)) from the model

Post-Hoc Analysis

Once effects are identified: perform post-hoc analysis by examining differences between (crossed) modalities more closely, using Tukey’s test as in one-factor ANOVA

Beyond Two Factors

Principle and Limits

We seek to explain \(Y\) using \(k\) qualitative variables \(A=X^{(1)}\), \(B=X^{(2)}\), \(C=X^{(3)}\), …

We can assume that \(\E[Y_k]\) depends on each factor and their interactions:

  • Two-way interactions: \(AB\), \(BC\), \(AC\), etc.
  • Three-way interactions: \(ABC\), etc.
  • Higher-order interactions: and potentially more

Warning

This results in \(2^k - 1\) possible effects for \(k\) factors.

Testing Approach

Each effect can be tested using linear constraint tests. However, this approach presents several challenges:

  • Multiple testing burden: Too many tests to perform
  • Sample size limitations: Risk of insufficient sample sizes in each crossed modality

In practice, choices are made to include only a limited number of effects and interactions in the analysis.

Three factors: start additive

Run time (min) of a job: algo (new vs old), ssd (SSD vs HDD), ram (32 vs 8 GB), \(25\) per cell.

summary(lm(time ~ algo + ssd + ram))
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    57.76      0.776  74.473  < 2e-16 ***
algo           -5.79      0.776  -7.461 2.72e-12 ***
ssd            -8.06      0.776 -10.397  < 2e-16 ***
ram            -5.94      0.776  -7.665 8.09e-13 ***

Residual standard error: 5.484 on 196 degrees of freedom

Three switches, three savings, read anywhere: \(-5.8\) min for the new algorithm, \(-8.1\) for the SSD, \(-5.9\) for the RAM.

Three factors: adding one interaction

summary(lm(time ~ algo * ssd + ram))
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    59.72      0.810  73.715  < 2e-16 ***
algo           -9.72      1.025  -9.486  < 2e-16 ***
ssd           -12.00      1.025 -11.708  < 2e-16 ***
ram            -5.94      0.725  -8.203 3.11e-14 ***
algo:ssd        7.87      1.449   5.430 1.66e-07 ***

Residual standard error: 5.124 on 195 degrees of freedom

The new algorithm saves \(9.7\) min on a HDD but only \(9.72 - 7.87 \approx 1.9\) on an SSD: it was mostly avoiding disk reads. The RAM keeps its own \(-5.9\) whatever the rest, so it stays additive.

Analysis of Covariance (ANCOVA)

Model Components

Most general situation: we seek to explain Y using both quantitative and qualitative variables.

Quantitative variable effects: Through each regression coefficient \(\beta_j\) associated with each variable

Factor effects and interactions: As in ANOVA analysis

  • Main effects of factors (quali. var.)
  • Interactions between factors

Mixed interactions: Effects of interactions between factors and quantitative variables

Example of Mixed Interaction

  • \(A \in \{\text{Labrador}, \text{Chihuahua}\}\)
  • \(X^{(1)}\): daily walking distance (km), over the same range for both breeds
  • \(Y\): daily food needed (g)

\[Y = \beta_0 + \beta_1 \1\{A = \text{Lab}\} + \beta_2 X^{(1)} + \beta_3 \1\{A = \text{Lab}\}X^{(1)} + \varepsilon\]

\(\beta_3\): one extra km costs a Labrador more grams than a Chihuahua — the two slopes differ.

Statistical Testing

  • Each effect can be tested using a Fisher test.

  • Obviously, choices must be made regarding which effects and interactions to include in the model.

ANCOVA in R: Quantitative & Qualitative Variables

Model Without Interaction

\(Y\): quantitative response variable

\(X\): quantitative variable,

\(Z\): factor with \(I\) modalities \(\{A_1, \dots, A_I\}\)

lm(Y~X+Z) estimates the model without interaction, where, for each individual \(k = 1, \ldots, n\):

\[Y_k = m + \beta X_k + \sum_{i=2}^{I} \alpha_i \mathbf{1}\{Z_k=A_i\} + \varepsilon_k\]

Constraint \(\alpha_1 = 0\) is adopted to make the model identifiable.

Model With Interaction

lm(Y~X+A+X:A) or lm(Y~X*A) estimates the model with interaction:

\[ \begin{aligned} Y_k &= m + \beta X_k + \sum_{i=2}^{I} \beta_i X_k \mathbf{1}\{Z_k=A_i\} \\ &+ \sum_{i=2}^{I} \alpha_i \mathbf{1}\{Z_k=A_i\} + \varepsilon_k \end{aligned} \]

Key Insight: In the interaction model, the coefficient \(\beta\) associated with \(X\) varies according to the modality \(A_i\).

Two factors and a covariate: additive

Daily food (g) of \(n = 200\) dogs: breed (lab / chihuahua), ster (sterilised yes / no), km walked per day, \(0\) to \(10\) for both breeds.

summary(lm(food ~ breed + ster + km))
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    88.31      8.364  10.559  < 2e-16 ***
breedlab      486.40      6.703  72.571  < 2e-16 ***
steryes       -59.29      6.766  -8.763 9.02e-16 ***
km             24.99      1.170  21.364  < 2e-16 ***

Residual standard error: 47.35 on 196 degrees of freedom

One slope for every dog: \(+25\) g per km, whatever the breed.

Two factors and a covariate: mixed interaction

summary(lm(food ~ breed * km + ster))
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   148.88      4.840  30.763  < 2e-16 ***
breedlab      348.67      6.544  53.280  < 2e-16 ***
km             12.16      0.783  15.528  < 2e-16 ***
steryes       -58.92      3.364 -17.514  < 2e-16 ***
breedlab:km    28.26      1.156  24.455  < 2e-16 ***

Residual standard error: 23.54 on 195 degrees of freedom

Two slopes now: \(12.2\) g per km for a chihuahua, \(12.16 + 28.26 = 40.4\) for a labrador. Sterilisation stays a flat \(-59\) g, and \(\hat\sigma\) falls from \(47\) to \(24\).

Next

Introduction to the Generalized Linear Model