Error using tidy() with anova() for mixed model comparison
ShannonDailey opened this issue · 3 comments
I am using anova() to compare mixed models, which previously had worked, but is now throwing this error:
Error in `mutate()`:
! Can't transform a data frame with duplicate names.
I'm running R version 4.2.1 (2022-06-23) and broom.mixed version 0.2.9.4. Here is a (hopefully) reproducible example:
library(tidyverse)
library(lme4)
library(broom.mixed)
mod1 <- lmer(data = mtcars, mpg ~ hp + (1|gear))
mod2 <- lmer(data = mtcars, mpg ~ hp + disp + (1|gear))
anova(mod1, mod2, refit = FALSE) %>% broom.mixed::tidy()
confirm that I can reproduce (proximal problem happens in broom:::tidy.anova()
, but I'll have to see whether this is best to fix in broom.mixed
or in broom
...
This has to do with a difference in the structure of the header between the anova()
object produced in base R and lme4. In particular, the header on a base-R anova()
is "Analysis of Variance Table ... Model 1: ... Model 2: ...", whereas for lme4
it is "Models: mod1: ... mod2 ...".
I think I can probably hack broom::tidy.anova()
to make this work more generally, but I'm surprised this ever worked. There have been minor modifications to the anova()
output over the years, but I don't see where the header structure has changed ...
Looking at the NEWS file for broom for version 1.0.0 we see
Adds a term column and introduces support for car::lht() output in tidy.anova() (#1106 by @grantmcdermott).
I think this is probably what broke. This has already been raised here ...
mod3 <- lm(data = mtcars, mpg ~ hp)
mod4 <- lm(data = mtcars, mpg ~ hp + disp)
anova(mod3, mod4)