Skip to content

Instantly share code, notes, and snippets.

@njudd
Last active October 28, 2019 05:48
Show Gist options
  • Save njudd/f7e8ef1c2a49731bd201bdf0bf17d3c4 to your computer and use it in GitHub Desktop.
Save njudd/f7e8ef1c2a49731bd201bdf0bf17d3c4 to your computer and use it in GitHub Desktop.
## Trying to figure out what R is doing with anova's
# The difference of aov() and lm() simply changes how summary() acts
# summary(aov()) is the same as anova(lm(), which are both TYPE 1!
# for type = III SS you need to use Anova in the library(car) package
# https://rcompanion.org/rcompanion/d_04.html
# Different SS primer
# https://stats.stackexchange.com/questions/20452/how-to-interpret-type-i-type-ii-and-type-iii-anova-and-manova/20455#20455
# http://www.utstat.utoronto.ca/reid/sta442f/2009/typeSS.pdf
# my attempt
df <- mtcars
df$cyl <- as.factor(df$cyl)
# first summary responding to the class of objects
mod_lm <- lm(mpg ~ disp + hp + cyl, data = df)
mod_lm_sw <- lm(mpg ~ hp + disp + cyl, data = df)
mod_aov <- aov(mpg ~ disp + hp + cyl, data = df)
mod_aov_sw <- aov(mpg ~ hp + disp + cyl, data = df)
summary(mod_lm)
summary(mod_lm_sw) # NOTHING CHANGES
summary(mod_aov)
summary(mod_aov_sw) # TYPE 1!!!!
# summary has different behaviour dependent on class of mod
# anova reacts the same... is it class III?
library(car)
anova(mod_lm)
anova(mod_lm_sw) # TYPE 1!!!
anova(mod_aov)
anova(mod_aov_sw) # TYPE 1??
# these 4 act the same way as summary(aov())
Anova(mod_lm, type = "III")
Anova(mod_lm_sw, type = "III")
Anova(mod_aov, type = "III")
Anova(mod_aov_sw, type = "III")
# acts the same regardless of mod type, this is type III SS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment