Guide to R for SCU Economics Students, v. 3.0
Instrumental-variables (IV) regression is a widely used technique for dealing with endogeneity problems– namely, when one or more of the regressors (X) is correlated with the error term (u). When this is true, the estimated OLS coefficient on the endogenous X is biased and inconsistent. IV regression rests on finding a variable (or variables) that is correlated with X but uncorrelated with u. Such a variable is called an instrumental variable (IV), or just an instrument. Your textbook will discuss in greater detail the requirements for a valid instrument. In this tutorial, we focus on how to run IV regressions in R, using the packages AER and ivpack. As usual, we will work through some examples from Stock and Watson, Introduction to Econometrics– in this case, from Chapter 12.
The example we will look at is a classic application of IV regression: estimating the price elasticity of demand for a product. In this case the product in question is cigarettes. Recall that the elasticity of demand is the percentage change in quantity demanded for a percentage change in price, moving along the demand curve. This can also be expressed as the slope of the relationship between the natural logs of Q and P. That is,
\[elasticity = \epsilon = \frac{dQ_d/Q_d}{dP/P} = \frac{dln(Q_d)}{dln(P)}.\]
It seems that we could simply use data on quantity and price to run a regression between the log-transformed values and obtain an estimate of the elasticity, as follows:
\[lnQ = \alpha + \beta lnP + u, \,where \, \hat\beta \,is \,the \,estimate \,of \, the \,elasticity.\]
The problem is, combinations of quantity and price that we observe in the data reflect the forces of both demand and supply. Therefore in general the relationship we estimate is neither a demand curve nor a supply curve, but usually a complex mix of shifting demand and supply curves.
In the above regression model, P is endogenous: it is correlated with the error term in the demand curve. Unobserved factors that increase demand will tend to increase the price as the equilibrium moves up the supply curve. Thus \(\hat\beta\) is a biased estimate of the demand elasticity.
To deal with this we will estimate the demand equation using IV. IV regressions are usually estimated using a procedure called two-stage least squares (2SLS). The command ivreg
estimates 2SLS using a slight modification of our familiar regression syntax.
To get started, start RStudio and open the script t_ivreg.R in your script editor. Edit the setwd(…)
command for your working directory. Check the list of packages in the library
commands and make sure you have installed all of them. Then run the script from the top through the data section.
The data set CigarettesSW is imported from the AER package, and is the same data used by S&W throughout chapter 12. We will be working with the 1995 cross-section of states, which is called cigs in the tutorial. Note that we divide the prices by the CPI variable to obtain the “real” prices, though this is not strictly necessary when dealing with a single year of data.
The variable we will use as an instrument is tdiff, which is the real tax on cigarettes arising from the state’s general sales tax. The presumption is that in states with a larger general sales tax, consumers will tend to pay a higher price for cigarettes, but that the general tax is not determined by the cigarette market, so otherwise uncorrelated with cigarette demand.
As the name suggests and the Stock and Watson textbook explains, 2SLS involves estimating two regressions: In the first stage, the endogenous variable (log price in our example) is regressed on the instrument or instruments (tdiff), along with any other exogenous variables (controls). The estimated coefficients from the first-stage regression are used to predict the endogenous variable (log price). In the second stage, the dependent variable (log quantity) is regressed on the predicted values of the endogenous variable (predicted log price), along with the exogenous controls. If the instrument is valid, the estimated coefficient on the predicted endogenous variable in the second stage is an unbiased estimate of the desired parameter (in our case, the demand elasticity).
In practice, statistical software often just reports the second-stage regression, which is what one is usually interested in. That is the case with the command we use, ivreg
.
Before we turn to the IV estimates, let’s run the OLS regression (around line 110) and the stargazer table following it. What is the estimated price elasticity? Remember, this estimate is probably biased because of the endogeneity of the price.
Now let’s take a quick look at what would be the first-stage regression, which is called first1
in the script. Make sure you understand why this is the first-stage regression. Run it and note that the coefficient on the instrument, tdiff, is statistically significant (at what level?). This is important, as we shall discuss in class.
Now let’s turn to the actual IV regression. Note the syntax in the following ivreg
command:
iv1 = ivreg(lquant ~ lprice | tdiff , data = cigs)
The part before the vertical line, lquant ~ lprice
, is the (second-stage) regression we want to estimate: the demand curve. You enter it exactly as you would a regression in lm
. The part after the vertical line, tdiff, is the instrument. If we had more than one instrument, we would enter them using a “+” sign (see below). Instruments should only appear after the vertical line.
Note that in the stargazer
command we use the ivse function in se=list(ivse(iv1))
to obtain corrected standard errors.
Run the first ivreg
and the stargazer
and examine the results. You might compare them with the OLS we ran before. What is the estimate of the demand elasticity here? Also, compare with the estimate presented in Stock and Watson (equation 12.11). You’ll see a slight difference in the estimates SEs, due to a small difference in the formulas used.
Note that we can have more than one instrument, and we can also include exogenous control variables. Let Y be the outcome (dependent) variable of interest (lquant in our example), X be the endogenous variable (lprice), W be any exogenous regressors, not including instruments (see below), and Z be the instruments (tdiff in our case). Then the syntax for ivreg
is:
ivreg(Y ~ X + W | W + Z, ... )
Important note: Endogenous variables (X) can only appear before the vertical line; instruments (Z) can only appear after the vertical line; exogenous regressors that are not instruments (W) must appear both before and after the vertical line.
Let’s add an exogenous regressor: log per capita income. Why does this belong in a demand equation? Let’s also add another instrument suggested in the book: the real tax on cigarettes tax/cpi
, which we’ll create in the regression. This gives us the specification in the textbook (12.16). Here’s the code:
iv2 = ivreg(lquant ~ lprice + lincome | lincome + tdiff + I(tax/cpi),
data = cigs)
Run the above and the stargazer
that follows it.
The instrumental variables method must be used with considerable caution. In general it is crucial to have a compelling case for the validity of your instrument(s). Although there are statistical tests that can help us judge, for the most part IV requires knowledge and judgment about the specific application. Section 12.4 in Stock and Watson offers a good discussion for our example.
You can run some useful diagnostics on your IV regression using the summary
command, as shown at the end of the script. Here’s the code and the results:
summary(iv2, vcov = sandwich, diagnostics = TRUE)
Call:
ivreg(formula = lquant ~ lprice + lincome | lincome + tdiff +
I(tax/cpi), data = cigs)
Residuals:
Min 1Q Median 3Q Max
-0.6006931 -0.0862222 -0.0009999 0.1164699 0.3734227
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9.8950 0.9288 10.654 6.89e-14 ***
lprice -1.2774 0.2417 -5.286 3.54e-06 ***
lincome 0.2804 0.2458 1.141 0.26
Diagnostic tests:
df1 df2 statistic p-value
Weak instruments 2 44 228.738 <2e-16 ***
Wu-Hausman 1 44 3.823 0.0569 .
Sargan 1 NA 0.333 0.5641
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.1879 on 45 degrees of freedom
Multiple R-Squared: 0.4294, Adjusted R-squared: 0.4041
Wald test: 17.25 on 2 and 45 DF, p-value: 0.000002743
The top part of the table just presents the coefficients, etc. But the bottom portion, Diagnostic tests, contains some new information. Note the following:
iv2
. If the null is rejected, it means that at least one of our instruments is invalid, and possibly all of them.Key R commands (functions):
ivreg
: runs IV (2SLS) regressions (requires package AER
)Key points/ concepts:
ivreg
works a lot like lm
.ivreg
is as follows: ivreg(Y ~ X + W | W + Z, ... )
, where X is endogenous variable(s), Z is instrument(s), and W is exogenous controls (not instruments).stargazer
tables, requiring package ivpack
.summary
.