Search code examples
ranova

Comparing models using Anova


Im trying to compare 6 models using the Anova function. mod1:4 are made using the lm function and mod 5 and 6 are models made using the lmer function. I think this is whats causing this error message, any ideas how i can compare these 6 models?

anova(mod1, mod2, mod3, mod4, mod5, mod6)
Error: $ operator not defined for this S4 class

Solution

  • anova() can work with a mixture lmer and lm models. However, due to the way that R's type system is set up, it only works if the first argument is an lmer model. That is, the anova.merMod() method (which gets called if the first argument is a [g]lmer model) knows how to deal with lm objects, but the anova.lm() method (which gets called if an lm object is first) doesn't know about merMod objects ...

    > library(lme4)
    Loading required package: Matrix
    > fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy, REML = FALSE)
    > fm2 <- lm(Reaction ~ Days, sleepstudy)
    > fm3 <- lm(Reaction ~ 1, sleepstudy)
    > anova(fm1, fm2, fm3)
    Data: sleepstudy
    Models:
    fm3: Reaction ~ 1
    fm2: Reaction ~ Days
    fm1: Reaction ~ Days + (1 | Subject)
        npar    AIC    BIC  logLik deviance   Chisq Df Pr(>Chisq)    
    fm3    2 1965.0 1971.4 -980.52   1961.0                          
    fm2    3 1906.3 1915.9 -950.15   1900.3  60.756  1  6.461e-15 ***
    fm1    4 1802.1 1814.8 -897.04   1794.1 106.214  1  < 2.2e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    > anova(fm3, fm2, fm1)
    Error: $ operator not defined for this S4 class