lme4::glmer.nb 函数根据顺序生成 "Error in family$family : $ operator not defined for this S4 class" 运行 模型
lme4::glmer.nb function produces "Error in family$family : $ operator not defined for this S4 class" depending on the order I run 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"))
当我 运行 使用 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 函数 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
,则无法找到 stats
包中的正确函数 poisson
。
Ben 刚刚解决了这个问题,将其替换为:
mc$family <- quote(stats::poisson)
我最初对 family = "poisson"
和 match.fun
的观察并不是这里的真正问题。它只是解释了为什么在glm
和mgcv::gam
这样的例程中,传递一串family
.
是合法的
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 函数 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
,则无法找到 stats
包中的正确函数 poisson
。
Ben 刚刚解决了这个问题,将其替换为:
mc$family <- quote(stats::poisson)
我最初对 family = "poisson"
和 match.fun
的观察并不是这里的真正问题。它只是解释了为什么在glm
和mgcv::gam
这样的例程中,传递一串family
.