************************************************************************************************* *** Example: IV Simulation - 500 reps; n=10; corr(x,u) = 0.5; corr(z,u) = 0; corr (x,z) = 0.8 *** ************************************************************************************************* clear * Generating 2 variables with missing values to store estimated values of beta hat from the simulation * set seed 12345 set obs 500 g data_bxols=. g data_bxiv=. * Simulating data based on correlation values and storing estimates of beta hat * forval i=1/500 { preserve clear set obs 10 matrix C = (1, 0.8, 0.5 \ 0.8, 1, 0 \ 0.5, 0, 1) drawnorm x z u, corr(C) g y = 1 + 2*x + u reg y x local xcoefols =_b[x] ivregress 2sls y (x = z) local xcoefiv =_b[x] restore replace data_bxols=`xcoefols' in `i' replace data_bxiv=`xcoefiv' in `i' } * Summary and histogram of beta hat values * su data_bxols data_bxiv hist data_bxols hist data_bxiv *************************************************************************************************** *** Example: IV Simulation - 500 reps; n=1000; corr(x,u) = 0.5; corr(z,u) = 0; corr (x,z) = 0.8 *** *************************************************************************************************** clear * Generating 2 variables with missing values to store estimated values of beta hat from the simulation * set seed 12345 set obs 500 g data_bxols=. g data_bxiv=. * Simulating data based on correlation values and storing estimates of beta hat * forval i=1/500 { preserve clear set obs 1000 matrix C = (1, 0.8, 0.5 \ 0.8, 1, 0 \ 0.5, 0, 1) drawnorm x z u, corr(C) g y = 1 + 2*x + u reg y x local xcoefols =_b[x] ivregress 2sls y (x = z) local xcoefiv =_b[x] restore replace data_bxols=`xcoefols' in `i' replace data_bxiv=`xcoefiv' in `i' } * Summary and histogram of beta hat values * su data_bxols data_bxiv hist data_bxols hist data_bxiv **************************************************************************************************** *** Example: IV Simulation - 500 reps; n=1000; corr(x,u) = 0.5; corr(z,u) = 0; corr (x,z) = 0.01 *** **************************************************************************************************** clear * Generating 2 variables with missing values to store estimated values of beta hat from the simulation * set seed 12345 set obs 500 g data_bxols=. g data_bxiv=. * Simulating data based on correlation values and storing estimates of beta hat * forval i=1/500 { preserve clear set obs 1000 matrix C = (1, 0.01, 0.5 \ 0.01, 1, 0 \ 0.5, 0, 1) drawnorm x z u, corr(C) g y = 1 + 2*x + u reg y x local xcoefols =_b[x] ivregress 2sls y (x = z) local xcoefiv =_b[x] restore replace data_bxols=`xcoefols' in `i' replace data_bxiv=`xcoefiv' in `i' } * Summary and histogram of beta hat values * su data_bxols data_bxiv hist data_bxols hist data_bxiv ********************** *** Other examples *** ********************** use "C:\Users\royj\Dropbox\eco5720\Data Sets- STATA\MROZ.DTA", clear *** OLS *** drop if lwage==. reg lwage educ *** IV using the covariance formula *** corr lwage fatheduc, cov g covyz=r(cov_12) corr fatheduc educ, cov g covxz=r(cov_12) g iv1=covyz/covxz su iv1 di "IV estimate = " r(mean) *** IV using two stage least squares manually *** reg educ fatheduc predict educh reg lwage educh *** IV using two stage least squares *** ivregress 2sls lwage (educ = fatheduc), first