[R] vectorize a power analysis?

Chuck Cleland ccleland at optonline.net
Wed May 12 21:49:13 CEST 2010


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 package

  You 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



More information about the R-help mailing list