使用 matchit 在 R 中进行最近邻匹配

机器算法验证 r 匹配 倾向评分
2022-03-04 07:26:22

我正在使用 matchit 包对数据集进行倾向得分匹配。但是,在进行最近邻匹配时,如果我使用 caliper 选项,我每次都会得到一组不同的匹配对 - 即第一次处理 #18 匹配到控制 #2276,但如果我重新运行代码,处理 #18 匹配控制#2079(等等)。如果我移除卡尺选项,我每次都会得到相同的匹配结果,但是移除卡尺后产生的额外匹配会根据我的喜好产生距离有点远的匹配。

例如,如果我运行以下代码,请注意控制方式的差异:

match.out <- matchit(Category ~ FactorA + FactorB, Data, 
                     method = 'nearest', distance = 'logit', caliper = .10)
round(summary(match.out)$sum.matched, digits = 3)

           Means Treated   Means Control   SD Control    Mean Diff 
distance       0.506           0.496         0.151        0.010   
FactorA        24.243          24.450        3.344       -0.207  
FactorB        3.542           3.551         0.392       -0.008  


match.out <- matchit(Category ~ FactorA + FactorB, Data, 
                     method = 'nearest', distance = 'logit', caliper = .10)
round(summary(match.out)$sum.matched, digits = 3)

           Means Treated   Means Control   SD Control    Mean Diff 
distance       0.506           0.496         0.151        0.010   
FactorA        24.243          24.427        3.351       -0.184  
FactorB        3.542           3.541         0.392       -0.002

这对我来说是个问题,因为如果需要,我更希望能够准确地重现我的结果。但是我可以在没有 caliper 参数的情况下运行 matchit:

match.out <- matchit(Category ~ FactorA + FactorB, Data, 
                     method = 'nearest', distance = 'logit')

并全天获得完全相同的治疗控制匹配。(我实际上检查了匹配矩阵来验证这一点 - 这不仅仅是偶然的相同控制平均值)。

有没有办法仍然使用卡尺进行我在第一个代码块中所做的最近邻匹配,以稍微缩小我的匹配范围,但如果我重新运行代码仍然得到相同的结果?

感谢您提供任何帮助(不仅是在这个问题上,而是所有 - 虽然这是我觉得有必要在这里发布的第一个问题,但我在这里找到了很多答案)

2个回答

我不是 R 和倾向匹配方面的专家,但我在做一个项目时遇到了同样的问题。我认为matchit做的是随机选择一个落在治疗对象周围的卡尺间隔内的控制对象。如果每次运行时都将种子设置为相同的数字match.out,您将得到相同的结果:

set.seed(100)
match.out <- matchit(Category ~ FactorA + FactorB, Data, 
                     method = 'nearest', distance = 'logit', caliper = .10)

尝试将这两行一起运行。

我遇到了同样的问题。使用该函数将种子设置为固定数字set.seed(),但是,通过更改此函数中给出的数字,结果将发生变化。确实,matchit()当他们落入卡尺时会随机选择控制对象。

通过使用该参数mahvars,您可以根据哪些变量定义应从卡尺内的控制对象池中选择对象。

MatchIt 手册(第 19 页):

mahvars:在每个卡尺内执行马氏度量匹配的变量(默认= NULL)。变量应作为变量名称的向量输入。(例如,mahvars = c("X1", "X2"))。如果mahvars 不指定caliper,则卡尺设置为 0.25。