gpt4 book ai didi

r - lme4::glmer.nb 函数根据我运行模型的顺序产生 "Error in family$family : $ operator not defined for this S4 class"

转载 作者:行者123 更新时间:2023-12-03 09:34:28 26 4
gpt4 key购买 nike

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 , 正确功能 poisson来自 stats找不到包。

Ben刚刚解决了这个问题,将其替换为:
mc$family <- quote(stats::poisson)

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

关于r - lme4::glmer.nb 函数根据我运行模型的顺序产生 "Error in family$family : $ operator not defined for this S4 class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39727183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com