Re: [R] efficient code. how to reduce running time?

From: John Fox <jfox_at_mcmaster.ca>
Date: Mon 22 Jan 2007 - 01:11:19 GMT


Dear Haris,

Using lapply() et al. may produce cleaner code, but it won't necessarily speed up a computation. For example:

> X <- data.frame(matrix(rnorm(1000*1000), 1000, 1000))
> y <- rnorm(1000)
>
> mods <- as.list(1:1000)
> system.time(for (i in 1:1000) mods[[i]] <- lm(y ~ X[,i]))
[1] 40.53 0.05 40.61 NA NA
>
> system.time(mods <- lapply(as.list(X), function(x) lm(y ~ x)))

[1] 53.29 0.37 53.94 NA NA

In cases such as this, I don't even find the code using *apply() easier to read.

Regards,
 John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of
> Charilaos Skiadas
> Sent: Sunday, January 21, 2007 7:01 PM
> To: miraceti
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] efficient code. how to reduce running time?
>
> On Jan 21, 2007, at 5:55 PM, miraceti wrote:
>
> > Thank you all for lookin at it.
> > I'll fix the code to preallocate the objects.
> > and I wonder if there is a way to call anova on all the
> columns at the
> > same time..
> > Right now I am calling (Y~V1, data) from V1 to V50 thru a loop.
> > I tried (Y~., data) but it gave me different values from
> the results I
> > get when I call them separately, So I can't help but call
> them 25,000
> > times...
>
> Have you looked at lapply, sapply, apply and friends?
>
> Haris
>
> ______________________________________________
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Mon Jan 22 12:17:14 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 22 Jan 2007 - 15:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.