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

调用mice包D2函数做Cox模型Wald检验时出现D3不支持错误

关于mice包D2函数对比Cox模型与仅截距模型时的错误问题

我尝试使用mice包中的D2()函数,对基于多重插补数据的生存分析进行Wald检验,以此对比拟合模型与仅截距模型。以下是可复现代码:

surv_data <- as.data.frame(list(time=c(4,3,1,1,2,2,3,5,2,4,5,1), 
                             status=c(1,1,1,0,1,1,0,0,1,1,0,0), 
                             x=c(0,2,1,1,3,2,0,1,1,2,0,1), 
                             sex=c(0,0,0,0,1,1,1,1,0,1,0,0),
                             group=c(0,1,0,1,1,0,1,0,0,1,0,0)))
imp <- mice(surv_data,m=10)
coxModel<-with(imp, coxph(Surv(time, status) ~ sex))
D2(coxModel)

运行后返回错误:

Error: D3 does not support the Cox model.

但对比其他模型时,D2()函数可正常工作,例如:

coxModel1<-with(imp, coxph(Surv(time, status) ~ sex+group))
D2(coxModel1,coxModel)

返回结果:

test statistic df1 df2 dfcom p.value riv
1 ~~ 2 0.1776136 1 Inf NA 0.6734321 0

显式设置并对比仅截距模型时也可正常运行:

coxModel0<-with(imp, coxph(Surv(time, status) ~ 1))
D2(coxModel,coxModel0)

返回结果:

test statistic df1 df2 dfcom p.value riv
1 ~~ 2 0.1650726 1 Inf NA 0.6845286 0

问题解析

这并非bug,而是D2()函数的内部设计逻辑导致的:

  • 当仅传入一个模型参数时,D2()会默认调用D3()函数自动生成对应的仅截距基准模型,再进行对比。但D3()函数本身不支持Cox模型,因此抛出错误。
  • 当显式传入两个模型(包括手动拟合的仅截距模型)时,D2()会直接执行两个模型间的Wald检验逻辑,无需调用D3(),因此可以正常运行。

解决方案

若要对比Cox模型与仅截距模型,必须手动拟合仅截距模型coxph(Surv(time, status) ~ 1),再将两个模型同时传入D2()函数即可,就像最后测试的那样。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 08:28:12