簡體   English   中英

使用 plm 固定效果回歸

[英]Fixed effects regression with plm

下面的代碼使用 biglm 運行時間序列回歸“excessr ~ mkt_rf”,因為函數 lm 不適用於我的真實數據集。

現在我想從 biglm 切換到 plm 以解決固定效應。 不幸的是 plm 不起作用。

有誰知道我可以改變 plm 的工作嗎? 提前致謝!

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)
})

假設個人的 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)

有關更多詳細信息,請參閱此處plm文檔(控制台中的?plm

編輯:這篇文章這篇文章之后,您似乎可以使用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