The lme4 :: glmer.nb function creates a "Error in the $ family: $ operator not defined for this S4 class" depending on the order in which models are run

library(lme4)

dummy <- as.data.frame(cbind(speed = rpois(100, 10), pop = rep(1:4, each = 25), season = rep(1:2, each = 50), id = seq(1, 100, by = 1)))
dummy2 <- as.data.frame(cbind(speed = c(rnbinom(50, 10, 0.6), rnbinom(50, 10, 0.1)), pop = rep(1:4, each = 25), season = rep(1:2, each = 50), id = seq(1, 100, by = 1)))

poisson <- glmer(speed~pop*season + (1|id),
             data=dummy, family="poisson")
neg.bin <- glmer.nb(speed ~ pop*season + (1|id),
                data=dummy2, control=glmerControl(optimizer="bobyqa"))

When I run the script, creating the Poisson model before the negative binomial model using the lme4 package, when I run the neg.bin model, I get the following error:

Error in family$family : $ operator not defined for this S4 class

However, if I run the models in the reverse order, I do not report an error.

library(lme4)
dummy <- as.data.frame(cbind(speed = rpois(100, 10), pop = rep(1:4, each = 25), season = rep(1:2, each = 50), id = seq(1, 100, by = 1)))
dummy2 <- as.data.frame(cbind(speed = c(rnbinom(50, 10, 0.6), rnbinom(50, 10, 0.1)), pop = rep(1:4, each = 25), season = rep(1:2, each = 50), id = seq(1, 100, by = 1)))
neg.bin <- glmer.nb(speed ~ pop*season + (1|id),
                data=dummy2, control=glmerControl(optimizer="bobyqa"))
poisson <- glmer(speed~pop*season + (1|id),
             data=dummy, family="poisson")

In the example of the neg.bin model, there are warnings about convergence, but the same picture happens with my real models, which converge well. How does the Poisson model work, first influencing the neg.bin model?

+4
source share
1 answer

R poisson. ( , neg.bin):

library(lme4)
set.seed(0)
dummy <- as.data.frame(cbind(speed = rpois(100, 10), pop = rep(1:4, each = 25), season = rep(1:2, each = 50), id = seq(1, 100, by = 1)))
dummy2 <- as.data.frame(cbind(speed = c(rnbinom(50, 10, 0.6), rnbinom(50, 10, 0.1)), pop = rep(1:4, each = 25), season = rep(1:2, each = 50), id = seq(1, 100, by = 1)))

## use a different name for your model, say `poisson_fit`
poisson_fit <- glmer(speed~pop*season + (1|id),
         data=dummy, family="poisson")

negbin_fit <- glmer.nb(speed ~ pop*season + (1|id),
            data=dummy2, control=glmerControl(optimizer="bobyqa"))

. glmer.nb :

mc$family <- quote(poisson)

, poisson, ​​ poisson stats .

Ben , :

mc$family <- quote(stats::poisson)

family = "poisson" match.fun . , , glm mgcv::gam, family.

+2

Source: https://habr.com/ru/post/1655975/


All Articles