Econometrics in R
Grant V. Farnsworth
October 26, 2008
This paper was originally written as part of a teaching assistantship and has subsequently become a personal reference.
I learned most of this stu by trial and error, so it may contain ineciencies, inaccuracies, or incomplete explanations. If
you nd something here suboptimal or have suggestions, please let me know. Until at least 2009 I can be contacted at
g-farnsworth@kellogg.northwestern.edu.
Contents
1 Introductory Comments 3
1.1 What is R? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 How is R Better Than Other Packages? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Obtaining R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Using R Interactively and Writing Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Working with Data 6
2.1 Basic Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Caveat: Math Operations and the Recycling Rule . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Important Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.2 Arrays, Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.3 Dataframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3.4 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3.5 S3 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3.6 S4 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Working with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 Merging Dataframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.6 Opening a Data File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7 Working With Very Large Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.7.1 Reading elds of data using scan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.7.2 Utilizing Unix Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.7.3 Using Disk instead of RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.7.4 Using RSQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.8 Issuing System Commands|Directory Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.9 Reading Data From the Clipboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.10 Editing Data Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3 Cross Sectional Regression 14
3.1 Ordinary Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2 Extracting Statistics from the Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 Heteroskedasticity and Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3.1 Breusch-Pagan Test for Heteroskedasticity . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3.2 Heteroskedasticity (Autocorrelation) Robust Covariance Matrix . . . . . . . . . . . . . 16
3.4 Linear Hypothesis Testing (Wald and F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.5 Weighted and Generalized Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.6 Models With Factors/Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 Special Regressions 18
4.1 Fixed/Random Eects Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.1 Fixed Eects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.2 Random Eects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2 Qualitative Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2.1 Logit/Probit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2.2 Multinomial Logit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2.3 Ordered Logit/Probit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.3 Tobit and Censored Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.4 Quantile Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.5 Robust Regression - M Estimators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.6 Nonlinear Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.7 Two Stage Least Squares on a Single Structural Equation . . . . . . . . . . . . . . . . . . . . 21
4.8 Systems of Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2
4.8.1 Seemingly Unrelated Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.8.2 Two Stage Least Squares on a System . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5 Time Series Regression 22
5.1 Dierences and Lags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.2 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23