简体   繁体   English

使用 plm 固定效果回归

[英]Fixed effects regression with plm

The code below runs the time series regression "excessr ~ mkt_rf" with biglm, because the function lm does not work with my real dataset.下面的代码使用 biglm 运行时间序列回归“excessr ~ mkt_rf”,因为函数 lm 不适用于我的真实数据集。

Now I would like to switch from biglm to plm to account for fixed effects.现在我想从 biglm 切换到 plm 以解决固定效应。 Unfortunately plm doesn't work.不幸的是 plm 不起作用。

Does anyone know what I could change that plm work ?有谁知道我可以改变 plm 的工作吗? Thanks in advance!提前致谢!

library(biglm)
library(plm)
library(data.table)

union_with_factors = data.table(
  t = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
  excessr  = c(10,23,13,53,43,76,34,12,45,13,42,31,4,53,64),
  FundId = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
  mkt_rf = c(1,1,2,1,3,1,1,2,1,3,1,1,2,1,3)
)

sp <- split(union_with_factors, union_with_factors$FundId)
beta <- sapply(sp, function(tmp){
  fit <- plm(excessr ~ mkt_rf, data = tmp)
  coef(fit)
})

Supposing that the ID of individuals is given by FundId and that time ID is given by t , here's how you can apply a fixed effects regression:假设个人的 ID 由FundId给出,时间 ID 由t给出,以下是应用固定效应回归的方法:

library(biglm)
library(data.table)
library(plm)

union_with_factors = data.table(
  t = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
  excessr  = c(10,23,13,53,43,76,34,12,45,13,42,31,4,53,64),
  FundId = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
  mkt_rf = c(1,1,2,1,3,1,1,2,1,3,1,1,2,1,3)
)
fit <- plm(excessr ~ mkt_rf, 
             data = union_with_factors, 
             index = c("FundId", "t"), 
             model = "within")
summary(fit)
fixef(fit)

See here and in the plm documentation ( ?plm in console) for more details有关更多详细信息,请参阅此处plm文档(控制台中的?plm

Edit: following this post and this article , it appears that you can do a Fama-MacBeth regression with pmg (also in the plm package):编辑:这篇文章这篇文章之后,您似乎可以使用pmg (也在plm包中)进行 Fama-MacBeth 回归:

fama_macbeth <- pmg(excessr ~ mkt_rf, 
                   data = union_with_factors, 
                   index = c("FundId", "t"))
summary(fama_macbeth)

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

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