Showing posts with label ivregress. Show all posts
Showing posts with label ivregress. Show all posts

Nov 25, 2019

Instrumental variable regression

clear
set seed 1
set obs 10000
eststo clear

// Simulate data for IV regression
matrix input c = ( 1, .5, 0, .5\ ///
                  .5,  1, 0,  0\ ///
                   0,  0, 1,  0\ ///
                  .5,  0, 0,  1)
corr2data x u e z, corr(c)

// Calculate y
generate y = 1 + 2*x + 2*u + 5*e

// Without omitted variable bias
eststo: regress y x u

// With omitted variable bias
eststo: regress y x

// Reduced form
eststo: regress y z
local rf = _b[z]

// First stage
eststo: regress x z
local fs = _b[z]
predict xhat
predict xres, resid

// Two-stage predictor substitution (2SPS)
eststo: regress y xhat

// Two-stage residual inclusion (2SRI)
eststo: regress y x xres

// Two-stage least square (2SLS)
eststo: ivregress 2sls y (x = z)

// IV estimate = reduced form/first stage
di `rf'/`fs'

// Table esttab, b(2) se(2) mtitle("DGP" "Bias" "Reduced form" "1st stage" "2SPS" "2SRI" "2SLS") /// coeflabel(x "Predictor x" /// z "Instrument z" /// xres "1st stage residual" /// _cons "Intercept") /// rename(xhat x) drop(u) /// stats(N, label(Observations) fmt(%9.0gc)) /// title(Different varieties of instrumental variable regression) /// varwidth(20)

Jan 8, 2018

Omitted variable bias

clear 
set obs 10000
set seed 1

// Simulate data
generate x  = rnormal(0,1)                     // Exogenous variable
generate w  = rnormal(0,1)                     // Instrumental variable
generate u  = rnormal(0,1)                     // Omitted variable
generate e1 = rnormal(0,1)                     // Outcome eqation error
generate e2 = rnormal(0,1)                     // Endogenous regressor equation error
generate y2 =      x  + .2 * w + .5 * u + e2   // Endogenous regressor equation
generate y1 = .5 * y2 + .5 * x + .5 * u + e1   // Outcome equation

// Fit models
eststo clear
eststo: regress        y1 y2 x u
eststo: regress        y1 y2 x
eststo: ivregress 2sls y1 x (y2 = w)

coefplot est1 est2 est3, xscale(alt) xtitle(Coefficient) ///
                         xline(.5) ///
                         coeflabel(_cons = "Intercept" ///
                                   u     = "Omitted variable" ///
                                   x     = "Exogenous predictor" ///
                                   y2    = "Endogenous predictor") ///
                         legend(order(2 "Without omitted variable bias" ///
                                      4 "With omitted variable bias" ///
                                      6 "2SLS estimate") pos(11) ring(0) col(1))

Jul 20, 2017

IV regression using the -sem- command

// Read in data from Angrist and Krueger (1991)
// https://economics.mit.edu/faculty/angrist/data1/data/angkru1991
infile lwklywge educ yob qob pob using asciiqob.txt, clear

// Generate dummy variables as SEM command does not take factor variables
qui tabulate qob, gen(qobx)
qui tabulate yob, gen(yobx)
qui tabulate pob, gen(pobx)
drop qobx1 yobx1 pobx1      // Get rid of reference category

eststo clear

// Model 2 of Table 4.1.1 of Mostly Harmless Econometrics
eststo: regress lwklywge educ yobx* pobx*, robust                              

// Model 6 of Table 4.1.1 of Mostly Harmless Econometrics
eststo: ivregress 2sls lwklywge yobx* pobx* (educ = qobx*), robust

// Model 6 of Table 4.1.1 using the sem command
eststo: sem (lwklywge <- yobx* pobx* educ) (educ <- pobx* qobx*), cov(e.lwklywge*e.educ) 
esttab, b(3) se(3) nostar drop(_cons educ:) /// indicate("9 year-of-birth dummies = yobx*" /// "50 state-of-birth dummies = pobx*") /// title("OLS and 2SLS estimates of the economic returns to schooling") /// coeflabel(educ "Years of education") /// mtitles("OLS" "-ivregress-" "-sem-") nonumbers varwidth(30) /// eqlabels("", none) // Removes equation label