Heng Sun
2005-Oct-14 15:55 UTC
[R] run many linear regressions against the same independent variables in batch
R function lm(response ~ term) allows me to run a linear regression on a single response vector. For example, I have recent one year historical prices for a stock and S&P index. I can run regression of the stock prices (as response vector) against the S&P index prices (as term vector). Now assume I have 1000 stocks to run the above regressions (against the same S&P index prices). The only way I know is that I write a loop. Within each loop I do the regression for one stock price. Is there a batch method to run the 1000 regressions in one shot? Note that this functionality is available in SAS (the SAS procedure "reg"). Actually, some times we run such regressions for about 300K securities. Performing regressions in loop takes a long time. On the contrary, running on SAS is much faster. Thank you in advance. Heng Sun 212-855-5754 Director Quantitative Risk Depository Trust and Clearing Corporation [[alternative HTML version deleted]]
Gabor Grothendieck
2005-Oct-14 16:05 UTC
[R] run many linear regressions against the same independent variables in batch
This runs a regression of each column (except the first) of matrix state.x77 against the first: lm(state.x77[,-1] ~ state.x77[,1]) On 10/14/05, Heng Sun <HSun at dtcc.com> wrote:> R function > lm(response ~ term) > allows me to run a linear regression on a single response vector. For > example, I have recent one year historical prices for a stock and S&P > index. I can run regression of the stock prices (as response vector) > against the S&P index prices (as term vector). > > Now assume I have 1000 stocks to run the above regressions (against the > same S&P index prices). The only way I know is that I write a loop. Within > each loop I do the regression for one stock price. > > Is there a batch method to run the 1000 regressions in one shot? Note that > this functionality is available in SAS (the SAS procedure "reg"). > > Actually, some times we run such regressions for about 300K securities. > Performing regressions in loop takes a long time. On the contrary, running > on SAS is much faster. > > Thank you in advance. > > Heng Sun > 212-855-5754 > > Director > Quantitative Risk > Depository Trust and Clearing Corporation > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 >
Huntsinger, Reid
2005-Oct-14 16:05 UTC
[R] run many linear regressions against the same independent variables in batch
I have always used lsfit() for this, but have been told that lm() is preferred, and I note the help for lm() states If 'response' is a matrix a linear model is fitted separately by least-squares to each column of the matrix. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Heng Sun Sent: Friday, October 14, 2005 11:56 AM To: r-help at stat.math.ethz.ch Subject: [R] run many linear regressions against the same independent variables in batch R function lm(response ~ term) allows me to run a linear regression on a single response vector. For example, I have recent one year historical prices for a stock and S&P index. I can run regression of the stock prices (as response vector) against the S&P index prices (as term vector). Now assume I have 1000 stocks to run the above regressions (against the same S&P index prices). The only way I know is that I write a loop. Within each loop I do the regression for one stock price. Is there a batch method to run the 1000 regressions in one shot? Note that this functionality is available in SAS (the SAS procedure "reg"). Actually, some times we run such regressions for about 300K securities. Performing regressions in loop takes a long time. On the contrary, running on SAS is much faster. Thank you in advance. Heng Sun 212-855-5754 Director Quantitative Risk Depository Trust and Clearing Corporation [[alternative HTML version deleted]] ______________________________________________ R-help at 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