**
** Stata Code for Chapter 7, Exercise #4
**

** (a) Here is the code for the initial settings.  For part (i) change Gamma1 to 0 etc and re-run (and so on).
clear all
program OLS_Sim_a
  clear
  set obs 1000              /* Set sample size */
  gen X1 = rnormal()  		/* Generate X1 (independent variable) */
  scalar Gamma1 = 1.0		/* Direct effect of X1 on Y */
  scalar Alpha  = 0.0		/* Effect of X1 on X2 */
  scalar Gamma2 = 1.0		/* Effect of X2 on Y */
  gen X2 = Alpha*X1 + rnormal()  		/* Post-treatment variable */
  gen Y = Gamma1*X1 + Gamma2*X2 + rnormal() 
							/*  Dependent variable: function of X & post-treatment variable */
  regress Y X1       /* Run regression as function only of X1 */
end

simulate _b _se, reps(1): OLS_Sim_a  /* Run simulation 1 times */

** Display and summarize the estimated coefficients
list _b_* _se* /* Coefficient estimates & std. errors for each simulation */
summarize _b* /* Summarize coefficient estimates for each simulation */


** (b) Here is the code for the initial settings.  For part (i) change Gamma1 to 0 etc and re-run (and so on).
clear all
program OLS_Sim
  clear
  set obs 1000              /* Set sample size */
  gen X1 = rnormal()  		/* Generate X1 (independent variable) */
  scalar Gamma1 = 1.0		/* Direct effect of X1 on Y */
  scalar Alpha  = 0.0		/* Effect of X1 on X2 */
  scalar Gamma2 = 1.0		/* Effect of X2 on Y */
  gen X2 = Alpha*X1 + rnormal()  		/* Post-treatment variable */
  gen Y = Gamma1*X1 + Gamma2*X2 + rnormal() 
							/*  Dependent variable: function of X & post-treatment variable */
  regress Y X1 X2       /* Run regression as function of X1 and X2*/
end

simulate _b _se, reps(1): OLS_Sim  /* Run simulation 50 times */

** Display and summarize the estimated coefficients
list _b_* _se* /* Coefficient estimates & std. errors for each simulation */
summarize _b* /* Summarize coefficient estimates for each simulation */


** (d)
clear all
program OLS_Sim
  clear
  set obs 1000              /* Set sample size */
  gen X1 = rnormal()  		/* Generate X1 (independent variable) */
  scalar Rho1 = 1.0		/* Effect of U on X2 */
  scalar Rho2 = 1.0		/* Effect of U on Y */
  gen U = rnormal()  		/* Generate U, unobserved error */
  scalar Gamma1 = 1.0		/* Direct effect of X1 on Y */
  scalar Alpha  = 1.0		/* Effect of X1 on X2 */
  scalar Gamma2 = 0.0		/* Effect of X2 on Y */
  gen X2 = Alpha*X1 + Rho1*U + rnormal()  		/* Post-treatment variable */
  gen Y = Rho2*U + rnormal() 
							/*  Y is only a function of U and an additional error term */
  regress Y X1       /* Run regression as function of X1*/
end

simulate _b _se, reps(50): OLS_Sim  /* Run simulation 1 times */

** Display and summarize the estimated coefficients
list _b_* _se* /* Coefficient estimates & std. errors for each simulation */
summarize _b* /* Summarize coefficient estimates for each simulation */


** (e)
clear all
program OLS_Sim
  clear
  set obs 1000              /* Set sample size */
  gen X1 = rnormal()  		/* Generate X1 (independent variable) */
  scalar Rho1 = 1.0		/* Effect of U on X2 */
  scalar Rho2 = 0.0		/* Effect of U on Y */
  gen U = rnormal()  		/* Generate U, unobserved error */
  scalar Gamma1 = 1.0		/* Direct effect of X1 on Y */
  scalar Alpha  = 1.0		/* Effect of X1 on X2 */
  scalar Gamma2 = 1.0		/* Effect of X2 on Y */
  gen X2 = Alpha*X1 + Rho1*U + rnormal()  		/* Post-treatment variable */
  gen Y = Rho2*U + rnormal() 
							/*  Y is only a function of U and an additional error term */
  regress Y X1 X2       /* Run regression as function of X1*/
end

simulate _b _se, reps(50): OLS_Sim  /* Run simulation 50 times */

** Display and summarize the estimated coefficients
list _b_* _se* /* Coefficient estimates & std. errors for each simulation */
summarize _b* /* Summarize coefficient estimates for each simulation */
