/*----------------------------------------------------------------------
 
 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: 			03_Appendix.do
 DESCRIPTION: 	Performs analyses for all tables and figures 
				reported in the supplemental appendix
 DATE: 			14 Dec 2016
 VERSION: 		1.0

----------------------------------------------------------------------*/
log using "03_Appendix.log", replace


/*========================
 TABLE A1
 Randomization Checks
========================*/

use PublicReplicationData.dta, clear
set matsize 11000

* Keep only subjects with valid vote history and valid household ID
keep if flag_drop_hhid == 0 & vhavg != .

* Randomization check using all data
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 d_hhsize2 d_hhsize3 d_hhsize4
local vhstrata vhblw vhabv
reg treat `covariates' `states' `vhstrata' [aweight=ipw_p_vvhvid] , vce(cluster hhid)
* The F-stat is 13899 and the F-test p-value is 0.0000, which is a problem and suggests a randomization failure

* Re-run randomization check restricting to 1-person households, since multi-person households have previously been an issue
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
reg treat `covariates' `states' `vhstrata' [aweight=ipw_p_vvhvid] , vce(cluster hhid)
* The F-stat is 32.18 and the F-test p-value is 0.0000, which suggests something else is happenign

* We know the randomization blocked by state, so we assess the randomization check in two steps, iteratively
* All subsequent analyses restrict sample to single-person households

* Step 1: Check, for all categorical covariates, whether there are some cells with small sample sizes by state
*   that may be perfectly predicting treatment assignment for those subjects.

foreach v in age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female vhblw vhabv {
	di "`v' == 0 by state"
	table state if `v' == 0 & hhsize == 1, c(n treat mean treat)
	di "`v' == 1 by state"
	table state if `v' == 1 & hhsize == 1, c(n treat mean treat)
}

* Step 2: Conduct state-by-state randomization checks regressing treatment on predictors
*   but omitting categorical predictors with small sample size that perfectly predict treatment by state;
*   also generate predicted value of voting in 2014 by state (predict, xb)

gen prvote14 = .
local vhstrata vhblw vhabv
levelsof state, local(stlevels)

foreach st in `stlevels' {
if("`st'"=="AK") { 
 local covariates i_age age2 age_miss voted06 voted08  voted10  voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
 }
if("`st'"=="AR") { 
local covariates  i_age age2  voted06 voted08  voted10  voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="AZ") { 
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 
}
if("`st'"=="CO") { 
local covariates  i_age age2  voted06 voted08  voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="FL") { 
local covariates  i_age age2  voted06 voted08  voted10  voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="GA") { 
local covariates  i_age age2  voted06 voted08  voted10  voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="IA") { 
local covariates  i_age age2  voted06 voted08 voted09 voted10  voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="KS") { 
local covariates  i_age age2  voted06 voted08 voted09 voted10  voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="KY") { 
local covariates  i_age age2  voted06 voted08  voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="LA") { 
local covariates  i_age age2  voted06 voted08  voted10  voted12  d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="ME") { 
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 
}
if("`st'"=="MI") { 
local covariates  i_age age2  voted06 voted08  voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="NC") { 
local covariates  i_age age2  voted06 voted08  voted10 voted11 voted12 voted13 d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="NH") { 
local covariates  i_age age2 age_miss voted06 voted08  voted10  voted12  d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="SD") { 
local covariates  i_age age2  voted06 voted08  voted10  voted12  d_race_b d_race_h d_race_o d_married d_female 
}
if("`st'"=="TX") { 
local covariates  i_age age2  voted06 voted08  voted10 voted11 voted12 d_race_b d_race_h d_race_o d_married d_female 
}
if("`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
}

di " ************ `st' : Predict Probability of Voting in 2014, by State, using Covariates Used for Balance Checks ************ "
reg voted14 `covariates' `vhstrata' if state =="`st'" & treat == 0 & hhsize == 1
predict prvote14`st', xb
replace prvote14 = prvote14`st' if state == "`st'"
drop prvote14`st'

di " ************ `st' : Balance Table by State ************ "
if("`st'" != "NC"){
	reg treat `covariates' `vhstrata' [aweight=ipw_h_vvhvid] if hhsize==1 & state == "`st'", vce(cluster hhid)
	testparm `covariates' `vhstrata'  
	local fstat = round(r(F), 0.01)
	local p = round(r(p), 0.01)
}
else {
	reg treat `covariates' `vhstrata' [aweight=ipw_h_vvhvid] if hhsize==1 & state == "`st'" & i_age >= 23, vce(cluster hhid)
	testparm `covariates' `vhstrata'  
	local fstat = round(r(F), 0.01)
	local p = round(r(p), 0.01)
}

if("`st'"=="AK") {
	outreg2 using "Tables/Diagnostic_RandCheckByStateHH1.xls", ctitle("`st'") replace label addstat("F-statistic", `fstat', "p-value", `p') dec(3)
}
else {
	outreg2 using "Tables/Diagnostic_RandCheckByStateHH1.xls", ctitle("`st'") append label addstat("F-statistic", `fstat', "p-value", `p') dec(3)
}
}


/* Results (saved to "Tables/Diagnostic_RandCheckByStateHH1.xls") show that
	there are still concerns with North Carolina (F-stat p value is zero). Let's
	figure out what went wrong in North Carolina. Age perdictors are all significant
	in North Carolina as are past voting (likely correlated). Asseess the relationship
	between treatment assignment and age nonparametrically in North Carolina
*/
twoway (lpoly treat i_age) if hhsize==1 & state=="NC"
twoway (lpoly treat i_age) if hhsize==1, by(state)

/* The plot shows that there's an usually high proportino of subjects under age 23
	in North Carolina who are assigned to control; likely a randomization failure
	where they could not have been assigned to treatment. Since we do not know
	what happened here, we remain conservative by excluding subjects under 23 in NC
	from the analysis sample
*/

/* Update analysis sample definition & conduct new randomization check

  Now we re-run the randomization check on the pooled data, for single person households
  who have valid vote history data and a valid household ID, and excluding any subject
  who is under 23 in North Carolina
 
  Additionally for categorical covariates excluded from the state-by-state randomization
  checks we set them all to zero (for subjects in that state only) in the pooled randomization check
*/

* First, for covariates that are dropped in the state-by-state randomization checks
* recode these all as zero for states where they are dropped 

gen r_age_miss = age_miss
gen r_voted09 = voted09
gen r_voted11 = voted11
gen r_voted13 = voted13

replace r_age_miss = 0 if state=="AR" | state=="AZ" | state=="CO" | state=="FL" | state=="GA" | ///
	state=="IA" | state=="KS" | state=="KY" | state=="LA" | state=="ME" | state=="MI" | state=="NC" | state=="SD" | state=="TX"

replace r_voted09 = 0 if state=="AK" | state=="AR" | state=="AZ" | state=="CO" | state=="FL" | ///
	state=="GA" | state=="IA" | state=="KS" | state=="KY" | state=="LA" | state=="ME" | state=="MI" | state=="NC" | state=="NH" | ///
	state=="SD" | state=="TX"
	
replace r_voted11 = 0 if state=="AK" | state=="AR" | state=="AZ" | state=="CO" | ///
	state=="FL" | state=="GA" | state=="IA" | state=="KS" | state=="KY" | state=="LA" | ///
	state=="ME" | state=="MI" | state=="NC" | state=="NH" | state=="SD"

replace r_voted13 = 0 if state == "LA" | state == "NH" | state == "SD" | state == "TX"

* Randomization Checks, Save Output

* Col 1: Analysis Sample

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 covs i_age age2 r_age_miss voted06 voted08 r_voted09 voted10 r_voted11 voted12 r_voted13 d_race_b d_race_h d_race_o d_married d_female 
local vhstrata vhblw vhabv

reg treat `covs' `vhstrata' `states' [aweight=ipw_h_exnclt23_vvhvid] if (state != "NC" & hhsize==1) | (state=="NC" & hhsize == 1 & i_age >=23), vce(cluster hhid)
testparm `covs' `vhstrata' `states' 
local fstat = round(r(F), 0.01)
local p = round(r(p), 0.01)
outreg2 using "Tables/TableA1_RandomizationChecks.xls", ctitle("Exclude if Under 23 in NC, Weighted") label addstat("F-statistic", `fstat', "p-value", `p') dec(3) replace

* Col 2: Analysis Sample, Include Predicted Value of Voting in 2014 as Covariate

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 covs i_age age2 r_age_miss voted06 voted08 r_voted09 voted10 r_voted11 voted12 r_voted13 d_race_b d_race_h d_race_o d_married d_female prvote14
local vhstrata vhblw vhabv

reg treat `covs' `vhstrata' `states' [aweight=ipw_h_exnclt23_vvhvid] if (state != "NC" & hhsize==1) | (state=="NC" & hhsize == 1 & i_age >=23), vce(cluster hhid)
testparm `covs' `vhstrata' `states' 
local fstat = round(r(F), 0.01)
local p = round(r(p), 0.01)
outreg2 using "Tables/TableA1_RandomizationChecks.xls", ctitle("Exclude if Under 23 in NC, with Pred. Vote as Covariate, Weighted") label addstat("F-statistic", `fstat', "p-value", `p') dec(3) append

* Col 3: Same as Col 2 but Exclude NC

reg treat `covs' `vhstrata' `states' [aweight=ipw_h_exnclt23_vvhvid] if (state != "NC" & hhsize==1), vce(cluster hhid)
testparm `covs' `vhstrata' `states' 
local fstat = round(r(F), 0.01)
local p = round(r(p), 0.01)
outreg2 using "Tables/TableA1_RandomizationChecks.xls", ctitle("Exclude North Carolina, with Pred. Vote as Covariate, Weighted") label addstat("F-statistic", `fstat', "p-value", `p') dec(3) append


/*========================
 TABLE A2
 Summary Statistics
========================*/
* Define variables
local states d_st_AK 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_w d_race_b d_race_h d_race_o d_unmarried d_married d_notfem d_female
local vhstrata vhavg vhblw vhabv

* Pooled
outsum `states' `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if (state != "NC" & hhsize==1) | (state=="NC" & hhsize == 1 & i_age >=23) using "Tables/TableA2_SumStats.xls", ctitle("Full Sample") replace  bracket  addnote("Cells present weighted means and weighted standard deviations.")
foreach z in 0 1 {
	if (`z' == 0){
		local clabel = "Control"
		outsum `states' `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & ((state != "NC" & hhsize==1) | (state=="NC" & hhsize == 1 & i_age >=23)) using "Tables/TableA2_SumStats.xls", append ctitle("`clabel'") bracket 
	}
	else {
		local clabel = "Treatment"
		outsum `states' `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & ((state != "NC" & hhsize==1) | (state=="NC" & hhsize == 1 & i_age >=23)) using "Tables/TableA2_SumStats.xls", append ctitle("`clabel'") bracket
	}
}

/*========================
 TABLES A3 AND A4
 Summary Statistics by State
========================*/
* Define variables
local covariates i_age age2 age_miss voted06 voted08 voted09 voted10 voted11 voted12 voted13 d_race_w d_race_b d_race_h d_race_o d_unmarried d_married d_notfem d_female
local vhstrata vhavg vhblw vhabv

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

	if("`st'" == "AK") {
		outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if state == "`st'" & hhsize==1 using "Tables/TablesA3A4_SumStatsByState.xls", ctitle("`st' - Full Sample") replace  bracket addnote("Cells present weighted means and weighted standard deviations.")
		foreach z in 0 1 {
		if (`z' == 0){
			local clabel = "Control"
			outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & state == "`st'" & hhsize==1 using "Tables/TablesA3A4_SumStatsByState.xls", append ctitle("`st' - `clabel'") bracket 
		}
		else {
			local clabel = "Treatment"
			outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & state == "`st'" & hhsize==1 using "Tables/TablesA3A4_SumStatsByState.xls", append ctitle("`st' - `clabel'") bracket
		}
		}
	}
	else if ("`st'" != "NC") {
		outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if state == "`st'" & hhsize==1 using "Tables/TablesA3A4_SumStatsByState.xls", ctitle("`st' - Full Sample") append  bracket
		foreach z in 0 1 {
		if (`z' == 0){
			local clabel = "Control"
			outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & state == "`st'" & hhsize==1 using "Tables/TablesA3A4_SumStatsByState.xls", append ctitle("`st' - `clabel'") bracket 
		}
		else {
			local clabel = "Treatment"
			outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & state == "`st'" & hhsize==1 using "Tables/TablesA3A4_SumStatsByState.xls", append ctitle("`st' - `clabel'") bracket
		}
		}
	}
	else {
		outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if state=="`st'" & hhsize == 1 & i_age >=23 using "Tables/TablesA3A4_SumStatsByState.xls", ctitle("`st' - Full Sample") append  bracket
		foreach z in 0 1 {
		if (`z' == 0){
			local clabel = "Control"
			outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & state=="`st'" & hhsize == 1 & i_age >=23 using "Tables/TablesA3A4_SumStatsByState.xls", append ctitle("`st' - `clabel'") bracket 
		}
		else {
			local clabel = "Treatment"
			outsum `covariates' `vhstrata' [aweight=ipw_h_exnclt23_vvhvid] if treat == `z' & state=="`st'" & hhsize == 1 & i_age >=23 using "Tables/TablesA3A4_SumStatsByState.xls", append ctitle("`st' - `clabel'") bracket
		}
		}
	}	
}


/*========================
 TABLE A5
 Unweighted ITT 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 != .)

/* Analysis 1: For NC Exclude if Age is under 23 */

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

* model 1

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* 
qui sum voted14 if treat == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/TableA5_UnweightedITT.xls", replace ctitle("Any Report Card") label keep(treat `vhstrata' `states' `covariates') dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", No, "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)
	
* model 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_*		// note: doesn't work with clustered errors
qui sum voted14 if treat == 0 & e(sample)
local c_turnout = r(mean)
qui sum voted14 if treat == 0 & vhblw == 1 & e(sample)
local c_turnout_below = r(mean)
qui sum voted14 if treat == 0 & vhblw == 0 & vhabv == 0 & e(sample)
local c_turnout_median = r(mean)
qui sum voted14 if treat == 0 & vhabv == 1 & e(sample)
local c_turnout_above = r(mean)
outreg2 using "Tables/TableA5_UnweightedITT.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?", No, "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 A6
 ITT by Vote History Stratum
  (partition, then estimate)
========================*/
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 != .)

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"
		
levelsof vh_stratum, local(vhgrp)

foreach vh in `vhgrp' {

preserve

keep if vh_stratum == "`vh'"

* 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 

* 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'
		}
	}
}


* 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 

* model 1

reg voted14 treat `states' `covariates' Z1_* [aweight=ipw_sh_exnclt23]
qui sum voted14 [aweight=ipw_sh_exnclt23] if treat == 0 & e(sample)
local c_turnout = r(mean)

if("`vh'" == "above") {
outreg2 using "Tables/TableA6_PartitionedAnalysisByStratum.xls", replace ctitle("`vh'") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes,  "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes)
}
else {
outreg2 using "Tables/TableA6_PartitionedAnalysisByStratum.xls", append ctitle("`vh'") label keep(treat) dec(3) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes,  "With State Fixed Effects?", Yes, "With State-Covariate Interactions?", Yes)
}

restore

}

/*========================
 TABLE A7
 Sensitivity of ITT Estimates
  to Dropping Each 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 != .)

* 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'
			}
		}
	}
}

* POOLED ITT, RE-ESTIMATE DROPPING EACH STATE


* 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/TableA7_PanelA_IterativelyDropState.dta"
tempname tempfile
postfile `tempfile' str2 state b_treat se_treat N df_res c_turnout t_turnout t_turnout_se using "Tables/TableA7_PanelA_IterativelyDropState.dta"

levelsof state, local(sts)

foreach st in `sts' {

reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [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/TableA7_PanelA_IterativelyDropState.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/TableA7_PanelA_IterativelyDropState.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'

* TREATMENT-BY-STRATUM NITERACTIONS, RE-ESTIMATE DROPPING EACH STATE

gen treat_below = treat * vhblw
gen treat_above = treat * vhabv


* Estimate Effect of Report Card by Vote History Stratum, By State
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
label var treat_below "Report Card * Below Median"
label var treat_above "Report Card * Above Median"

capture erase "Tables/TableA7_PanelB_IterativelyDropState_ByVoteHistory.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/TableA7_PanelB_IterativelyDropState_ByVoteHistory.dta"

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

reg voted14 treat treat_below treat_above `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [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/TableA7_PanelB_IterativelyDropState_ByVoteHistory.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/TableA7_PanelB_IterativelyDropState_ByVoteHistory.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 A8
 Treatment by High Salience
  Interaction Model
========================*/
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

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

gen treat_hi = treat * hi_salience
reg voted14 treat treat_hi hi_salience `states' `vhstrata' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_h_exnclt23_vvhvid]
qui sum voted14 [aweight=ipw_h_exnclt23_vvhvid] if treat == 0 & hi_salience == 0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Tables/TableA8_HetEffectsByHighSalience.xls", replace ctitle("Outcome: Voting in 2014") label keep(treat hi_salience treat_hi `vhstrata' `covariates') dec(3)  ///
	addstat(Control Group in Lower-Salience Contexts Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes, "With Vote History Stratum Fixed Effects?", Yes, "With Stratum-Covariate Interactions?", Yes)


/*========================
 TABLE A9
 Predicting Baseline Probability
  of Voting in 2014 in Control
========================*/
use PublicReplicationData.dta, clear
set matsize 11000

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

* define local macros for variable sets to construct 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 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 treat == 0
		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 treat == 0
		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 treat == 0
		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 treat == 0
			if ( r(sd) == . | r(sd) == 0 ){
				drop Z4_`st'_`v'_`vh'
			}
		}
	}
}

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 vhstrata vhblw vhabv
* redefine covariates local macro to include age_miss
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

logit voted14 `covariates' `states' `vhstrata' if treat == 0
local ll = round(e(ll), 3)
local r2p = e(r2_p)
outreg2 using "Tables/TableA9_PredVote2014.xls", replace ctitle("Logit with No Interactions") label keep(`vhstrata' `covariates' `states') dec(3) ///
	addstat("Pseudo R-squared", `r2p', "Log Likelihood", `ll') ///
	addtext("With State-Covariate Interactions?", No, ///
			"With State-Stratum Interactions?", No, ///
			"With Stratum-Covariate Interactions?", No, ///
			"With State-Stratum-Covariate Interactions?", No)

logit voted14 `covariates' `states' `vhstrata' Z1_* Z2_* Z3_* if treat == 0
local ll = round(e(ll), 3)
local r2p = e(r2_p)
outreg2 using "Tables/TableA9_PredVote2014.xls", replace ctitle("Logit with Two-Way Interactions") label keep(`vhstrata' `covariates' `states') dec(3) ///
	addstat("Pseudo R-squared", `r2p', "Log Likelihood", `ll') ///
	addtext("With State-Covariate Interactions?", Yes, ///
			"With State-Stratum Interactions?", Yes, ///
			"With Stratum-Covariate Interactions?", Yes, ///
			"With State-Stratum-Covariate Interactions?", No)

* predict probability of voting in 2014 across all arms using control group estimation results from the full model
predict prvote14, pr
label var prvote14 "Predicted Probability of Voting in 2014"

* re-estimate predicted vote 2014 using OLS
reg voted14 `covariates' `states' `vhstrata' Z1_* Z2_* Z3_* if treat == 0
outreg2 using  "Tables/TableA9_PredVote2014.xls", append ctitle("OLS with Two-Way Interactions") label keep(`vhstrata' `covariates' `states') dec(3) ///
	addtext("With State-Covariate Interactions?", Yes, ///
			"With State-Stratum Interactions?", Yes, ///
			"With Stratum-Covariate Interactions?", Yes, ///
			"With State-Stratum-Covariate Interactions?", No)
predict prvote14ols, xb
label var prvote14ols "Linear Prediction: Voting in 2014"

* pairwise correlation = .9968; use prvote14
pwcorr prvote14ols prvote14

/*========================
 FIGURE A2
 Histogram of prvote14
========================*/

hist prvote14, freq width(0.01) yla(, angle(0) format(%20.0f)) 
graph export "Figures/FigureA2_Histogram_PredVote14.tif", replace


/*========================
 FIGURE A3
 ITT Effect of Report Card
  by Predicted Baseline 
  Prob of Voting in 2014
========================*/

* Construct treat X prvote14 interaction term
gen treat_prvote14 = treat * prvote14

xtile bin_prvote14 = prvote14, nq(20)

twoway scatter bin_prvote14 prvote14
tab bin_prvote14, m
sum prvote14

forv i = 1(1)20 {
	preserve
	duplicates drop hhid, force
	contract state treat if flag_drop_hhid == 0 & vhavg != . & bin_prvote14 == `i', freq(freq)
	list, t noobs clean
	rename freq freq_bin`i'
	save  "n_byprvote14_20qbin`i'.dta", replace
	restore
}


preserve
forv i = 1(1)20 {
	if (`i' == 1){
		use "n_byprvote14_20qbin`i'.dta", clear
	}
	else {
		merge 1:1 state treat using "n_byprvote14_20qbin`i'.dta"
		drop _merge
	}
	
	if (`i' == 20){
		save "n_byprvote14_20qbin.dta", replace
	}
}
restore

forv i = 1(1)20 {
	erase "n_byprvote14_20qbin`i'.dta"
}

* calculate ipws for heterogeneous effects analysis by predicted prob vote 14
preserve
use "n_byprvote14_20qbin.dta", clear
	forv i = 1(1)20 {
		replace freq_bin`i' = 0 if freq_bin`i' == .
		bys state: egen sum_freq_bin`i' = sum(freq_bin`i')
		gen ipw_bin`i' = freq_bin`i' / sum_freq_bin`i'
				
	}
drop freq_* sum_*
order state treat ipw_*
save "ipw_byprvote14_20qbin.dta", replace
restore

* merge IPWs into analysis file
merge m:1 state treat using "ipw_byprvote14_20qbin.dta"
drop _merge


* re-estimate model 1 by bin
capture erase "Tables/FigureA3_Estimates.dta"
tempname tempfile
postfile `tempfile' caliper_lower caliper_mid caliper_upper b_treat se_treat N df_res c_turnout using "Tables/FigureA3_Estimates.dta"

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 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 vhstrata vhblw vhabv

forv i = 1(1)20 {
	preserve
	di `i'  // show which iteration we're on
	reg voted14 treat `vhstrata' `states' `covariates' Z1_* Z2_* Z3_* Z4_* [aweight=ipw_bin`i'] if bin_prvote14 == `i'
	local numobs = e(N)
	local dfres = e(df_r)
	qui sum voted14 [aweight=ipw_bin`i'] if treat == 0 & e(sample)
	local c_turnout = r(mean)
	sum prvote14 if bin_prvote14 == `i'
	local cplb = r(min)
	local cpub = r(max)
	local cpmid = r(mean)
	post `tempfile' (`cplb') (`cpmid') (`cpub') (_b[treat]) (_se[treat]) (`numobs') (`dfres') (`c_turnout')
	restore
}

postclose `tempfile'

preserve
use "Tables/FigureA3_Estimates.dta", clear
serrbar b_treat se_treat caliper_mid, scale(1.96) msize(.6) yline(0.007) ///
	ytitle("Estimated Effect of Report Card Treatment") ///
	xtitle("Predicted Probability of Voting in 2014")
graph export "Figures/FigureA3_ITTByPredVote14_20Q.pdf", replace

restore

* Clean up temp files
erase ipw_byprvote14_20qbin.dta
erase n_byprvote14_20qbin.dta

log close
