/* Topics in Data Analysis: Panel Data Models (Winter 2012) homework4.do: fit a random effect model Source: http://www.stern.nyu.edu/wgreene/Econometrics/WHO-data.xls Written by Hun Myoung Park, kucc625@iuj.ac.jp Last modified on 02/28/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 "homework4_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 // Q.1 ########################################################################## use who_health, clear global OLS = "comp hexp hc3" global group = "country" global D1 = "year93 year94 year96 year97" //global D1 = "year93 year94 year96" regress \$OLS // pooled OLS global sse_ols = e(rss) // SSE of the pooled OLS global k = e(df_m) // k = K-1 regress \$OLS \$D1 // LSDV1 global sse_within = e(rss) global df_within = e(df_r) // n-K = n-k-1 global nT = e(N) tsset country year xtreg \$OLS, fe i(\$group) global sse_within = e(rss) global df_within = e(df_r) // n-K = n-k-1 global nT = e(N) // nT global n = e(N_g) // n global T = \$nT / \$n // T di e(rss) " " e(df_r) // Same as those of LSDV areg \$OLS, absorb(\$group) foreach i of global OLS { egen gm_`i' = mean(`i'), by(\$group) // obtain group means gen dev_`i' = `i' - gm_`i' // calculate deviations from group means } global FE_OLS "dev_comp dev_hexp dev_hc3" regress \$FE_OLS, noc save random, replace // Q.2 ########################################################################## // 2.1 get a new data set of group means collapse gm_comp=comp gm_hexp=hexp gm_hc3=hc3, by(\$group) global GM_OLS = "gm_comp gm_hexp gm_hc3" list \$GM_OLS regress \$GM_OLS // between estimation global sse_between=e(rss) // SSE of the between estimation global df_between = e(df_r) // 2.2 use random, clear xtreg \$OLS, be i(\$group) // between estimation //global sse_between=e(rss) // SSE of the between estimation //global df_between = e(df_r) // 2.3 di \$sse_between // 2.4 global sigma2_between = \$sse_between / \$df_between di \$sigma2_between // This is MSE of the between estimation // 2.5 calculate sigma v (or e) squared global sigma2_v = \$sse_within / (\$n * \$T - \$n - \$k) di \$sigma2_v // 2.6 calculate sigma u squared global sigma2_u = \$sigma2_between - \$sigma2_v/\$T di \$sigma2_u // 2.7 calculate the theta global theta = 1-sqrt(\$sigma2_v/(\$T*\$sigma2_u+\$sigma2_v)) di \$theta // 2.8 transform DV and IVs (including the intercept) using the theta. foreach i of global OLS { gen ran_`i' = `i' - \$theta*gm_`i' } gen ran_one = 1- \$theta // 2.9 OLS with transformed variables: random effect model global RE_OLS = "ran_comp ran_hexp ran_hc3 ran_one" regress \$RE_OLS, noc // Q.3 ########################################################################## // 3.1 random effect model xtreg \$OLS, re i(\$group) theta di "Sigma2 v: " e(sigma_e)^2 di "Sigma2 u: " e(sigma_u)^2 // 3.2 di "SEE: " e(rmse) di "SSE: " (e(rmse)^2)*(\$n*\$T-\$k-1) // 3.3 xtreg matrix list e(b) matrix list e(V) // 3.4 di "sigma2 v: " e(sigma_e)^2 " Calculated sigma v squared: " \$sigma2_v // 3.5 di "sigma2 u: " e(sigma_u)^2 " Calculated sigma u squred: " \$sigma2_u // 3.6 di "sigma v(e): " e(sigma_e) " sigma u: " e(sigma_u) " rho : " e(rho) di e(sigma_u)^2/(e(sigma_u)^2 + e(sigma_e)^2) // 3.7 di "Theta: " e(theta) " Calculated theta hat: " \$theta // Q.4 ########################################################################## // 4.1 // rho is the proportion of group specific disturbance to the overall disturbance // 4.2 // Gauss-Markov assumption 2 (disturbances are not correlated with any regressor) // If violated, a fixed efect model is problematic. // 4.3 // i.i.d assumption for u and v (components of disturbances) // Q.5 ########################################################################## // 5.1 regress \$OLS predict r, residual // obtain residuals gen r2 = r^2 // compute squared residuals sum r2 global r2_sum = r(sum) // sum of squred residuals (e'e) = SSE of the pooled OLS di "Sum of squared residuals = " \$r2_sum // 5.2 obtain a data set of group means of residuals collapse gm_r = r, by(\$group) gen gm_r2 = gm_r^2 // compute squared group means of residuals list sum gm_r2 global gm_r2_sum = r(sum) // sum of squared group means of residuals di "Sum of squared group means of residuals = " \$gm_r2_sum // 5.3 di "n= " \$n " T= " \$T " k= " \$k // 5.4 di "ee of group mean = " \$gm_r2_sum " ee of pooled OLS = " \$r2_sum global lm = (\$n * \$T)/(2*(\$T-1))*( ( ( (\$T^2)*\$gm_r2_sum )/\$r2_sum ) -1)^2 di "LM statistic = " \$lm di chi2tail(1, \$lm) // p-value // 5.5 use random, clear xtreg \$OLS, re i(\$group) theta xttest0 // 5.6 // Q.6 ########################################################################## // 6.1 xtmixed \$OLS || \$group:, // 6.2 // 6.3 // 6.4 xtreg \$OLS, re i(\$country) mle // 6.5 xtmixed \$OLS || \$group:, mle // 6.6 log close // Random Effect Model