【发布时间】:2023-04-06 17:01:02
【问题描述】:
因此,请遵循 Matching 包中的示例,尤其是 GenMatch 示例 Link to package description pp11。
我们有以下代码
library(Matching)
data(lalonde)
attach(lalonde)
lalonde$ID <- 1:length(lalonde$age)
X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
BalanceMat <- cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74,
I(re74*re75))
genout <- GenMatch(Tr=treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE", M=1,
pop.size=16, max.generations=10, wait.generations=1)
Y=re78/1000
mout <- Match(Y=Y, Tr=treat, X=X, Weight.matrix=genout)
summary(mout)
总结显示,所有 185 个treat==1 案例都已匹配
然后我们检查
summary(mout$weights)
这告诉我们,某些treat==1 案例已与treat==0 匹配多次
我想创建一个 data.frame,其中仅包含一次重复的 treat==1 案例,但所有 treat==0。
所以本质上,长度是185 + length(mout$index.control)
然后,我想引入一个变量 $PairID,对于每个 treat==1 案例,对于每个 treat==0 案例重复。
data.frame 应该是这样的:
所以上面我们看到案例 1-3 只返回一对,但案例 6 返回 2 对。这可以通过以下方式看到:mout$weights[mout$index.treated]
我的想法是先去掉重复的 $index.treatment 案例
treat <- lalonde[mout$index.treated,]
library(dplyr)
DATA_clean <- treat %>%
group_by(ID) %>%
filter(!n() > 1)
但这会删除所有重复的情况。我想留一个!
【问题讨论】:
-
我不知道 dplyr,但也许有类似
sample_n(1)的东西可以从小组中随机观察?