Dear useRs, Suppose we have loop: res <- c() for (i in 1:100000) { x <- rnorm(2) res <- c(res,x[2]-x[1]) } and this loop for 10^5 cases runs about - for example 5 minutes. When I add one zero (10^6) this loop will not end overnight but probably hangs. This occurs regardless of calculated statistics in such simulation, always above 10^5 times. Nested loops do not help. Any suggestions for collecting larger amount of Monte Carlo data ? Regards Minimax
have you tried res <- rnorm(10^6)-rnorm(10^6) ? it take 0.5 sec for me... b On Feb 28, 2008, at 3:30 PM, Minimax wrote:> Dear useRs, > > Suppose we have loop: > > res <- c() > for (i in 1:100000) { > x <- rnorm(2) > res <- c(res,x[2]-x[1]) > } > > and this loop for 10^5 cases runs about - for example 5 minutes. > > When I add one zero (10^6) this loop will not end overnight but > probably > hangs. This occurs regardless of calculated statistics in such > simulation, always above 10^5 times. Nested loops do not help. > > Any suggestions for collecting larger amount of Monte Carlo data ? > > Regards > > Minimax > > ______________________________________________ > R-help at 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.
If you really want to do a loop, then preallocate your storage. You were dynamically allocating each time through (or there abouts):> system.time({+ res <- numeric(100000) + for (i in 1:100000) { + x <- rnorm(2) + res[i] <- x[2] - x[1] + } + }) user system elapsed 2.75 0.02 3.10 On Thu, Feb 28, 2008 at 3:30 PM, Minimax <minimax at hell.org> wrote:> Dear useRs, > > Suppose we have loop: > > res <- c() > for (i in 1:100000) { > x <- rnorm(2) > res <- c(res,x[2]-x[1]) > } > > and this loop for 10^5 cases runs about - for example 5 minutes. > > When I add one zero (10^6) this loop will not end overnight but probably > hangs. This occurs regardless of calculated statistics in such > simulation, always above 10^5 times. Nested loops do not help. > > Any suggestions for collecting larger amount of Monte Carlo data ? > > Regards > > Minimax > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? Tell me what you want to do, not how you want to do it.