I'm attempting to run the following code (used for EWAS purposes) in a parallel format:
registerDoParallel(2)
combined <- foreach(i = 28:78, .combine=rbind) %dopar%
{
mm<-rep(NA,412)
FIDunique<-unique(pheno$FID)
for(j in FIDunique)
{
nn<-which(pheno1$FID==j)
mm[nn]<-mean(pheno1[nn,i])
}
tt4<-which(!is.na(mm))
if(length(tt4)>19 & length(table(pheno1[,i]))>1)
{
meth<-pheno1[tt4,i]
mm<-mm[tt4]
if(length(levels(factor(pheno1$smoking)))<2)
{
lme2<-tryCatch(lme(I(meth-mm)~delta_residSpine+I(DNA_year-AGE_year)+CD8T+CD4T+NK+Bcell+Mono+Gran,random=~1|FID, control=lmeControl(msMaxIter=50000,opt="optim"), data=pheno1[tt4,]),error = function(e) rep (NaN,1))
}
else
{
lme2<-tryCatch(lme(I(meth-mm)~delta_residSpine+I(DNA_year-AGE_year)+factor(smoking)+CD8T+CD4T+NK+Bcell+Mono+Gran,random=~1|FID, control=lmeControl(msMaxIter=50000,opt="optim"), data=pheno1[tt4,]),error = function(e) rep (NaN,1))
}
if(!is.na(lme2)) out18[i-27,2:5]<-summary(lme2)$tTable[2,-3]
rm(mm,nn,meth,lme2)
}
out18[i-27,6]<-length(tt4)
out18[i-27,]
}
The issue i'm having is that when I use %do% in the second line, the program executes perfectly and 'combined' contains the the correct output.
However when I use %dopar% (as is shown in the code above), the middle four columns of combined dissapear. I suppose this is due to those four columns being assigned based on a tryCatch? I've been stuck on this for a few days and really need to make progress.
It's also worth nothing that when I run the program as a %do%, I get the warning
Warning in if (!is.na(lme2)) out18[i - 27, 2:5] <- summary(lme2)$tTable[2, : the condition has length > 1 and only the first element will be used
for every iteration, however I do not get this at all when running under %dopar%.
Apologies if I haven't given enough information on what the variables are, i'm trying to disclose as little as possible about the actual information being processed for ethical reasons.