s-news
[Top] [All Lists]

Re: Many regressions

To: <S-news@wubios.wustl.edu>
Subject: Re: Many regressions
From: "Quinn, David" <David.Quinn@nrgenergy.com>
Date: Thu, 20 Jun 2002 18:07:36 -0500
Cc: "Quinn, David" <David.Quinn@nrgenergy.com>
Thread-index: AcIYjhPfa597XLokSA6Xh3vmSIdiCAADDqAQAAUdY5A=
Thread-topic: [S] about aggregate
Thanks to Brad Biggerstaff, Jim Pratt, and Tim Hesterberg and Jeffrey Wang of 
Insightful for their answers.  I think the easiest for what I want to do wil be 
what Tim and Jeffrey both suggested: use the linear regression formulas and use 
vectorization to stack the thousands of them up.  Here is Tim's reply; 
Jeffrey's is similar except that he pre-computes the means of the second order 
terms along with the linear terms.

Use the basic formulae
beta1 = sum( (y-ybar)(x-xbar) ) / sum( (x-xbar)^2 )
beta0 = ybar - beta1 xbar
Put your x's and y's in columns of matrices, and operate on columns.
Here's an example with 30 columns:
set.seed(0)
x _ rmvnorm(20, d0)
y _ rmvnorm(20, d0)

xbar _ colMeans(x)
ybar _ colMeans(y)
b1 _ colSums( (x - rep(xbar, each=nrow(x))) * y) / colVars(x, SumSquares=T)
b0 _ ybar - b1 * xbar

lm(y[,1]~x[,1])  # note the coefficients
c(b0[1], b1[1])  # these match the previous
# b0 and b1 are vectors containing the intercept and slope coefficients.

========================================================
| Tim Hesterberg       Research Scientist              |
| timh@insightful.com  Insightful Corp.                |
| (206)802-2319        1700 Westlake Ave. N, Suite 500 |
| (206)283-6310 (fax)  Seattle, WA 98109-3044, U.S.A.  |
|                      www.insightful.com/Hesterberg   |


 -----Original Message-----
From:   Quinn, David  
Sent:   Thursday, June 20, 2002 5:10 PM
To:     S-news@wubios.wustl.edu
Cc:     Quinn, David
Subject:        [S] Many regressions


I want to run several thousand independent linear regressions of the form

lm(y~x)

without building a loop, which runs too slow.  Each pair of x and y vectors 
generates an independent pair of regression intercept and slope.  Is there a 
way to vectorize this?

Thanks,
David

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



<Prev in Thread] Current Thread [Next in Thread>