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

多重插补与倾向匹配后,如何从cobalt的love plot移除指定协变量?

解决方法

你之前直接修改match.out$object$data$married无效,是因为match.out是MatchThem生成的多重插补匹配对象,每个插补后的匹配数据集都存在match.out$imputations列表中,单独修改某一个数据集的变量不会影响整体的变量提取逻辑。

下面提供两种可靠的实现方式:

方法一:指定要显示的变量(推荐)

利用love.plot()的vars参数,明确列出你想要展示的协变量,自动跳过married变量,无需修改原始匹配对象:

data("lalonde_mis", package = "cobalt")
set.seed(100)

# 生成插补数据集
m <- 10 
imp.out <- mice::mice(lalonde_mis, m = m, print = FALSE) 

# 倾向得分匹配
match.out <- MatchThem::matchthem(nodegree ~ age + race + 
                              re74 + re75, datasets = imp.out,
                           exact = "married",
                           method = "nearest",
                           caliper = 0.2,
                           distance = "glm")

# 绘制love plot,仅保留指定变量
love.plot(match.out, threshold = .05, stats = "m",
          abs = FALSE,
          stars = "std", 
          which.imp = .none, agg.fun = "mean",
          drop.distance = TRUE,
          vars = c("age", "race", "re74", "re75")) # 指定要展示的协变量

方法二:彻底移除匹配对象中的married变量

如果你后续所有分析都不需要married变量,可以遍历所有插补数据集,逐个删除该变量,同时更新对象的公式属性:

data("lalonde_mis", package = "cobalt")
set.seed(100)

# 生成插补数据集
m <- 10 
imp.out <- mice::mice(lalonde_mis, m = m, print = FALSE) 

# 倾向得分匹配
match.out <- MatchThem::matchthem(nodegree ~ age + race + 
                              re74 + re75, datasets = imp.out,
                           exact = "married",
                           method = "nearest",
                           caliper = 0.2,
                           distance = "glm")

# 遍历所有插补数据集,移除married变量
for (i in seq_along(match.out$imputations)) {
  match.out$imputations[[i]]$married <- NULL
}

# 更新匹配对象的公式,移除married变量(避免后续操作警告)
match.out$formula <- update(match.out$formula, . ~ . - married)

# 绘制love plot
love.plot(match.out, threshold = .05, stats = "m",
          abs = FALSE,
          stars = "std", 
          which.imp = .none, agg.fun = "mean",
          drop.distance = TRUE)

说明

  • 方法一无需改动原始匹配对象,操作简洁,仅调整绘图显示内容,适合临时需求;
  • 方法二会修改匹配对象的结构,适合后续所有分析都不需要married变量的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 20:55:04