R中马氏距离最近邻匹配的子分类

时间:2020-07-05 06:37:48

标签: r mahalanobis

我正在使用MatchIt包来实现与Mahalonobis距离的最近邻居匹配。匹配阶段结束后,如何报告每个对照观察与哪个对照观察相匹配?

以下代码不起作用,并发出警告“纯马氏距离不进行子分类”。

library("MatchIt")

data("lalonde")

lalonde_matchit_nn <-
  matchit(
    treat ~ age + educ + black + hispan + nodegree + married + re74 + re75,
    baseline.group = 1,
    data = lalonde,
    method = "nearest",
    distance = "mahalanobis",
    subclass = T
  )

同样,我要寻找的是输出对每对处理和控制都有一个ID,就像使用其他匹配方法(例如“精确”或“ cem”)报告的子类一样。

1 个答案:

答案 0 :(得分:2)

在这种情况下,您正在寻找输出的属性:输出为lalonde_matchit_nn,属性为nnmatch.matrix

smry<-lalonde_matchit_nn$nn #A basic summary table of matched data (e.g., the number of matched units)

#represent the names of the treatment units, which
#come from the data frame specified in data. Each column stores the name(s)
#of the control unit(s) matched to the treatment unit of that row. F
matchedPool<-lalonde_matchit_nn$match.matrix

现在,如果您从上述代码中查看smry和匹配的池:

smry
          Control Treated
All           429     185
Matched       185     185
Unmatched     244       0
Discarded       0       0

head(matchedPool)

     1        
NSW1 "PSID375"
NSW2 "PSID341"
NSW3 "PSID361"
NSW4 "PSID345"
NSW5 "PSID172"
NSW6 "PSID237"

smry告诉每种类型的人口,匹配的池为您提供了根据您的最佳条件匹配的ID,在这种情况下为Mahanlobis距离,但是警告消息Warning message: No subclassification with pure Mahalanobis distance告诉您此方法其他最佳参数可能是更好的选择。

有关更多详细信息,请始终参考打包文档, https://cran.r-project.org/web/packages/MatchIt/MatchIt.pdf