You are on page 1of 3

clear

clear matrix
use http://www.stata-press.com/data/imeus/griliches, clear

save c:\econometrics\instrumental_vars\griliches.dta, replace

set matsize 1000

gen cons= 1

/* The X matrix contains the exogenous and endogenous regressors, but no


instruments */
mkmat cons iq s expr tenure rns smsa _I*, mat(X)

/* The Z matrix is the X matrix but with the endogenous variable(s) replaced
with its/their instrument(s) */
mkmat cons s expr tenure rns smsa _I* med kww age mrt, mat(Z)

mat dir

/* X has 13 columns, one for each of the independent variables. We remove


the endogenous variable, which brings the starting point of the Z matrix
to 12, and then include the four instruments for the endogenous variable iq.
13 -1 + 4= 16
12 + 4 = 16
*/

/* Create the matrices necessary for the formula in Baum's


"An Introduction to Modern Econometrics Using Stata" pg. 188. */
mkmat lw, mat(Y)

mat ZT= Z'

mat ZTZ= ZT * Z

mat ZTZInv= inv(ZTZ)

mat ZTX= ZT * X

mat Xhat= Z * ZTZInv * ZTX

/* Generate predicted values of iq based on the instruments and all


of the other exogenous variables in the wage equation. */
qui reg iq med kww age mrt s expr tenure rns smsa _I*
predict iqhat, xb

/* Create a vector of the predicted values of iq based on the instruments


and all of the exogenous variables */
mkmat iqhat, mat(iqhat)

/* Create a vector of all of the values of iq from the Xhat matrix */


matrix Xhatiq= Xhat[1..`e(N)', 2]

/* Next, create one matrix that contains (in order) the values of iq
in the Xhat matrix, the predicted value of iq based on all exogenous variables,
and iq from the original X matrix */
matrix Xhatiq_iqhat_iqx= Xhatiq, iqhat, X[1.. `e(N)', 2]
matrix list Xhatiq_iqhat_iqx
/* Can see above that the predicted values of iq are 1) based on a regression
of iq on all the exogenous variables
(i.e., instruments and exogenous regressors) 2) agree to the values of
the iq variable used in the Xhat matrix and 3) that these values are different
from the observed values of iq in the X matrix. The Xhat matrix has replaced
the endogenous, observed values of iq with their predictions using the
instrumental variable(s). */

local names1: colnames Xhat


display "`names1'"

local names2: colnames Z


display "`names2'"

local names3: colnames X


display "`names3'"

/* Check a few of the columns of the exogenous variables and see if


the Z, Xhat, and X matrices contain the same values for these variables.

The variable in the second column of Z, s, is the variable in the third


column of X and Xhat. */

matrix Z2Xhat3X3= Z[1..`e(N)', 2] , Xhat[1.. `e(N)', 3], X[1.. `e(N)', 3]


matrix list Z2Xhat3X3
/* No differences noted. */

local names1: colnames Xhat


display "`names1'"

local names2: colnames Z


display "`names2'"

local names3: colnames X


display "`names3'"

/* The variable in the fourth column of Z, tenure, is the fifth


column of Xhat and X. */
matrix Z4Xhat5X5= Z[1..`e(N)', 4], Xhat[1.. `e(N)', 5], X[1.. `e(N)', 5]
matrix list Z4Xhat5X5
/* No differences noted. */

local names1: colnames Xhat


display "`names1'"

local names2: colnames Z


display "`names2'"

local names3: colnames X


display "`names3'"

/* The variable in the sixth column of Z, smsa, is the seventh


column of Xhat and X. */
matrix Z6Xhat7X7= Z[1..`e(N)', 6], Xhat[1.. `e(N)', 7], X[1.. `e(N)', 7]
matrix list Z6Xhat7X7
/* No differences noted. */

matrix d
local names1: colnames Xhat
display "`names1'"
local names3: colnames X
display "`names3'"

/* The Z matrix has 16 columns, and both the X and Xhat matrix have 13
columns. You can see from the above that 1) the X and Xhat matrix contain
the same values for the exogenous regressors and 2) The endogenous iq
variable has been replaced in the Xhat matrix by its prediction from
an OLS regression model based on its instruments and all of the other
exogenous variables. */

/* Create the remaining matrices necessary for the Baum calculations


on page 188. */
mat XhatTrans= Xhat'

mat XhatTransX= XhatTrans * X

mat XhatTransXInv= inv(XhatTransX)

mat XhatTransY= XhatTrans * Y

/* Generate the "by hand" 2SLS parameter estimates. */


mat B= XhatTransXInv * XhatTransY

matrix list B

/* Compare the "by hand" estimates to Stata's output */


ivreg lw s expr tenure rns smsa _I* (iq= med kww age mrt)

/* The "by hand" results agree to Stata's output. */

You might also like