简体   繁体   中英

Regression coefficients by group in dataframe R

I have data of various companies' financial information organized by company ticker. I'd like to regress one of the columns' values against the others while keeping the company constant. Is there an easy way to write this out in lm() notation?

I've tried using:

reg <- lmList(lead2.dDA ~ paudit1 + abs.d.GINDEX + logcapx + logmkvalt + 
              logmkvalt2|pp, data=reg.df)

where pp is a vector of company names, but this returns coefficients as though I regressed all the data at once (and did not separate by company name).

A convenient and apparently little-known syntax for estimating separate regression coefficients by group in lm() involves using the nesting operator, / . In this case it would look like:

reg <- lm(lead2.dDA ~ 0 + pp/(paudit1 + abs.d.GINDEX + logcapx + 
          logmkvalt + logmkvalt2), data=reg.df)

Make sure that pp is a factor and not a numeric . Also notice that the overall intercept must be suppressed for this to work; in the new formulation, we have a different "intercept" for each group.

A couple comments:

  • Although the regression coefficients obtained this way will match those given by lmList() , it should be noted that with lm() we estimate only a single residual variance across all the groups, whereas lmList() would estimate separate residual variances for each group.
  • Like I mentioned in my earlier comment, the lmList() syntax that you gave looks like it should have worked. Since you say it didn't, this leads me to expect that really the problem is something else (although it's hard to tell what without a reproducible example), and so it seems likely that the solution I posted will fail for you as well, for the same unknown reasons. If you want more detailed guidance, please provide more information; help us help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM