简体   繁体   English

R:按组进行线性回归(2 个变量)

[英]R: Linear Regression By Group (2 variables)

I have a dataset of corporate fundamentals.我有一个企业基本面数据集。 I want to regress variable SALE on XOPR and PPEGT by industry-year (industry is SIC ).我想按行业年份对XOPRPPEGT的变量SALE进行回归(行业是SIC )。

So I need the coefficients (and the coefficients only) on the following: SALE ~ XOPR, PPEGT by SIC and date .所以我需要以下系数(和系数): SALE ~ XOPR, PPEGT by SICdate I am really not sure how to do this in R. I tried using the data.table package but I'm having trouble figuring out how to regress by two variables.我真的不知道如何在 R 中做到这一点。我尝试使用 data.table 包,但我无法弄清楚如何通过两个变量进行回归。

Can anyone help me out?谁能帮我吗?

Thanks very much!非常感谢!

Use dplyr package to group by使用 dplyr 包进行分组

library(dplyr)
##rough-syntax is as follows
grouped_data = orginal_data %>% group_by(SIC, Date)  

library(forecast)
pred <- lm(SALE ~ XOPR + PPEGT)
pred #should give you the output of coefficients

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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