[R] (no subject)

Daniel Nordlund djnordlund at verizon.net
Fri Jul 30 11:59:55 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Natasa Tzortzaki
> Sent: Friday, July 30, 2010 1:19 AM
> To: r-help at r-project.org
> Subject: [R] (no subject)
> 
> 
> 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
> 

Anastasia,

You haven't provided a reproducible example, so I am going to guess at some of what you are doing.  You also have used some function names for your variable names which I like to avoid, so I have used 'b' as the name of the vector for collecting beta coefficients and 'ts' for the predictor variable.  I have set var.all equal to 1.

To speed up your process, you want to eliminate looping as much as possible.  In addition, you should pre-allocate space for vectors that you are going to fill up in a loop.  Your e values can all be generated at once outside of your loop.  I also used the coef() extractor function directly on the lm() object.  So, you could do something like the following:

# number of simulations
n.k <- 999

# predictor variable
ts <- 1:100

tslength <- length(ts)
var.all <- 1

# pre-allocate vector to collect beta coefficients
b <- numeric(n.k)

# generate your e values all at once
# columns contain values for each simuation trial 
e <- matrix(rnorm(n.k*tslength,0,var.all), ncol = n.k)

# run your analyses
for(k in 1:n.k) 
  {
   b[k] <- coef(lm(e[,k] ~ ts))[2]
  }

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA



More information about the R-help mailing list