plm: using fixef() to manually calculate fitted values for a fixed effects twoways model

后端 未结 4 953
北恋
北恋 2021-02-19 21:44

Please note: I am trying to get the code to work with both time & individual fixed effects, and an unbalanced dataset. The sample code below works

4条回答
  •  野的像风
    2021-02-19 22:22

    I found this that can help you, since the lm() solution was not working in my case (I got different coefficients comparing to the plm package)

    Therefore, it is just about applying the suggestions by the authors of the plm package here http://r.789695.n4.nabble.com/fitted-from-plm-td3003924.html

    So what I did is just to apply

    plm.object <- plm(y ~ lag(y, 1) + z +z2, data = mdt, model= "within", effect="twoways")
    fitted <- as.numeric(plm.object$model[[1]] - plm.object$residuals) 
    

    where I need the as.numeric function since I need to use it as a vector to plug in for further manipulations. I also want to point out that if your model has a lagged dependent variable on the right hand side, the solution above with as.numeric provides a vector already NET of the missing values because of the lag. For me this is exactly what I needed to.

提交回复
热议问题