lme4::glmer.nb 函数会根据我运行模型的顺序产生“family$family 中的错误:未为此 S4 类定义 $ 运算符”

can*_*156 7 r lme4 mixed-models

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"))
Run Code Online (Sandbox Code Playgroud)

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

Error in family$family : $ operator not defined for this S4 class
Run Code Online (Sandbox Code Playgroud)

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

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")
Run Code Online (Sandbox Code Playgroud)

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

李哲源*_*李哲源 3

因为你有屏蔽 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"))
Run Code Online (Sandbox Code Playgroud)

这就是问题所在。在最开始的几行中glmer.nb有一行:

mc$family <- quote(poisson)
Run Code Online (Sandbox Code Playgroud)

因此,如果您屏蔽,则无法找到包中的poisson正确函数。poissonstats

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

mc$family <- quote(stats::poisson)
Run Code Online (Sandbox Code Playgroud)

我最初的观察family = "poisson"并不是match.fun真正的问题。它仅解释了为什么在像glmand 这样的例程中mgcv::gam,传递字符串 是合法的family