Hand Coding Instumental Variables
In a previous post we discussed the linear model and how to write a function that performs a linear regression. In this post we will use that linear model function to perform a [Two-Stage Least Squares estimation]. This estimation allows us to […]
Recall that we built the follow linear model function.
We used the following data.
This allowed us to estimate a linear model.
Which includes the intercept, since the default value it
TRUE (see function definition above),
we could estimate it without an intercept using.
Having revisited the above, we can continue with instumental variables.
We will replicate an example from the
AER (Applied Econometric Regressions) package.
We first need to obtain our first stage estimate (putting the whole function between parentheses allows us to both write it to the object
s1 and print it).
We can now obtain the predicted (fitted) values
Using these fitted values, we can finally estimate our second stage.
Now compare this to the results using
ivreg() fucntion from the
When we compare this estimate to the estimate from the linear model, we find that the effect of the price is significantly overestimated there.
We can also do this using
R’s built in
As an intermediate form, we can manually calculate
fitted.values) and estimate using that.
Note that if you estimate a TSLS using the
that you can only use the coefficients,
the error terms will be wrong.