Skip to content

Bug in anova.mer with models in a list (CRAN lme4 and lme4.0 only)

1 message · Henrik Singmann

#
Hi all,

there seems to be a bug in the anova method for mer objects with two models when both models are inside a list:

require(lme4)

# from ?lmer
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
fm2 <- lmer(Reaction ~ Days + (1|Subject) + (0+Days|Subject), sleepstudy)

# both models inside a list do not work:
anova(list(fm1)[[1]], list(fm2)[[1]])

## Error in names(mods) <- sapply(as.list(mCall)[c(FALSE, TRUE, modp)], as.character) :
##   'names' attribute [6] must be the same length as the vector [2]

# no list or one list works:
anova(fm1, fm2)
anova(list(fm1)[[1]], fm2)
anova(fm1, list(fm2)[[1]])

This bug most likely resides in the sapply statement on lines 1029 and 1030 in lmer.R (CRAN tar.gz of lme4):
names(mods) <- sapply(as.list(mCall)[c(FALSE, TRUE, modp)],
				    as.character)
mods is only of length 2, but the sapply call evaluates to:
      object
[1,] "[["        "[["
[2,] "list(fm1)" "list(fm2)"
[3,] "1"         "1"


This problem occurs for both CRAN lme4 and r-forge lme4.0, but not lme4 devel from github.

I do not think this bug is critical and it is relatively easy to circumvent (i.e., simply assign one of the list models to a non-list object prior) but couldn't find it documented somewhere, so I thought it would be nice to report it.

Cheers,
Henrik