I'm trying to understand the reason why anova(f1, f2, f3) and anova(f1, f2) gives me different result while anova(f1, f2, f3) and anova(f2, f3) gives me the same.
Here's the code:
> data(swiss)
> fit1 <- lm(Fertility ~ Agriculture, data=swiss)
> fit3 <- lm(Fertility ~ Agriculture + Examination + Education, data=swiss)
> fit5 <- lm(Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality, data=swiss)
> class(fit1)
[1] "lm"
> class(fit3)
[1] "lm"
> class(fit5)
[1] "lm"
> anova(fit1, fit3, fit5)
Analysis of Variance Table
Model 1: Fertility ~ Agriculture
Model 2: Fertility ~ Agriculture + Examination + Education
Model 3: Fertility ~ Agriculture + Examination + Education + Catholic +
Infant.Mortality
Res.Df RSS Df Sum of Sq F Pr(>F)
1 45 6283.1
2 43 3180.9 2 3102.2 30.211 8.638e-09 ***
3 41 2105.0 2 1075.9 10.477 0.0002111 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> anova(fit1, fit3)
Analysis of Variance Table
Model 1: Fertility ~ Agriculture
Model 2: Fertility ~ Agriculture + Examination + Education
Res.Df RSS Df Sum of Sq F Pr(>F)
1 45 6283.1
2 43 3180.9 2 3102.2 20.968 4.407e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> anova(fit3, fit5)
Analysis of Variance Table
Model 1: Fertility ~ Agriculture + Examination + Education
Model 2: Fertility ~ Agriculture + Examination + Education + Catholic +
Infant.Mortality
Res.Df RSS Df Sum of Sq F Pr(>F)
1 43 3180.9
2 41 2105.0 2 1075.9 10.477 0.0002111 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
To me, this is read like this:
anova(fit1, fit3, fit5)shows me P value 8.638e-09 for the comparison of f1 and f3.anova(fit1, fit3)shows me 4.407e-7 for the same comparison. And this doesn't make sense!anova(fit1, fit3, fit5)andanova(fit3, fit5)shows the same P value for the comparison of fit3 and fit5 and it's 0.0002111.
Probably I missed something. What is it?
anova()function in R behaves differently depending on what type of object you give it. In other words, what arefit1,fit2, andfit3? What is the result ofclass(fit1)? – Zoë Clark Jul 19 '14 at 21:57anova: "Normally the F statistic is most appropriate, which compares the mean square for a row to the residual sum of squares for the largest model considered." In addition, see this post. – COOLSerdash Jul 20 '14 at 06:58anova(fit1, fit3, fit5)gives another $F$-value forfit3is thescaleargument inanova.lm. The help page states that "If zero this [scale] will be estimated from the largest model considered." So foranova(fit1, fit3, fit5), the scale is estimated to be $2105.043/41\approx 51.34$. Then, the $F$-value forfit3is calculated as: $(3102.19/2)/51.34\approx 30.211$. I don't want to put this as a full answer as I feel that I don't fully understand why this is done yet. Maybe someone else can shed more light on the rationale of this procedure. – COOLSerdash Jul 20 '14 at 09:21Clearly, ?anova shows me this "When given a sequence of objects, anova tests the models against one another in the order specified." And I believe anova.lm should behave in the same way.
– Minkoo Seo Jul 20 '14 at 10:00anovawhen used with models of the classlmisanova.lm.anovais just the generic function. As I have tried to explain: The main difference is in the estimate of the noise variance $\sigma^{2}$. If you include the modelfit5,anovauses that model to estimate the noise variance. On the other hand: if onlyfit1andfit3are included, thenfit3is used to estimate the noise variance, resulting in a different $F$ and $p$-value, respectively. – COOLSerdash Jul 20 '14 at 10:17