Lets take a following simple exercise from a previous question.
Inputting the following code in R, we finally output our P1
variable as:
library(Matching)
data(lalonde)
lalonde$ID <- 1:length(lalonde$age)
n <- 10
P1 <- rep(NA, n)
for (i in 1:n) {
lalonde <- lalonde[sample(1:nrow(lalonde)), ] # randomise the order
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)
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]
}
And we can obtain our result:
summary(as.factor(P1))
I notice this is a low percent of CPU, so I call upon doParallel
package and try and run the loop
and want to output the same result (that is save P1[i]
). But I get an error:
require(doParallel)
cl <- makeCluster(3)
registerDoParallel(cl)
m <- 10
P1 <- rep(NA, m)
Result <- foreach(i=icount(m),.combine=cbind) %dopar% {
lalonde <- lalonde[sample(1:nrow(lalonde)), ] # randomise the order
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)
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 ]
}
that GenMatch
cannot be found. Any suggestion to improve my code?
When you create cluster, you create new invisible R sessions. So you have to give to your clusters the non-base functions. Try to run: