Skip to content

Instantly share code, notes, and snippets.

@Aariq
Created October 1, 2021 18:53
Show Gist options
  • Save Aariq/9d2e0b89b8632b0c4ab6286362f19a6c to your computer and use it in GitHub Desktop.
Save Aariq/9d2e0b89b8632b0c4ab6286362f19a6c to your computer and use it in GitHub Desktop.
don't use anova(), use car::Anova()
# anova() uses type I SS and unless you have only categorical predictors and
# balanced sample sizes, the order of the formula will change p-values,
# sometimes drastically!
m1 <- lm(Volume ~ Height + Girth, data = trees)
m2 <- lm(Volume ~ Girth + Height, data = trees)
anova(m1)
#> Analysis of Variance Table
#>
#> Response: Volume
#> Df Sum Sq Mean Sq F value Pr(>F)
#> Height 1 2901.2 2901.2 192.53 4.503e-14 ***
#> Girth 1 4783.0 4783.0 317.41 < 2.2e-16 ***
#> Residuals 28 421.9 15.1
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(m2)
#> Analysis of Variance Table
#>
#> Response: Volume
#> Df Sum Sq Mean Sq F value Pr(>F)
#> Girth 1 7581.8 7581.8 503.1503 < 2e-16 ***
#> Height 1 102.4 102.4 6.7943 0.01449 *
#> Residuals 28 421.9 15.1
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# This is almost never what you want. Just don't use anova()!
# Anova() from the car package, does type II SS by default—a marginal hypothesis
# test. The order of the formula doesn't matter.
library(car)
#> Loading required package: carData
Anova(m1)
#> Anova Table (Type II tests)
#>
#> Response: Volume
#> Sum Sq Df F value Pr(>F)
#> Height 102.4 1 6.7943 0.01449 *
#> Girth 4783.0 1 317.4129 < 2e-16 ***
#> Residuals 421.9 28
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(m2)
#> Anova Table (Type II tests)
#>
#> Response: Volume
#> Sum Sq Df F value Pr(>F)
#> Girth 4783.0 1 317.4129 < 2e-16 ***
#> Height 102.4 1 6.7943 0.01449 *
#> Residuals 421.9 28
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# This is almost always what you want (unless your model object doesn't have an
# Anova() method, in which case the anova() method *might* work correctly—check
# the help file!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment