Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notes on econometrics in R

Notes on Econometrics in R

This note summarizes several tools for traditional econometric analysis using R. The CRAN Task View - Econometrics provides a very comprehensive overview of available econometrics packages in R. Rather the duplicate this resource, I will highlight several functions and tools that accommodate 95% of my econometric analyses.

Packages and functions

Linear Regression

  • stats::lm - the standard OLS routine included in the base R package stats. The call summary(lm(y ~ x1 + x2, data = mydata)) produces output most similar to reg y x1 x2 in Stata.

  • lfe - Linear Fixed Effects models. In addition to efficiently handling high-dimension fixed effects, the workhorse function felm also supports instrumental variables and clustered standard errors. As it improves lm by incorporating features common to many econometric analyses, felm is my preferred tool for linear models. To illustrate typical usage, one might summarize the results of a linear model with

    summary(felm(y ~ w1 + w2 | f1 + f2 | (x1 ~ z1) | f3, data = mydata))
    

    where y is the dependent variable, w1 and w2 are exogenous continuous covariates, f1 and f2 are categorical variables that are projected out as fixed effects, x1 is an endogenous independent variable that is instrumented using exogenous z1, and f3 is the categorical variable by which standard errors are clustered.

  • AER - this package includes many functions and datasets to accompany the excellent book by Christian Kleiber and Achim Zeileis, Applied Econometrics with R (2008), which I highly recommend reading. One notable function is ivreg for instrumental variables estimation using 2SLS.

  • plm - Panel Linear models. I have found this package to be a bit less flexible than lfe but I have admittedly little experience with it.

Summarizing Results

  • knitr - Dynamic documentation tool. Rather than copying and pasting R output into a document, knitr and associated tools such as RMarkdown and Sweave provide a framework in which one can mix R code and output with the final output document. A much better introduction to the package and countless examples can be seen here.
  • stargazer - easily summarizes regression models in tables. In addition to the package documentation, I cannot recommend the phenomenal cheat sheet by Jake Russ, which not only illustrates the features of stargazer but also the common process of summarizing regression results in general.
  • broom and xtable - Occasianally I find that stargazer is not flexible to generate the type of summary I need. The next step is to use tools such as broom to extract estimates from fitted models and xtable to convert R data.frames to LaTeX tables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment