Hello, Is there a fancy way I can use a function (like apply) to avoid a the follwoing loop, or otherwise make this more efficient? It fills a 3-d array, one matrix at a time. "nsize" is big, and efficiency is important. for (i in 1:nsize) simy[,,i] <- myfunction(p1,p2, ...) Thanks! Michael J. Roberts Resource Economics Division, PMT USDA-ERS 202-694-5557 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 29 Jan 2001, Michael Roberts wrote:> Hello, > > Is there a fancy way I can use a function (like apply) > to avoid a the follwoing loop, or otherwise make this more > efficient? It fills a 3-d array, one matrix at a time. > "nsize" is big, and efficiency is important. > > for (i in 1:nsize) simy[,,i] <- myfunction(p1,p2, ...) >You will need to tell us a lot more. At present it appears that you are doing the same calculation nsize times, which is of course easy to avoid. Avoiding loops in R is by no mean always useful or effective. There are myths about that go back to long-forgotten versions of S, in which for loops were very much to be avoided. Up to a point *vectorization* is worthwhile, the point being where handling large vectors becones expensive. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Sorry, I can now see why I needed to provide more information. I am trying to use simulation to estimate a likelihood function that cannot be evaluated explicitly: a multinomial probit model with a spatail autocorrelation parameter for each latent dependent variable. "myfunction" generates a single latent observation (for all latent dependent variables less one) for a parameter guess. Clearly, I need to do this many times to get an accurate estimate of the likelihood. Is a loop the best way to do this? Thanks much, Michael J. Roberts Resource Economics Division, PMT USDA-ERS 202-694-5557>>> Prof Brian D Ripley <ripley at stats.ox.ac.uk> 01/29 1:16 PM >>>On Mon, 29 Jan 2001, Michael Roberts wrote:> Hello, > > Is there a fancy way I can use a function (like apply) > to avoid a the follwoing loop, or otherwise make this more > efficient? It fills a 3-d array, one matrix at a time. > "nsize" is big, and efficiency is important. > > for (i in 1:nsize) simy[,,i] <- myfunction(p1,p2, ...) >You will need to tell us a lot more. At present it appears that you are doing the same calculation nsize times, which is of course easy to avoid. Avoiding loops in R is by no mean always useful or effective. There are myths about that go back to long-forgotten versions of S, in which for loops were very much to be avoided. Up to a point *vectorization* is worthwhile, the point being where handling large vectors becones expensive. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi Michael,> I am trying to use simulation to estimate a likelihood function > that cannot be evaluated explicitly: a multinomial probit model > with a spatail autocorrelation parameter for each latent dependent > variable. "myfunction" generates a single latent observation (for > all latent > dependent variables less one) for a parameter guess. Clearly, I > need > to do this many times to get an accurate estimate of the likelihood. > > Is a loop the best way to do this?A loop may be the best way to do this, but of course there are loops and loops. Rather than create your simy array explicitly and then assign into its third extent, you might find it quicker to create simy "on the hoof" and then reshape it. For example, something like simy <- lapply(1:nsize, function(i) myfunction(p1, p2, i)) simy <- array(unlist(simy), c(nx, ny, nsize)) will often turn out to be faster. Cheers, Jonathan. Jonathan Rougier Science Laboratories Department of Mathematical Sciences South Road University of Durham Durham DH1 3LE tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388 http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._