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

运行Matching::Match执行倾向得分匹配后如何提取对照组均值

匹配后对照组均值提取与可视化实现

核心逻辑

Match函数返回结果的index.control字段确实存储了所有匹配到的对照组样本的行索引,你之前方法失效大概率是忽略了 replace = TRUE 有放回匹配的权重规则:同一个对照组样本可能被多次匹配给不同处理组样本,直接对去重后的对照组样本取均值会忽略匹配次数的权重,导致计算结果偏差。

具体实现代码

# 1. 提取匹配到的对照组与处理组的Y变量(lfp)取值
matched_control_y <- matching_df$lfp[psm1$index.control]
matched_treat_y <- matching_df$lfp[psm1$index.treated]

# 2. 按匹配权重计算两组均值(无放回匹配可直接用mean()替换weighted.mean())
control_mean <- weighted.mean(matched_control_y, w = psm1$weights)
treat_mean <- weighted.mean(matched_treat_y, w = psm1$weights)

# 验证:treat_mean和control_mean + psm1$est结果完全一致
all.equal(treat_mean, control_mean + psm1$est)

# 3. 计算两组均值的标准误
library(matrixStats)
control_se <- weightedSd(matched_control_y, w = psm1$weights) / sqrt(psm1$nobs)
treat_se <- psm1$se # 处理组均值的标准误可直接用匹配返回的ATT标准误

可视化示例

library(ggplot2)
# 构造绘图数据框
plot_data <- data.frame(
  group = c("匹配后对照组", "匹配后处理组"),
  mean = c(control_mean, treat_mean),
  se = c(control_se, treat_se)
)

# 绘制带95%置信区间的均值对比柱形图
ggplot(plot_data, aes(x = group, y = mean, fill = group)) +
  geom_col(width = 0.6, alpha = 0.8) +
  geom_errorbar(aes(ymin = mean - 1.96 * se, ymax = mean + 1.96 * se),
                width = 0.2, linewidth = 0.8) +
  labs(x = "组别", y = "lfp均值", title = "PSM匹配后两组lfp均值对比") +
  theme_bw() +
  theme(legend.position = "none")

内容的提问来源于stack exchange,提问作者C.Robin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:54:05