mike.dl
mike.dl

Reputation: 67

Parallelise panel logit computations in R on a set of different explanatory variables

I'm a beginner to parallel computing in R. I came across the doParallel package and I thought it might be useful in my case.

The following code aims at evaluating in parallel several pglm regressions:

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()

When I try to parallelise it in the following way, it doesn't work. I get the following error:

Error in { : task 1 failed - "object 'XX' not found"

A set of several pglm regressions sequentially evaluated works:

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)

}

Can someone please advice on how to parallelise this task correctly?

Thanks!

Upvotes: 3

Views: 137

Answers (1)

rbm
rbm

Reputation: 3253

Yes, it does look like there is an issue with pglm and the way it accesses variables. A simple fix is to assign the XX into global variable, i.e. change the

XX<-X[,c("resVar",vars,"Year")]

to

assign("XX", X[,c("resVar",vars,"Year")], pos = 1)

This should do the trick, as each cluster runs as a separate process (not a separate thread as far as I know), so you won't have issue with two processes/threads trying to use the XX variable.

I added two extra lines - a set.seed(131) and another line after coefficients(ans),i.e.

set.seed(131)

... rest of your code ....
coefficients(ans)

write(paste0(coefficients(ans)[1],"\n"),file="c:\\temp\\r\\out.txt",append=TRUE)

and got consistently 6 lines in the file (same numbers, but obviously in different order):

0.703727602527463
1.03799340156792
1.15220874833614
1.30381769320552
1.42656613017171
1.77287504108163

That should work for you as well.

Upvotes: 3

Related Questions