0

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.

  • Please provide an example of the data you are working on. Few rows would be good. Provide `dput()` of your data frame. – YOLO Mar 24 '18 at 04:47
  • And, P1new, P1old contains a column vector of 700,000 observations – Bobby W Chung Mar 24 '18 at 17:05
  • For "Error": structure(list(.......), .Names = c("fromid", "toid", "rowid", "scidx", "toerror1old", "toerror2old", "toerror1new", "toerror2new", "scidy", "fromerror1old", "fromerror2old", "fromerror1new", "fromerror2new", "differror1old", "differror1new", "differror2old", "differror2new" ), sorted = "fromid", class = c("data.table", "data.frame"), row.names = c(NA, -2L), .internal.selfref = ) – Bobby W Chung Mar 24 '18 at 17:09
  • For "DATA": structure(list(......), .Names = c("scid", "aid", "Y", "boy_mumdegree", "boy_daddegree", "girl_mumdegree", "girl_daddegree", "peergpa", "peergender", "peerage", "peerblack", "peerother", "parentboth", "gpa", "grade", "female", "age", "black", "other", "multirace", "mum_degree", "mum_pro", "dad_degree", "dad_pro", "comm_race", "comm_crime", "income", "miss_pdegree", "miss_ppro", "parent_both", "degree", "degreewhite", "degreeblack", "degreeother", "Freq" ), row.names = 7:10, class = "data.frame") – Bobby W Chung Mar 24 '18 at 17:10
  • please add dput(DATA) and dput(Error) in the question. – YOLO Mar 24 '18 at 17:24
  • Sorry, not sure I understood. The output I attached are the results of dput(Error) and dput(DATA). Am I missing something? Thanks again – Bobby W Chung Mar 25 '18 at 17:41

0 Answers0