bbolker / broom.mixed

tidy methods for mixed models in R
227 stars 23 forks source link

Error using tidy() with anova() for mixed model comparison #129

Closed ShannonDailey closed 1 year ago

ShannonDailey commented 1 year ago

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()
bbolker commented 1 year ago

Won't be able to get to this immediately, probably next week

On Wed, Aug 17, 2022, 6:30 PM Shannon Dailey @.***> wrote:

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()

— Reply to this email directly, view it on GitHub https://github.com/bbolker/broom.mixed/issues/129, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATIRXCROSYJS7PX2BEVOLVZVRXPANCNFSM563EK3YQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

bbolker commented 1 year ago

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 ...

bbolker commented 1 year ago

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)