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

在CMAverse的cmest函数中传递mice参数时遇列选择错误

解决CMAverse cmest函数传入predictorMatrix时的"undefined columns selected"错误

问题根源

你遇到的错误本质是:cmest调用mice插补时,无法找到predictorMatrix中指定的部分变量。核心原因有两点:

  1. 传入的data仅包含模型用到的7个变量(X、Y、M1-M3、C1-C2),但PredMatrix_alldata包含46个变量,其中大部分不在data中,导致mice找不到对应列。
  2. cmest默认会从data中仅筛选模型所需变量(结果、暴露、中介、协变量)传给mice,即便data包含额外变量,也可能被自动过滤,导致predictorMatrix中的额外变量无法被识别。

解决方案

要实现用模型外变量插补模型内变量,需按以下步骤调整:

1. 确保data包含所有插补所需变量

把你想用于插补的46个变量全部包含在传入cmest的data数据框中,不能只保留模型用到的7个。这样mice才能访问到所有用于插补的变量。

2. 调整预测矩阵匹配data的变量范围

修正PredMatrix_alldata,确保它的行名和列名与data的所有变量名完全一致(注意大小写、拼写、下划线等细节),避免出现data中不存在的变量名。可以用以下代码快速调整:

# 确保预测矩阵的行/列与data的变量完全匹配
PredMatrix_adjusted <- PredMatrix_alldata[colnames(data), colnames(data)]

3. 重新运行cmest

将调整后的预测矩阵传入args_mice,同时确保data包含所有46个变量:

X=binary  
Y=continuous  
M1, M2, M3 = continuous  
C1, C2 =continuous

# 确保data包含全部46个插补所需变量
Model <- cmest(data = data, model = "rb", outcome = "Y", 
               exposure = "X", mediator = c("M1", "M2", "M3"), 
               basec = c("C1", "C2"), EMint = TRUE, 
               mreg = list("linear", "linear", "linear"), yreg = "linear", astar = 0, a = 1, mval = list(12, 13, 14), 
               estimation = "imputation", inference = "bootstrap", nboot = 500, boot.ci.type = "bca", 
               multimp = TRUE, args_mice = list(m=5, predictorMatrix = PredMatrix_adjusted, maxit=10))

额外验证步骤

  • 检查colnames(data)和rownames(PredMatrix_adjusted)是否完全一致,可使用all(colnames(data) == rownames(PredMatrix_adjusted))验证,返回TRUE才正确。
  • 确认预测矩阵中,用于插补模型内变量的额外变量对应的位置已设为1,模型内变量的插补规则也符合你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 13:23:33