我是 R 中并行计算的初学者。我遇到了这个doParallel
包,我认为它可能对我有用。
以下代码旨在并行评估多个pglm
回归:
require("foreach")
require("doParallel")
resVar <- sample(1:6,100,TRUE)
x1 <- 1:100
x2 <- rnorm(100)
x3 <- rchisq(100, 2, ncp = 0)
x4 <- rweibull(100, 1, scale = 1)
Year <- sample(2011:2014,100,replace=TRUE)
X <- data.frame(resVar,x1,x2,x3,x4,Year)
facInt = 1:4 # no factors
#find all possible combinations
cmbList <- lapply(2, function(nbFact) {
allCmbs <- t(combn(facInt, nbFact))
dupCmbs <- combn(1:4, nbFact, function(x) any(duplicated(x)))
allCmbs[!dupCmbs, , drop = FALSE] })
noSubModel <- c(0, sapply(cmbList, nrow))
noModel <- sum(noSubModel)
combinations <- cmbList[[1]]
factors <- X[,c("x1","x2","x3","x4")]
coeff_vars <- matrix(colnames(factors)[combinations[1:length(combinations[,1]),]],ncol = length(combinations[1,]))
yName <- 'resVar'
cl <- makeCluster(4)
registerDoParallel(cl)
r <- foreach(subModelInd=1:noSubModel[2], .combine=cbind) %dopar% {
require("pglm")
vars <- coeff_vars[subModelInd,]
formula <- as.formula(paste('as.numeric(', yName, ')',' ~ ', paste(vars,collapse=' + ')))
XX<-X[,c("resVar",vars,"Year")]
ans <- pglm(formula, data = XX, family = ordinal('logit'), model = "random", method = "bfgs", print.level = 3, R = 5, index = 'Year')
coefficients(ans)
}
stopCluster(cl)
cl <- c()
当我尝试以下列方式并行化它时,它不起作用。我收到以下错误:
{ 中的错误:任务 1 失败 - “找不到对象 'XX'”
一组几个pglm
回归顺序评估的作品:
require("pglm")
r <- foreach(icount(subModelInd), .combine=cbind) %do% {
vars <- coeff_vars[subModelInd,]
formula <- as.formula(paste('as.numeric(', yName, ')',' ~ ', paste(vars,collapse=' + ')))
XX<-X[,c("resVar",vars,"Year")]
ans <- pglm(formula, data = XX, family = ordinal('logit'), model = "random", method = "bfgs", print.level = 3, R = 5, index = 'Year')
coefficients(ans)
}
有人可以就如何正确并行化此任务提出建议吗?
谢谢!