[R] Parallel programming.
Art U
art.tem.us at gmail.com
Thu Mar 30 00:57:22 CEST 2017
Hello everyone,
I have general question about parallel programming. I'm doing simulations
that involves bootstrap. For now I'm using parLapply only for bootstrap
part and then function "replicate" for simulations. It is very long
function, so here is a short version:
simfun=function(data,n,alpha){
#code{}
cl <- makeCluster(detectCores())
clusterExport(cl,c("data"), envir=environment())
clusterExport(cl,c("n"), envir=environment())
q=1000;
#Bootstrap - zero-corrected stepwise selection
repl=*parLapply*(cl=cl,1:q,function(i,dataB=data,smpl=n,...){
dataB <- dataB[sample(nrow(dataB),size=n,replace=TRUE),]
m1=glm(y~x1+x2+x3+t,family=binomial,data=dataB)
S=step(m1,direction="backward",trace=F);
Sstep=summary(S);Sstep
if (any(rownames(Sstep$coefficients)=="t")==TRUE){
beta.Step.zero=Sstep$coefficients[paste0("t"), ][1]
}else{
beta.Step.zero=0;
}
return(beta.Step.zero)
})
stopCluster(cl)
beta.Step.zero.v=(do.call("rbind", repl));
beta.Step.zero=mean(beta.Step.zero.v);beta.Step.zero
lower.step.zero=quantile(beta.Step.zero.v, c(.025,
.975))[1];lower.step.zero
upper.step.zero=quantile(beta.Step.zero.v, c(.025,
.975))[2];upper.step.zero
Ind.Step.zero=as.integer(bt > lower.step.zero & bt < upper.step.zero);
Len.Step.zero=upper.step.zero-lower.step.zero;
#more code{}
return(list(Ind.Step.zero=Ind.Step.zero,Len.Step.zero=Len.Step.zero,lower.step.zero=lower.step.zero,beta.Step.zero=beta.Step.zero,upper.step.zero=upper.step.zero))
}
simN=1000;
repl=*replicate*(simN,simfun(data=data,n=n,alpha=0.025))
For now this code works in waves, each step in replicate it starts to use
all CPU and then stops. Is there the way to convert such function to 100%
parallel programming function? Or any way to make it faster?
Thank you in advance.
Ariel
--
*I like to pretend I'm alone*. *Completely alone*. *Maybe post-apocalypse
or plague*... *Whatever*. *No-one left to act normal for. No need to hide
who I really am. It would be... freeing*. *...*
[[alternative HTML version deleted]]
More information about the R-help
mailing list