You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行zipoisson模型报错“$运算符不适用于该S4类”的解决咨询

解决glmmTMB零膨胀泊松模型的$ operator not defined for this S4 class错误

在使用glmmTMB拟合零膨胀泊松(zipoisson)模型时触发错误Error in family$family : $ operator not defined for this S4 class,数据因大量零值存在偏态,已尝试普通泊松和拟泊松模型,现需修正代码解决该错误,原始代码如下:

# Fit the GLMM model for zero-inflated Poisson with mean duration
GLMM_freq_dur <- glmmTMB(N.x ~ duration * Sex + (1 | focalID) + (1 | videoID), 
                     data = GLMM_data, 
                     family = poisson (link='log'),zi=~1|focalID,
                     control = glmmTMBControl(optimizer = "bobyqa", optCtrl = 
list(maxfun = 1e6)))  # Increase max iterations

# Fit the GLMM model for zero-inflated Poisson with number of action units
GLMM_freq_au <- glmmTMB(N ~ Total_AU * Sex + (1 | focalID) + (1 | videoID), 
                    data = GLMM_data, 
                    family = zipoisson, 
                    control = glmmTMBControl(optimizer = "bobyqa", optCtrl = 
list(maxfun = 1e6)))  # Increase max iterations

错误原因与修正方案

  • 第一个模型的问题:指定了family = poisson(link='log')(普通泊松族),同时添加了zi=~1|focalID参数。glmmTMB中只有零膨胀族(如zipoisson)才能搭配zi参数,普通poisson族不支持该参数,导致族对象类型不匹配。
  • 第二个模型的问题:family = zipoisson直接引用族类名,而glmmTMB需要调用族函数生成族对象(必须带括号),正确写法为zipoisson(link="log")(log链接为默认值,可省略,但显式写出更清晰)。

修正后的代码

# 修正后的零膨胀泊松模型(基于时长)
GLMM_freq_dur <- glmmTMB(N.x ~ duration * Sex + (1 | focalID) + (1 | videoID), 
                     data = GLMM_data, 
                     family = zipoisson(link='log'),  # 替换为零膨胀泊松族函数
                     zi=~1|focalID,  # 保留零膨胀部分的随机效应
                     control = glmmTMBControl(optimizer = "bobyqa", optCtrl = list(maxfun = 1e6)))

# 修正后的零膨胀泊松模型(基于动作单元数量)
GLMM_freq_au <- glmmTMB(N ~ Total_AU * Sex + (1 | focalID) + (1 | videoID), 
                    data = GLMM_data, 
                    family = zipoisson(link='log'),  # 调用族函数生成对象
                    control = glmmTMBControl(optimizer = "bobyqa", optCtrl = list(maxfun = 1e6)))

额外检查

若修正后仍报错,建议更新glmmTMB包到最新版本:

update.packages("glmmTMB")

内容的提问来源于stack exchange,提问作者Siddharth Satishchandran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:07:19