With this very simple data:
> A
[1] "a" "a" "a" "a" "b" "b" "b" "b" "b" "b" "b" "b"
> B
[1] "x" "y" "x" "y" "x" "y" "x" "y" "x" "x" "x" "x"
> C
[1] "l" "l" "m" "m" "l" "l" "m" "m" "l" "l" "l" "l"
> response
[1] 14 30 15 35 50 51 30 32 51 55 53 55
I try to reproduce the Type-3 car::Anova by using step-by-step term elimination to better understand interactions and analysis of variance.
For example I want to assess the term "C"
> options(contrasts = c("contr.sum", "contr.poly"))
> m1 <- lm(response ~ A*B*C) # the full model
> car::Anova(m1, type=3, test.statistic = "LR")
Anova Table (Type III tests)
Response: response
Sum Sq Df F value Pr(>F)
(Intercept) 9374 1 1802.78 1.8e-06 ***
A 716 1 137.69 0.0003 ***
B 182 1 35.00 0.0041 **
C 178 1 34.23 0.0043 **
A:B 178 1 34.23 0.0043 **
A:C 317 1 61.03 0.0014 **
B:C 8 1 1.63 0.2714
A:B:C 0 1 0.00 0.9755
Residuals 21 4
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
For term "C" I got p-value = 0.0043 Now I going to assess term C by elimination:
> m2 <- lm(response ~ A + B + A:B) # the term "C", eliminated all terms with C
> anova(m1, m2)
Analysis of Variance Table
Model 1: response ~ A * B * C
Model 2: response ~ A + B + A:B
Res.Df RSS Df Sum of Sq F Pr(>F)
1 4 21
2 8 648 -4 -627 30.1 0.003 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Here p-value is = 0.003. Close but not the same. This is the simplest general linear model, no mixed effects, no transformations.
When I typed "test = LRT"
> anova(m1, m2, test="LRT")
Analysis of Variance Table
Model 1: response ~ A * B * C
Model 2: response ~ A + B + A:B
Res.Df RSS Df Sum of Sq Pr(>Chi)
1 4 21
2 8 648 -4 -627 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The result is totally different.
Please note, my goal IS NOT to make any formal inference, only to UNDERSTAND the calculations behind. I understand, that elimination terms from model should be equivalent to the car::Anova() and give equal result numerically.
Let's confirm:
> drop1(m1, scope = ~A*B*C, test="F")
Single term deletions
Model:
response ~ A * B * C
Df Sum of Sq RSS AIC F value Pr(>F)
<none> 21 22.6
A 1 716 737 63.4 137.69 0.0003 ***
B 1 182 203 47.9 35.00 0.0041 **
C 1 178 199 47.7 34.23 0.0043 **
A:B 1 178 199 47.7 34.23 0.0043 **
A:C 1 317 338 54.1 61.03 0.0014 **
B:C 1 8 29 24.7 1.63 0.2714
A:B:C 1 0 21 20.6 0.00 0.9755
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
It works! It agrees with car::Anova().
So how should I eliminate the terms to obtain the same result using anova()? Evidently comparing A+B+A:B vs. the full model is not enough!