/**
This code can be pasted into a Stata .do file 

It replicates simulation results referenced in:

Dunning, Thad. 2008. "Model Specification in Instrumental-Variables Regression." Political Analysis 16 (3): 290-302. 

The results are posted on the Political Analysis website and also at http://www.thaddunning.com/research/all-research 

(See Tables 1 and 2 of "Dunning_web.pdf")
 
The results of running this .do file will differ slightly from the posted tables, due to the regeneration of the quasi-random data

**/

* TABLE 1 (INDEPENDENT X)

* Row 1:

* IVLS

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		ivreg y (xtot=x2), noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.000 1.0000 2.000

sum _b

* OLS

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		regress y xtot, noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.000 1.0000 2.000

sum _b

*  Row 2:

* IVLS

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		ivreg y (xtot=x2), noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.000 1.0000 1.000

sum _b

* OLS

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		regress y xtot, noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.000 1.0000 1.000

sum _b

* TABLE 2 (CORRELATED X)

* Row 1 (Beta_2 = 2)

* IVLS Regression

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		ivreg y (xtot=x2), noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.4 1.0000 2.000

sum _b

* OLS Regression:

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		regress y xtot, noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.4 1.0000 2.000

sum _b

* Row 2 (Beta_2 = 1)

* IVLS Regression

* IVLS Regression

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		ivreg y (xtot=x2), noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.4 1.0000 1.000

sum _b

* OLS Regression:

program drop _all

	program define bias_regression3
		drop _all
		version 9
		args c d beta_1 beta_2
		matrix V = (1.0000,     `d', `c' \ 	///
			    	`d',     1.0000, 0.0000 \	 ///
			    	`c', 0.0000, 1.0000 )
		drawnorm x1 x2 epsilon, n(250) cov(V)
		gen xtot = x1 + x2
		gen y = `beta_1'*x1 + `beta_2'*x2 + epsilon
		regress y xtot, noconstant
	end

	simulate _b, reps(1000): bias_regression3 0.1500 0.4 1.0000 1.000

sum _b