2

I am trying to estimate the model with 3 fixed effects. One is a customer-fixed effect, another one is good fixed effect and the third one is time-fixed effect. I am new to plm package, but as I understand, if I had just 2 fixed effects (time and good). I would do something like this:

  fe <- plm(outcome ~ dependent variable + explanatory variable 1 + explanatory variable 2, 
            data = mydata, index = c("good_id", "time"), model = 'within', effect = "twoways")

But how I approach this problem in plm package if I have not 2 fixed effects, but 3?

2 Answers2

2

You may add the third fixed effect as a dummy variable using factor(). Example:

library(plm)
data("Produc", package="plm")

# plm FE model
zz1 <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + 
             factor(region),
          data=Produc, index=c("state","year"), model='within', effect="twoways")

# LSDV model
zz2 <- lm(log(gsp) ~ 0 + log(pcap) + log(pc) + log(emp) + unemp
         + factor(state) + factor(year) + factor(region),
          data=Produc)

summary(zz1)$coe
#               Estimate  Std. Error   t-value      Pr(>|t|)
# log(pcap) -0.030176057 0.026936544 -1.120265  2.629606e-01
# log(pc)    0.168828035 0.027656339  6.104497  1.655450e-09
# log(emp)   0.769306196 0.028141794 27.336786 1.275556e-114
# unemp     -0.004221093 0.001138837 -3.706493  2.256597e-04

summary(zz2)$coe[1:4,]
#               Estimate  Std. Error   t value      Pr(>|t|)
# log(pcap) -0.030176057 0.026936544 -1.120265  2.629606e-01
# log(pc)    0.168828035 0.027656339  6.104497  1.655450e-09
# log(emp)   0.769306196 0.028141794 27.336786 1.275556e-114
# unemp     -0.004221093 0.001138837 -3.706493  2.256597e-04

Yielding identical coefficients and statistics.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • But aren't there any potential problems with transferring it to factor in plm? I haven't fully understood how it can be bad, but from reading this thread (the problem there is different though) it seems that it may cause problems:https://stackoverflow.com/questions/39563595/fixed-effects-in-r-plm-vs-lm-factor Sorry if my question is obvious and there is nothing wrong, I confess that I don't fully get it :) – Anya Pilipentseva May 06 '20 at 11:17
  • @AnyaPilipentseva I think your linked answer refers to the error `Error in pdim.default(index[[1]], index[[2]]) : duplicate couples (id-time)`. – jay.sf May 06 '20 at 11:33
  • @AnyaPilipentseva Please notice my [new relevant answer](https://stackoverflow.com/a/70845623/6574038). – jay.sf Jan 25 '22 at 12:58
0

Thanks to everyone for help! I actually found another solution myself which may add to provided ideas: https://cran.r-project.org/web/packages/fixest/vignettes/fixest_walkthrough.html#1_simple_example_using_trade_data This package allows inclusion of multiple fixed effects