1

The code below runs the time series regression "excessr ~ mkt_rf" with biglm, because the function lm does not work with my real dataset.

Now I would like to switch from biglm to plm to account for fixed effects. Unfortunately plm doesn't work.

Does anyone know what I could change that plm work?

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)
})
Helix123
  • 3,502
  • 2
  • 16
  • 36
Rbeginner
  • 29
  • 5
  • What is the column supposed to contain fixed effects? ```FundId```? – bretauv Mar 19 '20 at 10:58
  • I am not that deep in to statistics but my dataset is unbalanced therefore I need to apply plm to avoid fund fixed effects in the regression – Rbeginner Mar 19 '20 at 11:03

1 Answers1

1

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:

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

Edit: following this post and this article, it appears that you can do a Fama-MacBeth regression with pmg (also in the plm package):

fama_macbeth <- pmg(excessr ~ mkt_rf, 
                   data = union_with_factors, 
                   index = c("FundId", "t"))
summary(fama_macbeth)
bretauv
  • 7,756
  • 2
  • 20
  • 57
  • Thanks you, that works. I am just wondering, because I am supposed to use the Fama McBeth regression where I first regress the time series (excessr ~ mkt_rf) for every FundId. Then in the second step regress the coefficients that I have calculated before cross-sectional (excessr ~ Beta_mkt_rf) for every t. Given this procedure is it possible to use the plm function? It seems to me that plm does both steps together.. – Rbeginner Mar 19 '20 at 11:31
  • I have never done that but it seems you can do it with ```pmg```. See my edit for details – bretauv Mar 19 '20 at 11:42
  • While plm works quite well with my data pmg throws an error which says "index = c("FundId", : Insufficient number of time periods". Do you know how to fix this. And big thanks for your efforts!! – Rbeginner Mar 19 '20 at 12:22
  • I have never worked with this type of regression but the error message is quite explicit and I can't help you with your data. Does my answer work for your example? – bretauv Mar 19 '20 at 12:38