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

零膨胀负二项模型(ZINB)回归结果合并报错求助

解决方案

问题根源

你的错误来自两个核心问题:

  1. 拟合ZINB模型后立刻用summarise(tidy(model))展开系数,导致后续数据框不再包含模型对象,而mice::pool需要的是模型对象列表,不是整理后的系数结果,因此触发类型不兼容错误。
  2. zeroinfl函数中错误指定data = cohort_1,导致所有模型都使用原始完整数据集,而非你抽样后的子集,完全违背了抽样的初衷。

步骤1:修正ZINB模型拟合代码

采用和逻辑回归一致的group_by() + do()方式,确保每个抽样子集拟合对应模型,同时保留模型对象:

library(dplyr)
library(tidyverse)
library(pscl)
library(broom)
library(mice)

# 修正模型拟合逻辑,确保用抽样后的子集建模
regr_comb_randcase.zeroinfl <- Combined_randcase %>% 
  group_by(sample) %>% 
  do(model = zeroinfl(
    formula = cm_number ~ after_wm + age + gender_male + ref_mode_police + ref_lg_invol + 
      ref_reas_selfharm + ref_reas_aggrpers + comm_limited + duration_days + 
      diagnosis_personality + diagnosis_psychosis + diagnosis_mania + diagnosis_substance + intoxication | 
      age + gender_male + ref_mode_police + ref_lg_invol + ref_reas_selfharm + ref_reas_aggrpers + 
      comm_limited + duration_days + diagnosis_personality + diagnosis_psychosis + diagnosis_mania + 
      diagnosis_substance + intoxication,
    data = .,  # 使用当前分组的抽样子集,而非原始数据集
    na.action = na.exclude,
    dist = "negbin"
  ))

步骤2:提取模型列表并合并结果

现在regr_comb_randcase.zeroinfl包含sample和model列,其中model是每个抽样子集的ZINB模型对象,直接提取后用pool()处理:

# 提取所有模型对象
models.zeroinfl <- regr_comb_randcase.zeroinfl$model

# 用mice的pool函数合并结果
pool_results.zeroinfl <- pool(models.zeroinfl)

# 查看合并结果,注意区分count(负二项计数模型)和zero(零膨胀logit模型)部分
summary(pool_results.zeroinfl)

备选方案:手动按Rubin规则合并(若pool不兼容ZINB)

如果mice::pool对zeroinfl对象支持不佳,可手动按Rubin规则合并结果:

2.1 提取所有模型的tidy结果

all_tidy_results <- regr_comb_randcase.zeroinfl %>% 
  mutate(tidy_result = list(tidy(model, conf.int = TRUE))) %>% 
  unnest(tidy_result)

2.2 按Rubin规则计算合并统计量

pooled_results <- all_tidy_results %>% 
  group_by(term, component) %>% 
  summarise(
    estimate = mean(estimate),
    within_var = mean(std.error^2),
    between_var = var(estimate),
    total_var = within_var + (1 + 1/n()) * between_var,
    std.error = sqrt(total_var),
    statistic = estimate / std.error,
    p.value = 2 * pnorm(-abs(statistic)),
    conf.low = estimate - qnorm(0.975)*std.error,
    conf.high = estimate + qnorm(0.975)*std.error,
    .groups = "drop"
  )

# 查看完整合并结果
print(pooled_results, n = Inf)

关键注意事项

  • 务必保证zeroinfl的data参数为data = .,否则抽样操作完全无效。
  • ZINB模型结果分为count(计数部分)和zero(零膨胀部分),合并时需注意区分两类系数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 21:42:29