Antonio Paredes
2009-Oct-05 13:29 UTC
[R] Long for Loop- calling C from R - Parallel Computing
Hello everyone, I'm running the following for loop to generate random variables in chunks of 60 at a time (l), here h is of order in millions (could be 5 to 6 millions), note that generating all the variables at once could have an impact on the final results for(j in 1:h){ dat$t.o[seq(0,g1,l)[j]+1:l]<-dat$mu[seq(0,g1,l)[j]+1:l] + rnorm(l,0,dat$g.var[seq(0,g1,l)[j]+1:l]) } Is there any way that I can improve on this loop and preserve my objective of generating variable 60 (l) at a time. What about calling C from R, will that be a lot faster. Is this a typical situation designed for parallel computing? My knowledge of looping in R is very weak; but please note, that my interest is not just on a piece of code that solve the problem; if you have a link to a reference that discussed some of these issues let me know. I'm currently reading the R Inferno; but any other reference will be appreciated. Thanks -- -Tony [[alternative HTML version deleted]]
Karl Ove Hufthammer
2009-Oct-05 13:48 UTC
[R] Long for Loop- calling C from R - Parallel Computing
In article <6f6f0fd60910050629p28c99209jcd7836353fd2d754 @mail.gmail.com>, antonioparedes14 at gmail.com says...> I'm running the following for loop to generate random variables in chunks of > 60 at a time (l), here h is of order in millions (could be 5 to 6 millions), > note that generating all the variables at once could have an impact on the > final resultsNo, it will not. See this example code for an illustration: set.seed(1) rnorm(3) rnorm(3) set.seed(1) rnorm(6) So if you generate the six numbers three at a time or all at once gives exactly the same result. So my suggestion is to generate all the numbers at once. That takes next to no time. Or, if it takes too much memory, generate for example a million at once, and repeat a few times. -- Karl Ove Hufthammer
Antonio Paredes
2009-Oct-06 14:10 UTC
[R] Fwd: Long for Loop- calling C from R - Parallel Computing
---------- Forwarded message ---------- From: Antonio Paredes <antonioparedes14@gmail.com> Date: Tue, Oct 6, 2009 at 9:41 AM Subject: Re: [R] Long for Loop- calling C from R - Parallel Computing To: Karl Ove Hufthammer <karl@huftis.org> Hello again, I'm hoping to get a response from some of the R gurus in this list. Is my assumption that R is not designed or build to deal with high levels (a lots of simulated data) simulation correct. For example, how to minimize system time; do one have to call a lower level language like C or Fortran; or just, like many of you have done, do a lots of programing in R and eventually the tricks will be learned. Thanks On Mon, Oct 5, 2009 at 8:35 AM, Antonio Paredes <antonioparedes14@gmail.com>wrote:> In my case it does, because I need to preserved a "high level" of > independence (lack of correlation) among the different groups of 60. Also, > when I say final result I mean computation of standard errors and that > source of stuff; sorry about the lack clarity in my statement. > > > On Mon, Oct 5, 2009 at 9:48 AM, Karl Ove Hufthammer <karl@huftis.org>wrote: > >> In article <6f6f0fd60910050629p28c99209jcd7836353fd2d754 >> @mail.gmail.com>, antonioparedes14@gmail.com says... >> > I'm running the following for loop to generate random variables in >> chunks of >> > 60 at a time (l), here h is of order in millions (could be 5 to 6 >> millions), >> > note that generating all the variables at once could have an impact on >> the >> > final results >> >> No, it will not. See this example code for an illustration: >> >> set.seed(1) >> rnorm(3) >> rnorm(3) >> set.seed(1) >> rnorm(6) >> >> So if you generate the six numbers three at a time or all at once gives >> exactly the same result. >> >> So my suggestion is to generate all the numbers at once. That takes next >> to no time. Or, if it takes too much memory, generate for example a >> million at once, and repeat a few times. >> >> -- >> Karl Ove Hufthammer >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > -Tony >-- -Tony -- -Tony [[alternative HTML version deleted]]