R中的一致匹配对
consistent matched pairs in R
所以使用 Matching
包 (Link to package here)
我们可以通过修改后的 GenMatch
示例来工作。
library(Matching)
data(lalonde)
#introduce an id vaiable
lalonde$ID <- 1:length(lalonde$age)
X = cbind(lalonde$age, lalonde$educ, lalonde$black, lalonde$hisp,
lalonde$married, lalonde$nodegr, lalonde$u74, lalonde$u75,
lalonde$re75, lalonde$re74)
BalanceMat <- cbind(lalonde$age, lalonde$educ, lalonde$black,
lalonde$hisp, lalonde$married, lalonde$nodegr,
lalonde$u74, lalonde$u75, lalonde$re75, lalonde$re74,
I(lalonde$re74*lalonde$re75))
genout <- GenMatch(Tr=lalonde$treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE",
pop.size=16, max.generations=10, wait.generations=1)
mout <- Match(Y=NULL, Tr=lalonde$treat, X=X,
Weight.matrix=genout,
replace=TRUE, ties=FALSE)
# here we set ties FALSE so we only have 1-1 Matching
summary(mout)
#now lets create our "Matched dataset"
treated <- lalonde[mout$index.treated,]
# and introduce an indetity variable for each pair
treated$Pair_ID <- treated$ID
non.treated <- lalonde[mout$index.control,]
non.treated$Pair_ID <- treated$ID
matched.data <- rbind(treated, non.treated)
matched.data <- matched.data[order(matched.data$Pair_ID),]
#this outputs which of the non-treated ID was paired with the first person
matched.data$ID[matched.data$Pair_ID==1 & matched.data$treat==0]
我们看到对于数据,ID=1 与 ID=193
匹配
现在让我们在数据顺序中引入一些随机化,看看我们是否得到相同的对
n <- 500
P1 <- rep(NA, n)
P2 <- rep(NA, n)
P3 <- rep(NA, n)
P4 <- rep(NA, n)
P5 <- rep(NA, n)
P6 <- rep(NA, n)
P7 <- rep(NA, n)
for (i in 1:n) {
lalonde <- lalonde[sample(1:nrow(lalonde)), ] # randomise order
genout <- GenMatch(Tr=lalonde$treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE",
pop.size=16, max.generations=10, wait.generations=1)
mout <- Match(Y=NULL, Tr=lalonde$treat, X=X,
Weight.matrix=genout,
replace=TRUE, ties=FALSE)
summary(mout)
treated <- lalonde[mout$index.treated,]
treated$Pair_ID <- treated$ID
non.treated <- lalonde[mout$index.control,]
non.treated$Pair_ID <- treated$ID
matched.data <- rbind(treated, non.treated)
matched.data <- matched.data[order(matched.data$Pair_ID),]
P1[i] <- matched.data$ID[matched.data$Pair_ID==1 & matched.data$treat==0]
P2[i] <- matched.data$ID[matched.data$Pair_ID==2 & matched.data$treat==0]
P3[i] <- matched.data$ID[matched.data$Pair_ID==3 & matched.data$treat==0]
P4[i] <- matched.data$ID[matched.data$Pair_ID==4 & matched.data$treat==0]
P5[i] <- matched.data$ID[matched.data$Pair_ID==5 & matched.data$treat==0]
P6[i] <- matched.data$ID[matched.data$Pair_ID==6 & matched.data$treat==0]
P7[i] <- matched.data$ID[matched.data$Pair_ID==7 & matched.data$treat==0]
}
因此 loop
将匹配对 500 次并且 P1
将每次保存 treat==0
案例。
然后我们看看哪个 P1
出现的次数最多,作者:
plot(1:n, P1, main="P1")
或
summary(as.factor(P1))
我们发现没有一个 treat==0
案例是通常配对的。
我希望有一个案例(可能=193??)通常配对不依赖于数据的顺序。因此我认为我的循环是错误的。有人可以指出在哪里吗?或者当他们 运行 一个循环时,他们发现,与数据的顺序无关,相似的案例是成对的??
问题是你随机化了 lalonde
的顺序,但是你对 GenMatch
和 Match
的输入是 X
和 BalanceMat
,它们仍然有原来的顺序。当您最后构建 matched.data
时,您正在使用不再与 lalonde
绑定的索引进行子集化。再试一次,但在循环中包括 X
和 BalanceMat
的赋值。
即
X = cbind(lalonde$age, lalonde$educ, lalonde$black, lalonde$hisp,
lalonde$married, lalonde$nodegr, lalonde$u74, lalonde$u75,
lalonde$re75, lalonde$re74)
BalanceMat <- cbind(lalonde$age, lalonde$educ, lalonde$black,
lalonde$hisp, lalonde$married, lalonde$nodegr,
lalonde$u74, lalonde$u75, lalonde$re75, lalonde$re74,
I(lalonde$re74*lalonde$re75))
所以使用 Matching
包 (Link to package here)
我们可以通过修改后的 GenMatch
示例来工作。
library(Matching)
data(lalonde)
#introduce an id vaiable
lalonde$ID <- 1:length(lalonde$age)
X = cbind(lalonde$age, lalonde$educ, lalonde$black, lalonde$hisp,
lalonde$married, lalonde$nodegr, lalonde$u74, lalonde$u75,
lalonde$re75, lalonde$re74)
BalanceMat <- cbind(lalonde$age, lalonde$educ, lalonde$black,
lalonde$hisp, lalonde$married, lalonde$nodegr,
lalonde$u74, lalonde$u75, lalonde$re75, lalonde$re74,
I(lalonde$re74*lalonde$re75))
genout <- GenMatch(Tr=lalonde$treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE",
pop.size=16, max.generations=10, wait.generations=1)
mout <- Match(Y=NULL, Tr=lalonde$treat, X=X,
Weight.matrix=genout,
replace=TRUE, ties=FALSE)
# here we set ties FALSE so we only have 1-1 Matching
summary(mout)
#now lets create our "Matched dataset"
treated <- lalonde[mout$index.treated,]
# and introduce an indetity variable for each pair
treated$Pair_ID <- treated$ID
non.treated <- lalonde[mout$index.control,]
non.treated$Pair_ID <- treated$ID
matched.data <- rbind(treated, non.treated)
matched.data <- matched.data[order(matched.data$Pair_ID),]
#this outputs which of the non-treated ID was paired with the first person
matched.data$ID[matched.data$Pair_ID==1 & matched.data$treat==0]
我们看到对于数据,ID=1 与 ID=193
匹配现在让我们在数据顺序中引入一些随机化,看看我们是否得到相同的对
n <- 500
P1 <- rep(NA, n)
P2 <- rep(NA, n)
P3 <- rep(NA, n)
P4 <- rep(NA, n)
P5 <- rep(NA, n)
P6 <- rep(NA, n)
P7 <- rep(NA, n)
for (i in 1:n) {
lalonde <- lalonde[sample(1:nrow(lalonde)), ] # randomise order
genout <- GenMatch(Tr=lalonde$treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE",
pop.size=16, max.generations=10, wait.generations=1)
mout <- Match(Y=NULL, Tr=lalonde$treat, X=X,
Weight.matrix=genout,
replace=TRUE, ties=FALSE)
summary(mout)
treated <- lalonde[mout$index.treated,]
treated$Pair_ID <- treated$ID
non.treated <- lalonde[mout$index.control,]
non.treated$Pair_ID <- treated$ID
matched.data <- rbind(treated, non.treated)
matched.data <- matched.data[order(matched.data$Pair_ID),]
P1[i] <- matched.data$ID[matched.data$Pair_ID==1 & matched.data$treat==0]
P2[i] <- matched.data$ID[matched.data$Pair_ID==2 & matched.data$treat==0]
P3[i] <- matched.data$ID[matched.data$Pair_ID==3 & matched.data$treat==0]
P4[i] <- matched.data$ID[matched.data$Pair_ID==4 & matched.data$treat==0]
P5[i] <- matched.data$ID[matched.data$Pair_ID==5 & matched.data$treat==0]
P6[i] <- matched.data$ID[matched.data$Pair_ID==6 & matched.data$treat==0]
P7[i] <- matched.data$ID[matched.data$Pair_ID==7 & matched.data$treat==0]
}
因此 loop
将匹配对 500 次并且 P1
将每次保存 treat==0
案例。
然后我们看看哪个 P1
出现的次数最多,作者:
plot(1:n, P1, main="P1")
或
summary(as.factor(P1))
我们发现没有一个 treat==0
案例是通常配对的。
我希望有一个案例(可能=193??)通常配对不依赖于数据的顺序。因此我认为我的循环是错误的。有人可以指出在哪里吗?或者当他们 运行 一个循环时,他们发现,与数据的顺序无关,相似的案例是成对的??
问题是你随机化了 lalonde
的顺序,但是你对 GenMatch
和 Match
的输入是 X
和 BalanceMat
,它们仍然有原来的顺序。当您最后构建 matched.data
时,您正在使用不再与 lalonde
绑定的索引进行子集化。再试一次,但在循环中包括 X
和 BalanceMat
的赋值。
即
X = cbind(lalonde$age, lalonde$educ, lalonde$black, lalonde$hisp,
lalonde$married, lalonde$nodegr, lalonde$u74, lalonde$u75,
lalonde$re75, lalonde$re74)
BalanceMat <- cbind(lalonde$age, lalonde$educ, lalonde$black,
lalonde$hisp, lalonde$married, lalonde$nodegr,
lalonde$u74, lalonde$u75, lalonde$re75, lalonde$re74,
I(lalonde$re74*lalonde$re75))