Hi:

Avoiding loops in R by using vectorization is usually one of the best ways
to improve performance. Since we can't replicate your data (e.g., tslength
is not given), instead I'll generate a couple of functions to extract the
estimated beta from randomly generated data. The replicate() function comes
in handy for this type of simulation.

(1) Create a function to construct the sample and run the model:
wf <- function() {
   x <- sample(1:20, 10)
   y <- 2 + 1.5 * x + rnorm(20)
   summary(lm(y ~ x))$coef[2]
   }
system.time(replicate(1000, wf()))
   user  system elapsed
   3.17    0.00    3.17

(2) Use a loop instead of replicate():
b <- numeric(1000)
system.time(for(i in seq_along(b)) b[i] <- wf())
   user  system elapsed
   3.17    0.00    3.18

(3) Use lsfit() instead (only works for simple linear regression):
wf2 <- function() {
     x <- sample(1:20, 10)
     y <- 2 + 1.5 * x + rnorm(10)
    lsfit(x, y)$coef[2]
  }
system.time(replicate(1000, wf2()))
   user  system elapsed
   0.45    0.00    0.46

Hopefully you can adapt one or more of these functions to your needs.
Dennis

On Fri, Jul 30, 2010 at 1:18 AM, Natasa Tzortzaki <tz_nat...@hotmail.com>wrote:

>
> hello,
>
> i am new to R and trying to calculate the beta coefficient for standard
> linear regression for a series of randomly generated numbers. I have created
> this loop, but it runs really slow, is there a way to improve it?
>
> #number of simulations
> n.k<-999
> #create the matrix for regression coefficients generated from #random data
>
> beta<-matrix(0,1,n.k+1)
> e<-matrix(0,tslength,n.k+1)
>
>
> for(k in 1:n.k+1)
>   {
>    for(i in 1:tslength)
>       {
>        beta[1,1]<-beta1
>        e[i,k]<-c(rnorm(1,0,var.all))
>        beta[1,k]<-summary(lm(e[1:tslength,k]~t))$coefficient[2]
>       }
>   }
> thanks
> Anastasia Tzortzaki
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to