0

I have to run thousands of models with thousands of outcomes. So I selected "foreach" and "doparallel" package to increase my speed. For the packages to analyze my data, I chose two different packages: "MASS" package and "lqmm" package. "MASS" package worked well, but "lqmm" package always came up with error. But I am thinking there might ways to deal with this issue by editing some statements in foreach function. I have searched all related questions in this aspect, but still get stuck. I hope someone could help me with it. Thanks in advance.

I tried to add ".packages" in the foreach function.

# read packages
library(foreach)
library(doParallel)
library(Formula)

# Parallel 
cl <- makeCluster(5)
registerDoParallel(cl)
results <- foreach (i=1:10,.combine='rbind',.packages=c('lqmm')) %dopar% {
model <- lqmm(((eval(data)[,i] ~ AGE)), random=~1, group=id, data=data)
exposure="AGE"
pvalue=summary(model)$tTable[2.5]
want <- cbind(exposure, pvalue)
}
stopCluster(cl)

But I still got this error: Error in {: task 1 failed - "object 'i' not found"

I am guessing foreach doesn't understand i here (i.e., 1 to 10), but I don't know how to make foreach understand i here. It's not a variable/object, it's a loop changing from 1 to 10.

Can someone help me with it? Thank you.

Olivia1289
  • 11
  • 2
  • 1
    What's with the `eval(data)[,i]`? Where is `data` coming from? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You should pass in a proper formula rather than using `eval()`. Use `reformulate()` for help. – MrFlick Apr 06 '19 at 04:29
  • 1
    Are you sure about that `[2.5]` in there? I suspect it should be `[2,5]`. – DanY Apr 06 '19 at 05:30
  • Hi, sorry for my unclear format. I reposted my question. Please see this link if you're free. Thanks a lot! https://stackoverflow.com/questions/55578925/how-to-fix-object-i-not-found-in-r-foreach – Olivia1289 Apr 08 '19 at 17:43
  • It should be [2,5] not [2.5]. I don't know why it changed when I copied and pasted it. It is still shown in my new poster. But I added in my comments there. Thank you. – Olivia1289 Apr 08 '19 at 17:45

0 Answers0