********************************************************************
*
* Figure C1: Falsification in no-speech states: Procedure validation
*
********************************************************************

global program "FigureC1"

cap log close
cap log using "$log_files/log_$program$today", replace

clear all
set mem 2g
set more off
set matsize 11000
set scheme s1color

***************************
***** 1. PREPARE DATA *****
***************************

// Get data

use "$data/Data_County", clear

// Controls definition

global geog 		"railroad_dens ldist_capital ldist_largest" 
global demo 		"urban_dum perc_pop_dstr lpop_total perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig" 
global demo_cont	"perc_pop_dstr lpop_total perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig" 
global econ 		"perc_farm perc_manuf dlog_avbasket ldist_silver ldist_gold" 
global econ_cont	"perc_farm perc_manuf dlog_avbasket ldist_silver ldist_gold" 
global econ_p 		"perc_farm perc_manuf log_avbasket ldist_silver ldist_gold" 
global vote 		"lag_margin_pres_ttl" 			
global demo_add		"urban_dum lpop_total larea perc_white perc_below21 perc_native perc_literate newsp_circ_elig pop_male2144 pop_male4564 pop_male6599 perc_catholic perc_black"

// Id

sort county year
cap g id=_n
cap g nospeech=1*(speech==0 & state_speech==1)

tempfile  tmp
save `tmp.dta', replace

// Fake treatment

use `tmp.dta', clear

tab state_speech, miss
recode state_speech (0=1) (1=0), g(nostate_speech)
tab nostate_speech, miss

sort id

qui teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (nostate_speech) if year==1896 & railroad_dum==1, ///
	metric(euclidean) biasadj(lag_dem_pres_ttl lag_pop_pres_ttl $demo_cont $geog $econ_cont) vce(robust) nneighbor(1) generate(nn)

// Pair id
	
egen pair_id=group(id nn1)

tempfile  tmp
save `tmp.dta', replace

// Get pairs

use `tmp.dta', clear

keep if nostate_speech==1
keep id nn pair_id

drop if id==. | nn1==.

merge m:1 id using `tmp.dta', keepusing(county)
drop if _merge!=3
drop _merge id

rename county county_false

rename nn1 id
merge m:1 id using `tmp.dta', keepusing(county speech)
drop if _merge!=3
drop _merge id

rename county county_true
rename speech speech_false

// Reshape to keep pairs

reshape long county_, i(pair_id) j(sample) string
rename county_ county

tempfile  tmp
save `tmp.dta', replace

// Expand to get county characteristics

use "$data/Data_County", clear

keep year
duplicates drop

cross using `tmp.dta'

// Get county characteristics

merge m:1 county year using "$data/Data_County"
drop if _merge!=3
drop _merge

tempfile  tmp
save `tmp.dta', replace

*****************************
***** 2. FIGURE RESULTS *****
*****************************

use `tmp.dta', clear

g false=1*(sample=="false")
tab false sample

bysort year: center dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ, inplace standardize

// Differences

forvalues s=0(1)1{
foreach var in dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename false `var'
	reghdfe outcome `var' if year==1896 & speech_false==`s', cluster(county) noabsorb
	estimates store R_`var'_`s'
	rename `var' false 
	rename outcome `var'
}
global OLS_estimates_`s' "R_lag_dem_pres_ttl_`s' \ R_lag_pop_pres_ttl_`s' \ R_lpop_total_`s' \ R_urban_dum_`s' \ R_ldist_capital_`s' \ R_ldist_largest_`s' \ R_perc_white_`s' \ R_perc_native_`s' \ R_perc_male21_`s' \ R_perc_below21_`s' \ R_perc_literate_`s' \ R_perc_farm_`s' \ R_perc_manuf_`s' \ R_dlog_avbasket_`s' \ R_newsp_circ_elig_`s' \ R_ldist_silver_`s' \ R_ldist_gold_`s'"
}

// Difference in differences

foreach var in dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	g `var'=false*speech_false
	reghdfe outcome false speech_false `var' if year==1896, cluster(county) noabsorb
	estimates store R_`var'
	drop `var'
	rename outcome `var'
}

global OLS_estimates "R_lag_dem_pres_ttl \ R_lag_pop_pres_ttl \ R_lpop_total \ R_urban_dum \ R_ldist_capital \ R_ldist_largest \ R_perc_white \ R_perc_native \ R_perc_male21 \ R_perc_below21 \ R_perc_literate \ R_perc_farm \ R_perc_manuf \ R_dlog_avbasket \ R_newsp_circ_elig \ R_ldist_silver \ R_ldist_gold"

****************************
***** 3. EXPORT FIGURE *****
****************************

// Labels

la var lag_dem_pres_ttl 	"Democratic 1892 (%)"
la var lag_pop_pres_ttl 	"Populist 1892 (%)"
la var lpop_total 			"Population (log)"
la var urban_dum			"Urban county (binary)"
la var ldist_capital 		"Dist to state capital (log)"
la var ldist_largest 		"Dist to state largest city (log)"
la var perc_white 			"White population (%)"
la var perc_native 			"Native population (%)"
la var perc_male21 			"Male above 21 (%)"
la var perc_below21			"Aged below 21 (%)"
la var perc_literate 		"Literacy (%)"
la var perc_farm 			"Farmer households (%)"
la var perc_manuf 			"Manufacture workers (%)"
la var dlog_avbasket		"Crop value change (log)"
la var newsp_circ_elig		"News circulation (per voter)"
la var ldist_silver			"Dist to silver mine (log)"
la var ldist_gold 			"Dist to gold mine (log)"

// Figure

coefplot ($OLS_estimates_0), drop(_cons) bylabel("No speech") /// 
	|| ($OLS_estimates_1), drop(_cons) bylabel("Speech") /// 
	|| ($OLS_estimates), drop(_cons false speech_false) bylabel("Diff-in-diff") /// 
	||, xlabel(-1 "-1" -0.5 0 "0" 0.5 1 "1", labsize(vsmall) format(%9.1f)) ylabel(, angle(0) labsize(vsmall)) /// 
	horizontal mcolor(navy) msymbol(S) msize(vsmall) mfcolor(white) pstyle(p3) fcolor(*.5) ///
	levels(90) ciopts(recast(rcap) lc(navy*.5) lwidth(thin)) citop ///
	msize(tiny) mlabel mlabposition(12) mlabcolor(navy) mlabgap(*1) mlabsize(tiny) format(%9.1f) ///
	xline(0, lwidth(thin) lpattern(dash)) xtitle("Difference between falsified and true sample in 1896 (std.dev.)", margin(small) size(small)) /// 
	graphregion(color(white) lstyle(none)) subtitle(, size(small) bmargin(top) margin(vsmall) color(white) bcolor(navy)) ///
	byopts(cols(6) legend(off) bgcolor(white) graphregion(fcolor(white) lstyle(none))) //	
graph export "$figures/$program.pdf", replace

cap log close