************ * Tutorial * * ExampleD * ************ *** Combined Index *** ** Mean Value analysis (Baseline) ** * Loading the data clear use "C:\Users\fbizz\Dropbox\Dissertation\Data\vdem_cy_v5.dta" * Setting up the dataset xtset country_id year * Generating Multiplicative Index qui gen edi_mult = v2xel_frefair * v2x_frassoc_thick * v2x_freexp_thick * v2x_accex * v2x_suffr * Baseline Model xtreg F.e_peinfmor edi_mult e_migdppcln, fe vce(cluster country_id) *** Incorporating Measurement Error *** * Make sure you downloaded all the posteriors in the same folder clear local files : dir "C:\Users\fbizz\Dropbox\ShamrockSeries\Fernando\tutorial_CIs\ind_posteriors" files "*.csv" cd "C:\Users\fbizz\Dropbox\ShamrockSeries\Fernando\tutorial_CIs\ind_posteriors" foreach file in `files' { import delimited using `file', clear * generate year and country_text_id gen country_text_id = substr(v1,1,3) gen year = substr(v1,5,4) destring year, replace * when there are multiple observations for a particular country-year, keep the oldest observation (i.e. toward the end of the year rather than the beginning) gen obs_sort =_n gsort -obs_sort duplicates drop country_text_id year, force sort obs_sort drop v1 obs_sort local varname = substr("`file'",1,length("`file'")-4) if "`varname'" == "frefair" local varname2 = "v2xel_`varname'" /* change this if you have other variables */ else if "`varname'" == "frefair" local varname2 = "v2x_`varname'" * add country_id merge 1:1 country_text_id year using "C:/Users/fbizz/Dropbox/Dissertation/Data/vdem_cy_v5.dta", keepusing(country_id `varname2') drop _merge * carry forward and rename variables xtset country_id year foreach var of varlist v2 - v901{ qui bysort country_id: carryforward `var', gen (`varname'_`var') drop `var' replace `varname'_`var' = normal(`varname'_`var') qui replace `varname'_`var' = . if missing(`varname2') } order country_text_id country_id year `varname2' save `varname'_post.dta, replace } * * Merging with V-Dem Dataset clear matrix drop _all set matsize 5000 local files : dir "C:\Users\fbizz\Dropbox\ShamrockSeries\Fernando\tutorial_CIs\ind_posteriors" files "*.dta" cd "C:\Users\fbizz\Dropbox\ShamrockSeries\Fernando\tutorial_CIs\ind_posteriors" use "C:/Users/fbizz/Dropbox/Dissertation/Data/vdem_cy_v5.dta", clear keep country_id country_name year e_migdppcln e_peinfmor v2x_accex v2x_suffr foreach file in `files' { merge 1:1 country_id year using `file', nogenerate } * generate multiplicative indices forvalues i = 2(1)901 { qui gen edi_mult_`i' = frefair_v`i' * frassoc_thick_v`i' * freexp_thick_v`i' * v2x_accex * v2x_suffr drop frefair_v`i' frassoc_thick_v`i' freexp_thick_v`i' } ***Monte Carlo Estimates Using 900 Draw Posterior Distribution*** *Run the monte carlo forvalues i = 2(1)901 { *Print out an iteration number display `i' *Fit the model, using the ith draw from the UDS posterior quietly xtreg F.e_peinfmor edi_mult_`i' e_migdppcln, fe vce(cluster country_id) *Extract the coefficients and variance-covariance matrix matrix b = e(b) matrix V = e(V) local blength = colsof(b) matrix rsq = e(r2) *Preserve the dataset, take a single multivariate normal draw from the *posterior distribution of the coefficients, and restore the dataset. *We use the capture command to catch possible errors in drawnorm *and drop these iterations gracefully. preserve capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear if _rc==0 { mkmat b1-b`blength', matrix(bsample) matrix posterior = nullmat(posterior) \ bsample matrix rsquared = nullmat(rsquared) \ rsq } else { display "Error drawing sample...iteration dropped" } restore *Closes the Monte Carlo Loop } *Get posterior ready to work with svmat posterior svmat rsquared *Calculate means and standard deviations tabstat posterior*, stat(mean sd) *Find the bounds of the 95 percent credible interval centile posterior*, centile(2.5, 97.5) * Find the R-Squared tabstat rsquared*, stat(mean sd)