Hi there, I am running Monte Carlo Simulations in R using ordinary "while (condition)" loops. Since the number of iterations is something like 100.000 and within each iteration a given subsample is extended sequentially it takes hours to run the simulation. Does anyone know if there is either a way to avoid using loops in Monte Carlo Simulations or how to include possible faster "c++" commands in R code? many thanks in advance. Nael Al-Anaswah ----------------------------------------------------- Nael Al-Anaswah Department of Econometrics University of Muenster Germany
On Tue, 28 Sep 2004 13:46:04 +0200, "Nael Al Anaswah" <Nael-Al.Anaswah at WIWI.UNI-MUENSTER.DE> wrote :>Hi there, > >I am running Monte Carlo Simulations in R using ordinary "while >(condition)" loops. Since the number of iterations is something like >100.000 and within each iteration a given subsample is extended >sequentially it takes hours to run the simulation. > >Does anyone know if there is either a way to avoid using loops in >Monte Carlo Simulations or how to include possible faster "c++" >commands in R code?It'll be a lot faster if you assign your storage at the start: - a 100000 long vector to hold the results at the start - enough space to hold a full iteration at the start Extending vectors is slow, because it requires a new allocation and a copy operation. The Writing R Extensions manual talks about linking C, C++ or Fortran code into R. It'll likely be faster, but if you need to allocated R storage from within it, there's a bit of a learning curve. Duncan Murdoch
Hi! Have you taken a look at the MCMCpack - package on cran: "This package contains functions for posterior simulation for a number of statistical models. All simulation is done in compiled C++ written in the Scythe Statistical Library Version 1.0. All models return coda mcmc objects that can then be summarized using the coda package." /E Nael Al Anaswah wrote:>Hi there, > >I am running Monte Carlo Simulations in R using ordinary "while >(condition)" loops. Since the number of iterations is something like >100.000 and within each iteration a given subsample is extended >sequentially it takes hours to run the simulation. > >Does anyone know if there is either a way to avoid using loops in >Monte Carlo Simulations or how to include possible faster "c++" >commands in R code? > >many thanks in advance. > >Nael Al-Anaswah > > > >----------------------------------------------------- >Nael Al-Anaswah >Department of Econometrics >University of Muenster >Germany > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >-- Dipl. bio-chem. Witold Eryk Wolski MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin _ tel: 0049-30-83875219 'v' http://www.molgen.mpg.de/~wolski / \ mail: witek96 at users.sourceforge.net ---W-W---- wolski at molgen.mpg.de
The key is to assign space in advance -- e.g., compare: > N <- 20000 > > res <- NULL > system.time( for(i in 1:N) res <- c(res, sample(10)) ) [1] 28.62 8.91 37.79 0.00 0.00 > > res <- vector("list",N) > system.time( for(i in 1:N) res[[i]] <- sample(10) ) [1] 0.45 0.00 0.44 0.00 0.00 > > res <- matrix(0.0, N,10) > system.time( for(i in 1:N) res[i,] <- sample(10) ) [1] 0.47 0.01 0.47 0.00 0.00 > Gardar At 01:46 PM 9/28/2004 +0200, Nael Al Anaswah wrote:>Hi there, > >I am running Monte Carlo Simulations in R using ordinary "while >(condition)" loops. Since the number of iterations is something like >100.000 and within each iteration a given subsample is extended >sequentially it takes hours to run the simulation. > >Does anyone know if there is either a way to avoid using loops in >Monte Carlo Simulations or how to include possible faster "c++" >commands in R code? > >many thanks in advance. > >Nael Al-Anaswah > > > >----------------------------------------------------- >Nael Al-Anaswah >Department of Econometrics >University of Muenster >Germany > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html