We are doing a power analysis by generating noisy data sets according to a model, fitting the model to the data, and extracting a p-value. What is the best way to do this many times? We are just using for loops and it is too slow because we are repeating the analysis for many parameterizations. I can think of several ways to do this: for loop sapply using the plyr package using the lme4 package Someone told me that the apply functions are barely any faster than for loops, so what is the best way, in general, to approach this type of problem in R-style? Could someone point to a discussion of the comparative time efficiencies of these and other appropriate methods? I'm not looking for specific code, just sort of a list of the common approaches with information about efficiency. Thanks, Jack -- View this message in context: http://r.789695.n4.nabble.com/vectorize-a-power-analysis-tp2196647p2196647.html Sent from the R help mailing list archive at Nabble.com.
On 5/12/2010 3:34 PM, Jack Siegrist wrote:> We are doing a power analysis by generating noisy data sets according to a > model, fitting the model to the data, and extracting a p-value. What is the > best way to do this many times? We are just using for loops and it is too > slow because we are repeating the analysis for many parameterizations. I can > think of several ways to do this: > > for loop > sapply > using the plyr package > using the lme4 packageYou don't mention replicate(), which I would consider. For example: replicate(10, t.test(rnorm(20))$p.value) [1] 0.2622419 0.1538739 0.9759340 [4] 0.7413474 0.1541895 0.4321595 [7] 0.5800549 0.7329299 0.9625038 [10] 0.1315875 If you write a function that does the data generating, model fitting, and p-value extraction, then replicate can run that function many times. I don't know how the timing compares, but I like the simplicity and readability of replicate(). hope this helps, Chuck> Someone told me that the apply functions are barely any faster than for > loops, so what is the best way, in general, to approach this type of problem > in R-style? > Could someone point to a discussion of the comparative time efficiencies of > these and other appropriate methods? > > I'm not looking for specific code, just sort of a list of the common > approaches with information about efficiency. > > Thanks, > > Jack-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
You should profile (Rprof) your code to see where it is spending it time; this will point you to what needs to be optimized. On Wed, May 12, 2010 at 3:34 PM, Jack Siegrist <jacksie@eden.rutgers.edu>wrote:> > We are doing a power analysis by generating noisy data sets according to a > model, fitting the model to the data, and extracting a p-value. What is the > best way to do this many times? We are just using for loops and it is too > slow because we are repeating the analysis for many parameterizations. I > can > think of several ways to do this: > > for loop > sapply > using the plyr package > using the lme4 package > > Someone told me that the apply functions are barely any faster than for > loops, so what is the best way, in general, to approach this type of > problem > in R-style? > Could someone point to a discussion of the comparative time efficiencies of > these and other appropriate methods? > > I'm not looking for specific code, just sort of a list of the common > approaches with information about efficiency. > > Thanks, > > Jack > -- > View this message in context: > http://r.789695.n4.nabble.com/vectorize-a-power-analysis-tp2196647p2196647.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<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 that you are trying to solve? [[alternative HTML version deleted]]