/* Topics in Data Analysis: Panel Data Models (Winter 2012) homework3.do: answer key for homework assignment 3 Source: http://www.stern.nyu.edu/wgreene/Econometrics/WHO-data.xls Written by Hun Myoung Park, kucc625@iuj.ac.jp Last modified on 02/25/2012 *******************************************************************************/ memory // to check memory allocation version 12 // to set the version of interpreter set more off // to tell Stata not to pause when output is long cd c:\temp // to set a default directory // ############################################################################# log using "hw3_log.txt", text replace // import an excel file import excel using WHO.xls, firstrow clear // In version 11 or earlier //version 10 //insheet using WHO.csv, comma names clear // change variable names from uppercase to lowercase foreach v of varlist Zimbabwe-LGDPC2 { local new_v = lower("`v'") // local macro rename `v' `new_v' } save WHO, replace // store original file rename country cnt_code gen country = real(cnt_code) // convert a string to a number keep if groupti==5 // get balanced panel data keep if oecd==1 forvalues i=93/97 { gen year`i'=(year==19`i') // e.g., gen year93=(year==1993) } save homework3, replace // Question 1 ################################################################## global OLS = "comp hexp hc3 gini geff" global D0 = "year93 year94 year95 year96 year97" global D1 = "year93 year94 year96 year97" regress \$OLS // pooled OLS regress \$OLS \$D1 // LSDV1 global df_lsdv = e(df_r) // Question 2 ################################################################## // 2.1 foreach i of global OLS { egen gm_`i' = mean(`i'), by(year) gen dev_`i' = `i' - gm_`i' } regress dev_comp dev_hexp dev_hc3 dev_gini dev_geff, noc // 2.2 di "F = " e(F) " SSM: " e(mss) di "SSE: " e(rss) " SEE: " e(rmse) " Df of Error: " e(df_r) di "R-squared: " e(r2) " Adjusted R-squared: " e(r2_a) // 2.3 quietly regress global df_within = e(df_r) matrix b = e(b) matrix V = e(V) mata // Beginning of Mata language b = st_matrix("e(b)") vc = st_matrix("e(V)") se = sqrt(diagonal(vc)) (b', se) // 2.4 se2 = se *sqrt(\$df_within/\$df_lsdv) (b', se2) end // End of Mata session // 2.5 di "Parameter estimate: " b[1,2] " SE: " sqrt(V[2,2]) " T: " b[1,2]/sqrt(V[2,2]) // 2.6 regress \$OLS \$D0, noc collapse gm_comp=comp gm_hexp=hexp gm_hc3=hc3 gm_gini=gini gm_geff=geff, by(year) global T = _N local gm_var = "gm_comp gm_hexp gm_hc3 gm_gini gm_geff" forvalues i=1/\$T { di gm_comp[`i']-(gm_hexp[`i']*b[1,1]+gm_hc3[`i']*b[1,2]+gm_gini[`i']*b[1,3]+gm_geff[`i']*b[1,4]) } // Question 3 ################################################################## use homework3, clear // 3.1 tsset year country xtreg \$OLS, fe i(year) // 3.2 di "F = " e(F) " SSM: " e(mss) di "SSE: " e(rss) " SEE: " e(rmse) " Df of Error: " e(df_r) di "R-squared: " e(r2) " Adjusted R-squared: " e(r2_a) // 3.3 quietly xtreg matrix b = e(b) matrix V = e(V) mata // Beginning of Mata language b = st_matrix("e(b)") vc = st_matrix("e(V)") se = sqrt(diagonal(vc)) (b', se) end // end of Mata language // 3.4 local t = b[1,1]/sqrt(V[1,1]) di "Parameter estimate: " b[1,1] " SE: " sqrt(V[1,1]) " T: " `t' " p-value: " ttail(\$df_within, `t')*2 // 3.5-3.7 quietly xtreg di "F statistic: " e(F_f) " p-value: " Ftail(e(df_a), e(df_r), e(F_f)) // Question 4 ################################################################## use homework3, clear // 4.1 areg \$OLS, absorb(year) matrix b = e(b) matrix V = e(V) // 4.2 di "F = " e(F) " SSM: " e(mss) di "SSE: " e(rss) " SEE: " e(rmse) " Df of Error: " e(df_r) di "R-squared: " e(r2) " Adjusted R-squared: " e(r2_a) // 4.3 di "Intercept: " b[1,5] " SE: " sqrt(V[5,5]) // This intercept is the averaged intercepts of all five years // 4.4 // .areg is useful when you want to get R2 without creating dummy variables // Question 5 ################################################################## use homework3, clear global OLS2 = "comp hexp hc3" // 5.1 xtreg \$OLS2, fe i(country) matrix b = e(b) matrix V = e(V) // 5.2 mata // Beginning of Mata language b = st_matrix("e(b)") vc = st_matrix("e(V)") se = sqrt(diagonal(vc)) (b', se) end // 5.3 quietly xtreg di "F statistic: " e(F_f) " p-value: " Ftail(e(df_a), e(df_r), e(F_f)) // The following is to check the F-test using LSDV1 sort year country gen cnt_id = 0 forvalues i=1/30 { replace cnt_id = `i' if mod(_n,30) == `i' } replace cnt_id = 30 if cnt_id==0 tab cnt_id forvalues j=1/30 { gen d`j' = (cnt_id==`j') } regress \$OLS2 d1-d29 test d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 d16 d17 d18 d19 d20 /// d21 d22 d23 d24 d25 d26 d27 d28 d29 log close // End of homework assignment 3