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模型?
答案 0 :(得分:2)
因为你屏蔽了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
字符串是合法的。