Search code examples
rregressionlmplm

model with three fixed effects in plm package in R


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?


Solution

  • 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.