store variables from foreach

I have this R code and I have made a function that store the calcluation in store in a matrix but unlike matlab foreach do not store the calcluation in a matrix. How can I do it.

x<-foreach (vertex=1:n,.combine="store",.multicombine=TRUE,.verbose = TRUE) %dopar%
#xfor (vertex in 1:n)
{
print(vertex)
cat(vertex,'\n')
store(model,nomodel,vertex,df,ydata)

}

I am not familiar with the function store(), so I do not know what kind of object it returns. You could assign the output to a list like this:

x <- vector(mode = "list", length = n)
for (vertex in 1:n) {
  x[[vertex]] <- store(model, nomodel, vertex, df, ydata)
}

If you can explain more about what store() does and what you need to do with the output, someone may have a better suggestion.

I would always advise to create a non-parallel version of your code, and get it working to your satisfaction, before then adapting that to use parallel foreach, for everyone less than expert. I've used foreach a fair amount but I still consider myself a beginner.

store<-function(model,nomodel,vertex,df,ydata){ 
  if (min(df[vertex,])>0){
    y=ydata[vertex,]
    model=lmer(y~group+time+sex_+(1|id_),REML= FALSE)
    nomodel=lmer(y~group+time+sex_+group*time+(1|id_),REML= FALSE)
    a=anova(model,nomodel)
    a_[vertex]=a
    print(a)
    #plot(model,select=c(1))
    coefs=data.frame(coef(summary(model)))
    df.KR=get_ddf_Lb(model,fixef(model))
    coefs$p.KR=2*(1-pt(abs(coefs$t.value),df.KR))
    
    print(coefs)
    p[1,vertex]=coefs$p.KR[1]
    p[2,vertex]=coefs$p.KR[2]
    p[3,vertex]=coefs$p.KR[3]
    p[4,vertex]=coefs$p.KR[4]
    p[5,vertex]=coefs$p.KR[5]
    p[6,vertex]=coefs$p.KR[6]
    p<-p
    model_list[vertex]=model
    
    betaVec[vertex]=fixef(model)[3]
    groupdiff[vertex]=fixef(model)[2]
    interaction[vertex]=fixef(model)[5]
    sexeff[vertex]=fixef(model)[4]
    sex_time[vertex]=fixef(model)[6] 
  }else betaVec[vertex]=NA
  groupdiff[vertex]=NA
  interaction[vertex]=NA
  sexeff[vertex]=NA
  sex_time[vertex]=NA
  p[1,vertex]=NA
  p[2,vertex]=NA
  p[3,vertex]=NA
  p[4,vertex]=NA
  p[5,vertex]=NA
  p[6,vertex]=NA
}

I use it for neuroimaging and I want to save in the matrix as I am able to do in matlab parfor

I'm seeing that the function takes inputs and uses them to modify global objects defined elsewhere, I'm doubtful whether this approach/architecture is suitable for parallelizing. I think the function would be better to be more explicit about the object(s) to be modified, passing them in as parameters maybe ? and preparing a single return object containing anything to be passed back. In that way foreach would combine the results.
I don't want to dissuade you from working on this, but I don't think what you are doing is trivial, and will take a lot of thought and experimentation to get right...
It might be that a reprex (of your non-parallelised approach) is the best way to go if you wish to engage this communities competative spirit to engineer up a parallel solution :smiley:
FAQ: How to do a minimal reproducible example ( reprex ) for beginners

I solve it by using bigstatr to enable shared memory and I used flock for locking to prevent R from writing over variables.

I converted all matrix to FBM.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.