lme4 :: glmer.nb函数产生“$family系列中的错误:$S4未定义此类的运算符”,具体取决于我运行模型的顺序

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

当我在使用lme4包的负二项式模型之前运行创建泊松模型的脚本时,运行neg.bin模型时出现以下错误:

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

但是,如果我以相反的顺序运行模型,我不会出现错误消息.

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

neg.bin模型示例确实有收敛警告,但同样的模式正在发生在我的实际模型正在收敛.如何运行泊松模型首先影响neg.bin模型?

最佳答案 因为你已经屏蔽了R函数泊松.以下工作正常(除了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)

因此,如果掩盖泊松,则无法找到来自stats包的正确函数泊松.

Ben刚刚解决了这个问题,将其替换为:

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

我对family =“poisson”和match.fun东西的原始观察不是真正的问题.它只能解释为什么在像glm和mgcv :: gam这样的例程中,传递一串字符串是合法的.

点赞