/* Topics in Data Analysis: Panel Data Models (Winter 2012) hausman.do: Hausman and Poolability Tests Source: http://www.stern.nyu.edu/wgreene/Econometrics/WHO-data.xls Written by Hun Myoung Park, kucc625@iuj.ac.jp Last modified on 03/01/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 "hausman_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 who_health, replace use who_health, clear global MODEL = "comp hexp hc3" global D1 = "year93 year94 year96 year97" global group = "year" // ############################################################################# /* use http://www.indiana.edu/~statmath/stat/all/panel/airline.dta, clear global MODEL = "cost output fuel load" global group = "airline" */ sum \$group global min = r(min) // minimum value of the group variable global max = r(max) // maximum value of the group variable // ############################################################################# xtreg \$MODEL, fe i(\$group) // fit a random effect model scalar n = e(N_g) estimates store fixed_effect xtreg \$MODEL, re i(\$group) // fit a random effect model estimates store random_effect xttest0 // LM test for the random effect hausman fixed_effect random_effect // or .hausman fixed_effect //================== mata // beginning of mata session b_random = st_matrix("e(b)") // b of randome effect model k = cols(b_random) - 1 // count the number of regressors b_random = b_random[1, 1::k] // extract slopes of regressors only V_random = st_matrix("e(V)") // variance-covariance matrix of b V_random = V_random[1..k, 1::k] // extract k by k matrix for regressors b_fixed = st_matrix("e(bf)"); b_fixed = b_fixed[1, 1::k] // b of fixed effect model V_fixed = st_matrix("e(VCEf)"); V_fixed = V_fixed[1..k, 1::k] // variance-covariance of b hausman = (b_fixed - b_random)*invsym(V_fixed-V_random)*(b_fixed - b_random)' b_fixed; b_random // display b of fixed and random effect V_fixed; V_random // display variance-covariance matrices of fixed and randome effect models hausman // display hausman statistics (chi-squared) chi2tail(k, hausman) // calculate the p-value end // end of mata session //================== // ############################################################################# // pooled OLS regress \$MODEL scalar T = e(N)/n scalar k = e(df_m) scalar sse_pooled = e(rss) // scalar of the SSE of the pooled OLS scalar sse_separated = 0 // scalar of accumulated SSE of separated OLS // separated OLS forvalues i = \$min/\$max { display " " display "`i' ============" regress \$MODEL if \$group==`i' scalar sse = e(rss) scalar sse_separated = (sse_separated + sse) } di sse_pooled // SSE of the pooled OLS di sse_separated // Sum of SSE of separated OLS di "n: " n " T: " T " k (# regressors): " k scalar chow = (sse_pooled-sse_separated) / ((n-1)*(k+1)) /( sse_separated / (n*(T-k-1)) ) di "Chow test (F): " chow di Ftail((n-1)*(k+1), (n*(T-k-1)), chow) // ############################################################################# // chow test (original version) forvalues j = 93/97 { gen hexp`j' = hexp * year`j' gen hc3_`j' = hc3 * year`j' } regress comp hexp hc3 hexp93 hc3_93 year93 hexp94 hc3_94 year94 /// hexp95 hc3_95 year95 hexp96 hc3_96 year96 test hexp93 hexp94 hexp95 hexp96, notest test hc3_93 hc3_94 hc3_95 hc3_96, notest accum test year93 year94 year95 year96, accum testparm hexp93 hexp94 hexp95 hexp96 hc3_93 hc3_94 hc3_95 hc3_96 year93 year94 year95 year96 test (_b[hexp93]=0) (_b[hexp94]=0) (_b[hexp95]=0) (_b[hexp96]=0), notest test (_b[hc3_93]=0) (_b[hc3_94]=0) (_b[hc3_95]=0) (_b[hc3_96]=0), accum notest test (_b[year93]=0) (_b[year94]=0) (_b[year95]=0) (_b[year96]=0), accum log close // Hausman and Poolability Test