R中的精确匹配与GenMatch
下面是匹配包中的示例,尤其是GenMatch示例 下面的例子R中的精确匹配与GenMatch,r,matching,R,Matching,下面是匹配包中的示例,尤其是GenMatch示例 下面的例子 library(Matching) data(lalonde) attach(lalonde) X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74) BalanceMat <- cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74,
library(Matching)
data(lalonde)
attach(lalonde)
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)
您会得到相同的值首先,我应该说我以前从未使用过这些软件包和函数,我的回答完全基于对代码和函数文档的处理 似乎在
Match()
函数中,Weight.matrix
的优先级低于exact
。在它的帮助页面中有一个提示(?Match
):
权重矩阵:
此代码将更改由
通过将第一个变量乘以1000来计算方差
重量很重要强制执行精确匹配,请参见精确匹配
和卡钳选项
当它说你应该使用exact
来强制执行精确匹配(而不是给出手动或从GenMatch()
计算的权重)时,在我看来,它是说你应该使用其中一个。但是,当您向Weight.matrix
提供参数时,精确的
似乎被忽略。将其从函数中删除,您将得到不同的结果:
> mout2 <- Match(Y=Y, Tr=treat, X=X, exact=c(0,0,0,0,1,0,0,0,0,0))
> summary(mout2)
Estimate... 1.7605
AI SE...... 0.86408
T-stat..... 2.0374
p.val...... 0.041606
对于BalanceMat
,也可以执行同样的操作。另一个优点是将数据保持为数据帧
exact
参数,更简洁的方法是:X <- lalonde[,!(colnames(lalonde)=="re78" | colnames(lalonde) == "treat")]
#or
X <- subset(lalonde, select=-c(re78, treat)) #Subset is shorter in this case, but usually not recommended
#instead of
X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
exact = colnames(X)=="married"
这样,您就不太容易更改列顺序等
X <- lalonde[,!(colnames(lalonde)=="re78" | colnames(lalonde) == "treat")]
#or
X <- subset(lalonde, select=-c(re78, treat)) #Subset is shorter in this case, but usually not recommended
#instead of
X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)
exact = colnames(X)=="married"