/*----------------------------------------------------------------------
 
 REPLICATION FILE FOR
 Gerber, Alan S., Gregory A. Huber, Albert H. Fang, and Andrew Gooch. (Forthcoming)
 "The Generalizability of Social Pressure Effects on Turnout Across High-Salience Electoral Contexts:
  Field Experimental Evidence from 1.96 Million Citizens in 17 States"
 American Politics Research
 
 FILE: 			02_Analysis.do
 DESCRIPTION: 	Performs analyses for all tables and figures 
				reported in main paper
 DATE: 			14 Dec 2016
 VERSION: 		1.0

----------------------------------------------------------------------*/
log using "02_Analysis.log", replace


/*========================
 TABLE 2
 Ns By State
========================*/

use PublicReplicationData.dta, clear
set matsize 11000

* Keep sample used for analysis
keep if (state != "NC" & hhsize == 1 & flag_drop_hhid == 0 & vhavg != .) | ///
		(state=="NC" & hhsize == 1 & i_age >= 23 & flag_drop_hhid == 0 & vhavg != .)

* Create table
tabout state treat using "Tables/Table2_NsByState.xls", replace cells(freq row)


/*========================
 TABLE 4
 ITT Effect Estimates
========================*/

use PublicReplicationData.dta, clear
set matsize 11000

* Keep sample used for analysis
keep if (state != "NC" & hhsize == 1 & flag_drop_hhid == 0 & vhavg != .) | ///
		(state=="NC" & hhsize == 1 & i_age >= 23 & flag_drop_hhid == 0 & vhavg != .)

* Set local macros defining variable sets
local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)
foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)
foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)
foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)
foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* Re-define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)
local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* column 1
reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_exnclt23_vvhvid]
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table4_ITTEstimates.xls", replace ctitle("Any Report Card") label keep(treat `vhstrata' `states' `covariates') dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
* column 2
gen treat_below = treat * vhblw
gen treat_above = treat * vhabv
label var treat_below "Report Card * Below Median"
label var treat_above "Report Card * Above Median"

reg voted14 treat treat_below treat_above `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_exnclt23_vvhvid]
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & vhblw == 1 & e(sample)
local c_turnout_below = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & vhblw == 0 & vhabv == 0 & e(sample)
local c_turnout_median = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & vhabv == 1 & e(sample)
local c_turnout_above = r(mean)
outreg2 using "Tables/Table4_ITTEstimates.xls", append ctitle("Report Card by,Vote History Stratum") label keep(treat treat_below treat_above `vhstrata' `states' `covariates') dec(3) ///
	addstat("Control Group Mean Turnout", `c_turnout', "Control Group Mean Turnout, Below State Median Stratum", `c_turnout_below', "Control Group Mean Turnout, At State Median Stratum", `c_turnout_median', "Control Group Mean Turnout, Above State Median Stratum", `c_turnout_above') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)



/*========================
 TABLE 5
 Sensitivity to alternative
  sample definitions
========================*/

use PublicReplicationData.dta, clear
set matsize 11000

/* --- Column 1: No exclusions --- */

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata r_vhblw r_vhabv

* Impute if missing vote history stratum and add flag

gen r_vhblw = vhblw
replace r_vhblw = 2 if r_vhblw == .
gen r_vhabv = vhabv
replace r_vhabv = 2 if r_vhabv == .
gen vhblw_miss = 0
gen vhabv_miss = 0
replace vhblw_miss = 1 if r_vhblw == 2
replace vhabv_miss = 1 if r_vhabv == 2

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata r_vhblw r_vhabv vhblw_miss vhabv_miss

* estimate model and save result

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_p_noex]
qui sum voted14 [aweight=ipw_p_noex] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table5_SensitivityAltSampDefs.xls", replace ctitle("No Exclusions") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
drop Z1_* Z2_* Z3_* Z4_*
drop r_vhblw r_vhabv vhblw_miss vhabv_miss


/* --- Column 2: Exclude if vote history unknown at baseline --- */

preserve

drop if vote_hist == .

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* estimate model and save result

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_p_vvh]
qui sum voted14 [aweight=ipw_p_vvh] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table5_SensitivityAltSampDefs.xls", append ctitle("Exclude if Vote, History Unknown, at Baseline") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
restore

/* --- Column 3: Exclude if vote history unknown at baselnie and if under 23 in NC --- */

preserve

keep if (state != "NC" & vhavg != .) | ///
		(state=="NC" & i_age >= 23 & vhavg != .)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* estimate model and save result

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_p_exnclt23_vvh]
qui sum voted14 [aweight=ipw_p_exnclt23_vvh] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table5_SensitivityAltSampDefs.xls", append ctitle("Exclude if Unknown, Vote History and, Under 23 in NC") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
restore

/* --- Column 4: Exclude if vote history unknown at baseline and if household size > 1 --- */

preserve

keep if (hhsize == 1 & vhavg != .)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* estimate model and save result

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_vvhvid]
qui sum voted14 [aweight=ipw_h_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table5_SensitivityAltSampDefs.xls", append ctitle("Exclude if Unknown, Vote History and, HH Size > 1") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
restore

/* --- Column 5: Exclude if vote history unknown at baseline AND if household size > 1 AND if under 23 in NC --- */

preserve

keep if (state != "NC" & hhsize == 1 & flag_drop_hhid == 0 & vhavg != .) | ///
		(state=="NC" & hhsize == 1 & i_age >= 23 & flag_drop_hhid == 0 & vhavg != .)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* estimate model and save result

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_exnclt23_vvhvid]
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table5_SensitivityAltSampDefs.xls", append ctitle("Exclude if Unknown, Vote History and, HH Size > 1 and, Under 23 in NC") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
restore

/* --- Column 6: Exclude if vote history unknown at baseline AND if household size > 1 AND if in North Carolina --- */

preserve

keep if state != "NC" & hhsize == 1 & flag_drop_hhid == 0 & vhavg != .

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* Estimate and output results

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* estimate model and save result

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_exnclt23_vvhvid]
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/Table5_SensitivityAltSampDefs.xls", append ctitle("Exclude if Unknown, Vote History and, HH Size > 1 and, in N. Carolina") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes, "With Stratum-Covariate Interactions?", Yes, "With State-Stratum-Covariate Interactions?", Yes)
	
restore


/*========================
 TABLE 6
 ITT effects by state
========================*/
use PublicReplicationData.dta, clear
set matsize 11000

keep if (state != "NC" & hhsize == 1 & flag_drop_hhid == 0 & vhavg != .) | ///
		(state=="NC" & hhsize == 1 & i_age >= 23 & flag_drop_hhid == 0 & vhavg != .)
		
* Construct interactions

* Define local macros with sets of variables

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Estimate Effect of Any Report Card Treatment, By State
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female
local vhstrata vhblw vhabv

capture erase "Tables/Table6_PanelA_Estimates.dta"
tempname tempfile
postfile `tempfile' str2 state b_treat se_treat N df_res c_turnout t_turnout t_turnout_se using "Tables/Table6_PanelA_Estimates.dta"

levelsof state, local(stlevels)
foreach st in `stlevels' {

reg voted14 treat `vhstrata' `covariates' Z3_* [aweight=ipw_h_exnclt23_vvhvid] if state=="`st'"
local numobs e(N)
local dfres = e(df_r)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 1 & e(sample)
local t_turnout = r(mean)
local t_turnout_se = r(sd) / sqrt(r(N))
post `tempfile' ("`st'") (_b[treat]) (_se[treat]) (`numobs') (`dfres') (`c_turnout') (`t_turnout') (`t_turnout_se')

if ("`st'" == "AK"){
outreg2 using "Tables/Table6_PanelA_ByState.xls", replace ctitle("`st'") label keep(treat `vhstrata' `covariates') dec(3) noaster ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)
}
else {
outreg2 using "Tables/Table6_PanelA_ByState.xls", append ctitle("`st'") label keep(treat `vhstrata' `covariates') dec(3) noaster ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)
}
}

postclose `tempfile'


* Estimate Effect of Report Card by Vote History Stratum, By State
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female
local vhstrata vhblw vhabv
gen treat_below = treat * vhblw
gen treat_above = treat * vhabv
label var treat_below "Report Card * Below Median"
label var treat_above "Report Card * Above Median"

capture erase "Tables/Table6_PanelB_Estimates.dta"
tempname tempfile
postfile `tempfile' str2 state b_treat se_treat b_tbelow se_tbelow b_tabove se_tabove b_below se_below b_above se_above N df_res c_turnout c_turnout_below c_turnout_median c_turnout_above using "Tables/Table6_PanelB_Estimates.dta"

levelsof state, local(stlevels)
foreach st in `stlevels' {

reg voted14 treat treat_below treat_above `vhstrata' `covariates' Z3_* [aweight=ipw_h_exnclt23_vvhvid] if state=="`st'"
local numobs e(N)
local dfres = e(df_r)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & vhblw == 1 & e(sample)
local c_turnout_below = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & vhblw == 0 & vhabv == 0 & e(sample)
local c_turnout_median = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & vhabv == 1 & e(sample)
local c_turnout_above = r(mean)

post `tempfile' ("`st'") (_b[treat]) (_se[treat]) (_b[treat_below]) (_se[treat_below]) (_b[treat_above]) (_se[treat_above]) /// 
	(_b[vhblw]) (_se[vhblw]) (_b[vhabv]) (_se[vhabv]) (`numobs') (`dfres') (`c_turnout') (`c_turnout_below') (`c_turnout_median') (`c_turnout_above')

if("`st'" == "AK") {
outreg2 using "Tables/Table6_PanelB_ByStateByVoteHistory.xls", replace ctitle("`st'") label keep(treat treat_below treat_above `vhstrata' `covariates') dec(3) noaster ///
	addstat("Control Group Mean Turnout", `c_turnout', "Control Group Mean Turnout, Below State Median Stratum", `c_turnout_below', "Control Group Mean Turnout, At State Median Stratum", `c_turnout_median', "Control Group Mean Turnout, Above State Median Stratum", `c_turnout_above') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)
}
else {
outreg2 using "Tables/Table6_PanelB_ByStateByVoteHistory.xls", append ctitle("`st'") label keep(treat treat_below treat_above `vhstrata' `covariates') dec(3) noaster ///
	addstat("Control Group Mean Turnout", `c_turnout', "Control Group Mean Turnout, Below State Median Stratum", `c_turnout_below', "Control Group Mean Turnout, At State Median Stratum", `c_turnout_median', "Control Group Mean Turnout, Above State Median Stratum", `c_turnout_above') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)
}
	
}

postclose `tempfile'

/*========================
 TABLE 7
 ITT effects by salience
========================*/
use PublicReplicationData.dta, clear
set matsize 11000

* Keep sample used for analysis
keep if (state != "NC" & hhsize == 1 & flag_drop_hhid == 0 & vhavg != .) | ///
		(state=="NC" & hhsize == 1 & i_age >= 23 & flag_drop_hhid == 0 & vhavg != .)

* Set local macros defining variable sets

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Construct state-by-covariate interactions (Z1)

foreach st in `states' {
	foreach v in `covariates' {
		gen Z1_`st'_`v' = `st' * `v'
		quietly sum Z1_`st'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z1_`st'_`v'
		}
	}
}

* Construct state-by-stratum interactions (Z2)

foreach st in `states' {
	foreach vh in `vhstrata' {
		gen Z2_`st'_`vh' = `st' * `vh'
		quietly sum Z2_`st'_`vh' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z2_`st'_`vh'
		}
	}
}

* Construct stratum-by-covariate interactions (Z3)

foreach vh in `vhstrata' {
	foreach v in `covariates' {
		gen Z3_`vh'_`v' = `vh' * `v'
		quietly sum Z3_`vh'_`v' 
		if ( r(sd) == . | r(sd) == 0 ){
			drop Z3_`vh'_`v'
		}
	}
}

* Construct state-by-stratum-by-covariate interactions (Z4)

foreach st in `states' {
	foreach v in `covariates' {
		foreach vh in `vhstrata' {
			gen Z4_`st'_`v'_`vh' = `st' * `v' * `vh'
			quietly sum Z4_`st'_`v'_`vh' 
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

* define predictors as local macros -- note `covariates' now includes age_miss (was excluded previously when creating interaction terms)

local states d_st_AR d_st_AZ d_st_CO d_st_FL d_st_GA d_st_IA d_st_KS d_st_KY d_st_LA d_st_ME d_st_MI d_st_NC d_st_NH d_st_SD d_st_TX d_st_WI
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

* Estimate for each state; save output

capture erase "Tables/Table7_BySalience_Estimates.dta"
tempname tempfile
postfile `tempfile' salience_score b_treat se_treat N df_res c_turnout t_turnout t_turnout_se using "Tables/Table7_BySalience_Estimates.dta"

levelsof salience, local(svals)

foreach sval in `svals' {

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_exnclt23_vvhvid] if salience == `sval'
local numobs e(N)
local dfres = e(df_r)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & e(sample)
local c_turnout = r(mean)
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 1 & e(sample)
local t_turnout = r(mean)
local t_turnout_se = r(sd) / sqrt(r(N))
post `tempfile' (`sval') (_b[treat]) (_se[treat]) (`numobs') (`dfres') (`c_turnout') (`t_turnout') (`t_turnout_se')

if (`sval' == 1){
outreg2 using "Tables/Table7_BySalience.xls", replace ctitle("Salience=`sval'") label keep(treat `vhstrata' `covariates') dec(3)  ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)
}
else {
outreg2 using "Tables/Table7_BySalience.xls", append ctitle("Salience=`sval'") label keep(treat `vhstrata' `covariates') dec(3)  ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)
}

}

postclose `tempfile'



/*========================
 FIGURE 2
 Plot state-specific ITTs
 and pooled ITT estimate
========================*/

use "Tables/Table6_PanelA_Estimates.dta" , clear
set scheme s2mono

egen stateid = group(state)
tab state stateid, m
labmask stateid, values(state)

serrbar b_treat se_treat stateid, scale(1.96) mvopts(msize(1.2)) xlabel(, valuelabel) /// 
	xlab(1(1)17) ylab(-.05(.05).1) yline(.007, lpa(dash)) ///
	xtitle("State") ///
	ytitle("Estimated ITT Effect of the Report Card Treatment")
graph export "Figures/Figure2.pdf", replace



log close
