* ASS1F09.DO log using ass1f09.txt, text replace * Program ass1f09.do September 2009 for Stata version 11 ********** OVERVIEW OF ASS1F09.DO ********** * This program does * (1) OLS using Stata command regress * (2) OLS using Stata MATA matrix commands * using wage-schooling data ********** SETUP ********** set mem 5m set more off version 11 ********** READ IN DATA, TRANSFORM AND SUMMARIZE ********** * See end of program for data description * Read in data use ivkling.dta * Descriptive Statistics sum wage76 grade76 age76 black col4 ********** ANALYSIS: OLS USING COMMAND REGRESS * OLS using regress regress wage76 grade76 age76 black regress wage76 grade76 age76 black, vce(robust) ********** ANALYSIS: OLS USING STATA MATA MATRIX PROGRAMMING LANGUAGE * Define an intercept generate cons = 1 * Invoke MATA to do OLS mata // Create y vector and X matrix from Stata data set using st_view command st_view(y=., ., "wage76") st_view(X=., ., ("grade76", "age76", "black", "cons")) // Compute OLS estimator b = invsym(X'X)*X'y // Compute variance-covariance matrix assuming independent heteroskedastic errors e = y - X*b n = rows(X) k = cols(X) esq = e:*e Vbhet = invsym(X'X)*(X'(X:*esq))*invsym(X'X)*(n/(n-k)) // Pass OLS estimates and covariance matrix to Stata using st_matrix command st_matrix("b",b') st_matrix("V",Vbhet) end * Back in Stata display nicely formatted results using command ereturn matrix colnames b = grade76 age76 black _cons matrix colnames V = grade76 age76 black _cons matrix rownames V = grade76 age76 black _cons ereturn post b V ereturn display ********** MATRIX OPERATORS IN MATA ********** * Operators are * A+B matrix addition * A-B matrix substraction * A*B matrix multiplication * A/b only for division by a scalar b * A'B matrix multiplication where first transpose A * In addition are element-by element operations using the colon operator * In particular for element-by-lement multiplication * A:*B has ij element A_ij x B_ij where A and B are both mxn * A:*b has ij element A_ij x b_i where A is mxn and b is mx1 * b:*A has ij element b_i x A_ij where A is mxn and b is mx1 * and similarly A:/b etc. for element-by-element division * For explanation in Stata give command * help m2 op_colon * Such operators are a key useful feature of matrix programming languages. ********** DATA DESCRIPTION ********** * Program mma4p4ivweak.do based on Kling Analys66.d0 September 2003 * written for Jeffrey R. Kling (2001) "Interpreting Instrumental Variables Estimates * of the Return to Schooling", Journal of Business and Economic Statistics, * July 2001, 19 (3), pp.358-364. * This program focuses on Columns (1) and (2) of Kling's Table 1 on p.359 * in turn based on * David Card (1995), "Using Geographic Variation in College Proximity to * Estimate the Returns to Schooling", in * Aspects of Labor Market Behavior: Essays in Honor of John Vanderkamp, * eds. L.N. Christofides et al., Toronto: University of Toronto Press, pp.201-221. ********** CLOSE OUTPUT ********** * log close * clear * exit