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]]
> -----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
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_natasa@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]]