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
stats::lm- the standard OLS routine included in the base
stats. The call
summary(lm(y ~ x1 + x2, data = mydata))produces output most similar to
reg y x1 x2in Stata.
lfe- Linear Fixed Effects models. In addition to efficiently handling high-dimension fixed effects, the workhorse function
felmalso supports instrumental variables and clustered standard errors. As it improves
lmby incorporating features common to many econometric analyses,
felmis 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))
yis the dependent variable,
w2are exogenous continuous covariates,
f2are categorical variables that are projected out as fixed effects,
x1is an endogenous independent variable that is instrumented using exogenous
f3is 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
ivregfor instrumental variables estimation using 2SLS.
plm- Panel Linear models. I have found this package to be a bit less flexible than
lfebut I have admittedly little experience with it.
knitr- Dynamic documentation tool. Rather than copying and pasting
Routput into a document,
knitrand associated tools such as
Sweaveprovide a framework in which one can mix
Rcode 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
stargazerbut also the common process of summarizing regression results in general.
xtable- Occasianally I find that
stargazeris not flexible to generate the type of summary I need. The next step is to use tools such as
broomto extract estimates from fitted models and
Rdata.frames to LaTeX tables.