* Compute covariance matrix of y, x, z corr y x z, covariance * Store covariance matrix matrix C = r(C) * Extract covariances scalar cov_y_z = C[1,3] scalar cov_x_z = C[2,3] * Compute β1_IV scalar beta1_IV = cov_y_z / cov_x_z display "beta1_IV = " beta1_IV * Compute means of y and x summ y, meanonly scalar mean_y = r(mean) summ x, meanonly scalar mean_x = r(mean) * Compute β0_IV = mean(y) – β1_IV * mean(x) scalar beta0_IV = mean_y - beta1_IV * mean_x display "beta0_IV = " beta0_IV ******************************************************************************** * Step 1: Regress x on z reg x z * Step 2: Obtain fitted values x_hat predict x_hat, xb * Step 3: Regress y on x_hat using OLS reg y x_hat ******************************************************************************* *================================================== * Manual Computation of the Variance of the IV (2SLS) Estimator *================================================== *-------------------------------------------------- * First stage: x on z *-------------------------------------------------- reg x z predict x_hat, xb * Store first-stage R-squared scalar R2_xz = e(r2) *-------------------------------------------------- * Second stage: y on x_hat *-------------------------------------------------- reg y x_hat * Estimate error variance from second stage scalar sigma2_u = e(rss) / e(df_r) *-------------------------------------------------- * Compute sum of squared deviations of x *-------------------------------------------------- summ x, meanonly scalar mean_x = r(mean) gen x_dev2 = (x - mean_x)^2 summ x_dev2, meanonly scalar Sxx = r(sum) *-------------------------------------------------- * Variance and standard error of IV estimator beta1 *-------------------------------------------------- scalar Var_beta1_IV = sigma2_u / (R2_xz * Sxx) scalar SE_beta1_IV = sqrt(Var_beta1_IV) display "Variance of beta1_IV = " Var_beta1_IV display "Standard Error of beta1_IV = " SE_beta1_IV ******************************************************************************** *============================================================== * R-squared Equals the Squared Correlation in Simple Regression *============================================================== *-------------------------------------------------------------- * Regress x on z *-------------------------------------------------------------- reg x z * Store R-squared from the regression scalar R2_xz = e(r2) *-------------------------------------------------------------- * Compute sample correlation between x and z *-------------------------------------------------------------- corr x z * Store correlation coefficient matrix C = r(C) scalar rho_xz = C[1,2] *-------------------------------------------------------------- * Compare R-squared and squared correlation *-------------------------------------------------------------- scalar rho_xz_sq = rho_xz^2 display "R-squared from regression (x on z) = " R2_xz display "Squared correlation between x and z = " rho_xz_sq