R Random and Fixed Effects Models – How to Implement GLM for Panel Data

fixed-effects-modelpanel datarrandom-effects-model

i am quite confused about creating a fixed effects model.
How exactly does the Fixed effects model differ from the basic model with fixed countrys, because up until now i thought that this model would be my Fixed effects model.
So my plan is to run three models:

  1. Basic model with fixed countrys
  2. Random effects with country
    intercept
  3. Fixed effects model without countrys (here i have no
    idea, on how to create this model anymore)

This is my code:

##country-level fixed effects
model_fes <- as.formula("comp ~  factor(country) + h + c + wd + ra + ac + ov")
clog_fes = glm(model_fes,data=df,family = binomial(link = cloglog))
summary(clog_fes)
summ(clog_fes)

random intercept with country-level effects

library(lme4)
model_res<- as.formula("comp ~  (1|country) + h + c + wd + ra + ac + ov")
clog_res = glmer(model_re1,data=df,family = binomial(link = cloglog))
summary(clog_res)
summ(clog_res)

fixed effects model

??

Best Answer

It seems to me that the three models are actually three different things. It is normal that there is some confusion about the terms "fixed" and "random" effects, because different applied fields use them differently, see for example here: https://statmodeling.stat.columbia.edu/2005/01/25/why_i_dont_use/

Within a more econometric reading of your question, model 3 would refer to a model where values all variables are de-meaned for country (i.e., you subtract the country-level mean from each observation, across all variables). In this case you will not need to include country explicitly as a predictor variable, so the model would indeed be "without countries", as in your description. This is typically called the fixed effects estimator in econometrics, or the within estimator (because you reduce your data to variation within countries only). You can do this in R for example by using plm::plm() with model = "within". The plm package, which can do all three model types you mentioned, is described in detail here: https://cran.r-project.org/web/packages/plm/vignettes/plmPackage.html

Note that the estimates and standard errors should be exactly identical to the case where you simply add dummies for country to your model matrix via factor(country) (model 1).

Model 1 is actually known as the LSDV (Least Square Dummy Variable) approach in econometrics. Equivalence between fixed effects and LSDV estimator is further shown and discussed in chapter 10.2 of

  • Verbeek, M. (2008). A guide to modern econometrics. John Wiley & Sons.

A note of cuation: De-meaning by hand and then running a simple lm() on the de-meaned data will lead to a fixed effects model with biased standard errors, because lm() will calculate with too many degrees of freedom, as if there were no country variables used at all. So better simply use plm(), which corrects for this.

Related Question