问题描述
我正在尝试使用 MatchIt 创建两组匹配的投资公司(处理与控制)。
我需要仅使用进行治疗的 1-3 年的数据将治疗公司与对照公司相匹配。
例如,如果一家公司在 2009 年接受了治疗,那么我想使用 2009、2008、2007 年的数据对其进行匹配(在这种情况下,我的治疗后效果假人将保持 2010 年以后的值)
我不确定如何将此选择添加到我的匹配代码中,目前看起来像这样:
matchit(signatory ~ totalUSD + brownUSD + country + strategy,data = panel6,method = "full")
我是否应该考虑以某种方式使用“后”处理效果假人?
解决方法
在 MatchIt
中没有直接的方法可以做到这一点。您可以设置一个卡尺,它要求控制公司与被处理公司在一定的年限内,但没有办法要求控制公司严格在被处理公司之前有一年。您可以使用 exact
参数对年份执行精确匹配,以便处理公司和控制公司的年份完全相同。
另一种稍微复杂的方法是自己构建一个距离矩阵,并将禁止相互匹配的单位之间的任何距离设置为 Inf
。第一步是估计倾向得分,您可以手动或使用 matchit()
进行。然后构造一个距离矩阵,并针对距离矩阵中的每个条目,决定是否将距离设置为Inf
。最后,您可以将距离矩阵提供给 distance
的 matchit()
参数。这样做的方法如下:
#Estimate the propensity score
ps <- matchit(signatory ~ totalUSD + brownUSD + country + strategy,data = panel6,method = NULL)$distance
#Create the distance matrix
dist <- optmatch::match_on(signatory ~ ps,data = panel6)
#Loop through the matrix and set set disallowed matches to Inf
t <- which(panel6$signatory == 1)
u <- which(panel6$signatory != 1)
for (i in seq_along(t)) {
for (j in seq_along(u)) {
if (panel6$year[u[j]] > panel6$year[t[i]] || panel6$year[u[j]] < panel6$year[t[i]] - 2)
dist[i,j] <- Inf
}
}
#Note: can be vectorized for speed but shouldn't take long regardless
#Supply the distance matrix to matchit() and match
m <- matchit(signatory ~ totalUSD + brownUSD + country + strategy,method = "full",distance = dist)
应该可以。您可以通过使用 match.data()
来查看匹配公司的各个组来进行验证:
md <- match.data(m,data = panel6)
md <- md[with(md,order(subclass,signatory)),]
View(md) #assuming you're using RStudio
您应该看到,在子类中,控制单位比处理单位低 0-2 年。