bug in lme4 package with anova()

Viewed 1637
library(Matrix)
library(lme4)


data <- lme4::sleepstudy

model1 <- lm(Reaction ~ Days, data = data)
model2 <- lmer(Reaction ~ 1+Days+(1+Days|Subject), data = data)
summary(model1)
summary(model2)
anova(model1, model2)

enter image description here enter image description here

So I needed to update R to 4.0.2 and now comparing mixed effects models using the anova function returns an error. The error seems to appear when I assign the model because in the global environment it says "object with null pointer". The error seems only to appear using lmer and not with lm. Can anybody tell me how to fix this? I need at least R version 4.0.0 (my professor writes scripts that require this version).

2 Answers

The accepted solution (explicitly call lme4::anovaLmer()) works, but the actual problem here is that if you want to run anova() comparing a(n) [g]lm model and a(n) [g]lmer model, you need to put the [g]lmer model first in the list of arguments, <because ... boring technical details: S3 method dispatch blah blah blah ...>. (This should be documented, and isn't that I can tell.) For your example,

anova(model2,model1)

works fine!

The "object with null pointer" error is a red herring: unrelated, mysterious, probably RStudio-related, see also this question and this question (neither is answered ...)

You could directly call the corresponding anova method from the lme4 package.

lme4:::anovaLmer(model1, model2)
# refitting model(s) with ML (instead of REML)
# Data: data
# Models:
# model1: Reaction ~ Days
# model2: Reaction ~ 1 + Days + (1 + Days | Subject)
#        npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)    
# model1    3 1906.3 1915.9 -950.15   1900.3                         
# model2    6 1763.9 1783.1 -875.97   1751.9 148.35  3  < 2.2e-16 ***
#   ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

However, this didn't work in e.g. R 3.6.3 either!

Related