Is there a function in R that could perform GLS cross-sectional regression for multiple cases all at once?

For example, when regressing stock returns over beta for 100 stocks over a 10-year time series for each case. However, the equation should solve all of the 100 regressions and give only one result, a "multiple regression".

  • $\begingroup$ Is this not what gls in nlme does? $\endgroup$ – Peter Ellis Aug 7 '12 at 20:07
  • $\begingroup$ @ Peter: yes the nlme package contains functions for gls. However, I couldn't find out how to make the regression in a form similar to: get regression for stock i in month t for all the cases $\endgroup$ – user13144 Aug 7 '12 at 20:53
  • $\begingroup$ Maybe if you describe how your data is structured eg what columns and rows, is it one rectangle of data or 100, etc. That might help. This sounds like a coding question how to use gls() or data management in R rather than a statistical question. $\endgroup$ – Peter Ellis Aug 7 '12 at 22:58
  • $\begingroup$ @PeterEllis: the rows are the dates whereas the columns are like the following: first 100 columns are(the dependent variable), followed by 100 columns (independent variable1)then another 100 for (independent variable 2). the statistical model looks like the following: dependent variable= intercept+ sum(gls estimator(y) x independent variable1 x Independent variable2)+error term $\endgroup$ – user13144 Aug 7 '12 at 23:20
  • $\begingroup$ look at this PDF file in CRAN: Econometrics in R there is chapter "Cross Sectional Regression" on page 16. $\endgroup$ – Stas Prihod'co Aug 16 '15 at 20:38

You should first use melt() or reshape() from the reshape or reshape2 libraries to convert your data into a long form with columns for:

  • Stock value
  • Stock id (a factor with 100 levels)
  • IV1
  • IV2
  • Date of observation

This will have 100 times as many rows as your current data rectangle.

Then you can use lme(), specifying whatever error structure (autocorrelation, etc) as well as grouping by stock id.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy