【问题标题】:make R compute with multiple CPU cores使 R 计算具有多个 CPU 内核
【发布时间】:2015-06-22 13:26:16
【问题描述】:

让我们从previous question 做一个简单的练习。

在R中输入如下代码,我们最终将P1变量输出为:

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]
}

我们可以得到我们的结果:

summary(as.factor(P1))

我注意到这是一个低百分比的 CPU,所以我调用 doParallel 包并尝试运行 loop 并希望输出相同的结果(即保存 P1[i])。但我得到一个错误:

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 ]
}

找不到GenMatch。有什么改进我的代码的建议吗?

【问题讨论】:

  • 尝试使用Matching::GenMatch。
  • 另外,this 可能会有所帮助。
  • 感谢您的链接,它很棒,并且对 dax 提供的答案表示赞赏

标签: r memory cluster-computing doparallel


【解决方案1】:

创建集群时,您会创建新的不可见 R 会话。因此,您必须为集群提供非基本功能。尝试运行:

clusterEvalQ(cl,library(Matching))
clusterEvalQ(cl,library(rgenoud))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2016-04-17
    • 2023-01-15
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多