# Performing a GLS cross-sectional regression using R

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".

-
Is this not what gls in nlme does? –  Peter Ellis Aug 7 '12 at 20:07
@ 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 –  user13144 Aug 7 '12 at 20:53
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. –  Peter Ellis Aug 7 '12 at 22:58
@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 –  user13144 Aug 7 '12 at 23:20

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.

-