Version (1.74 KB) by babaoglu
Two-Stage Least Squares method for instrumental variables estimation
Updated 28 Mar 2010

View License

% Original model: y = X*beta + u
% We are concerned that a regressor in X
% could be endogenous.
% W contains the instruments and all regressors in X
% except the suspected one.

% An example:
% m_t = b1 + b2*r_t + b3*y_t + b4*m_(t-1) + b5*m_(t-2) + u_t
% In this model, we have a concern that r_t could be correlated
% with u_t and we want to see the results with instruments,
% r_(t-1) and r_(t-2). So the inputs y, X and W must be
% y = m_t;
% X = [ones(length(y)) r y m_(t-1) m_(t-2)];
% W = [ones(length(y)) r_(t-1) r_(t-2) y m_(t-1) m_(t-2)];

% Please do not forget checking the length of the variables be equal

One computational method which can be used to calculate IV estimates is two-stage least squares (2SLS). In the first stage, each endogenous covariate in the equation of interest is regressed on all of the exogenous variables in the model, including both exogenous covariates in the equation of interest and the excluded instruments. The predicted values from these regressions are obtained.

Stage 1: Regress each column of X on W, (X = Wδ + errors)
and save the predicted (fitted) values:
^X=W inv(W'W)W'X=PwX

In the second stage, the regression of interest is estimated as usual, except that in this stage each endogenous covariate is replaced with the predicted values from its first stage model from the first stage.
Stage 2: Regress Y on the predicted values from the first stage:
Y=^Xβ+residuals (original model: Y=Xβ+u)
The resulting estimator of β is numerically identical to the expression displayed above. A small correction must be made to the sum-of-squared residuals in the second-stage fitted model in order that the covariance matrix of β is calculated correctly.

Cite As

babaoglu (2024). TSLS (2SLS) (https://www.mathworks.com/matlabcentral/fileexchange/27095-tsls-2sls), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Linear and Nonlinear Regression in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

I have added an example to the description