I want to loop over 116 groups (i) to do the follow:
r<- foreach(i=unique(Error$scidx),.combine = c) %dopar% {
numerator=sum(P1new[Error$scidx==i])+sum(P2new[DATA$scid==i])
denominator=sum(P1old[Error$scidx==i])+sum(P2old[DATA$scid==i])
probab=exp(numerator-denominator)
if (runif(1)<probab){
Change=1
} else{
Change=0
}
list(i,Change)
}
I am requesting 4 cores and it takes me long to run each time. I suspect it is because the data Error, P1new, P1old contain 700,000 observations. Is it true? Is there any way I can speed up the process? Or, my code has something wrong?
Thanks.