*********************************************
*RecodeStackedData.do
**
*Recode and subset the cases used for individual
*level models.
**
*Alan Gerber, Gregory Huber, Seth Hill, "Identifying the Effects of Elections Held All-Mail on
*Turnout: Staggered Reform in the Evergreen State."
*********************************************

*Argument passed from other files is the debug value.
args debug

*Data created in CreateWAVoterForAnalysis2.do.
*For analysis of 2008 turnout.
use WaVoterForAnalysis0608.dta, clear
gen recordyear = 2006
label variable recordyear "Voter file year from which record came."
*For analysis of 2010 turnout.
append using WaVoterForAnalysis0810.dta, force
replace recordyear = 2008 if recordyear == .

*************************************  
*Limit to appropriate counties.
*Though Island, Kittitas, and Klickitat Counties switched after 2006 election, analysis
*suggests that the number of votes in the voter file by type in Kittitas does not match
*to the number of votes by type in the county-level data. See CheckVoterFileVoteCounts.do.
*Instead, consider only Island and Klickitat relative to King and Pierce Counties, which 
*had polling places in 2008.
*************************************  
gen insample = 0
*Island and Klickitat switched after 2006; compare to King and Pierce Counties that
*continued to have polling places in 2010.
replace insample = 1 if (countycode == "KI" | countycode == "PI" | countycode == "IS" | countycode == "KT") & recordyear == 2006
*King County switched after the 2008 election; compare to Pierce County that
*continued to have polling places in 2010.
replace insample = 1 if (countycode == "KI" | countycode == "PI") & recordyear == 2008

keep if insample
drop insample

*Create Pierce, Island, and Klickitat records from the 2006 file for whom 2010 turnout
*will be modeled to estimate the decay of the treatment effect.
gen expander = ((countycode == "PI" | countycode == "IS" | countycode == "KT") & recordyear == 2006) + 1
count
expand expander
count
drop expander
*Tag the second record for each expanded case as a decay case.
gen decaycase = 0
label variable decaycase "Decay case from 2006 file for 2010 election"
bysort countycode recordyear statevoterid: replace decaycase = 1 if statevoterid[_n] == statevoterid[_n-1]
tab countycode decaycase 
*Set the recordyear to 2008 for the decay cases.
replace recordyear = 2008 if decaycase == 1
*Filter out decay cases where registrant moved across counties 2006 to 2010, or where
*match is bad 2006 to 2010.
drop if bad06to10match == 1
*When samecounty06and10 == "", 06 record did not match to 10 record; assume same county.
drop if samecounty06and10 == 0 & merge_10reg == 3

if (`debug' == 1) {
  count
  local keeppct = .04
  *Keep a random `keeppct' percent of precincts (or so).
  preserve
  contract recordyear countycode precinctcode decaycase
  count
  qui gen rand_p = runiform()
  keep if rand_p < `keeppct'
  drop rand_p
  sort recordyear countycode precinctcode decaycase
  save "SampledPrecincts.dta",replace
  restore
  *Join in set of sampled precincts.
  count
  joinby recordyear countycode precinctcode decaycase using "SampledPrecincts.dta", unmatched(using)
  tab _merge
  drop _merge
  di _n "=============" _n "Using a `keeppct' sample of precincts!" _n "=============" _n 
}

*************************************  
*Model variables.
*************************************  
*Basic model variables variables relative to record year.
gen voted = vote2008g if recordyear == 2006
replace voted = vote2010g if recordyear == 2008
label variable voted "Voted in post-treatment election"
tab voted recordyear, miss col

gen electionyear = recordyear + 2
label variable electionyear "Post-treatment election year"

gen priorvote = vote2006g if recordyear == 2006
replace priorvote = vote2008g if recordyear == 2008
label variable priorvote "Voted in pre-treatment election"
tab priorvote recordyear, miss col

*County fixed effects.
gen countystring = ""
replace countystring = "Island" if countycode == "IS"
replace countystring = "King" if countycode == "KI"
replace countystring = "Klickitat" if countycode == "KT"
replace countystring = "Pierce" if countycode == "PI"
tab countystring, miss gen(countyfe_)
drop countyfe_1
foreach var of varlist countyfe_* {
  local v : variable label `var'
  local newlab = subinstr("`v'","countystring==","",.)
  label variable `var' "County: `newlab'"
}

*Election-county fixed effects.
egen electioncounty = concat( electionyear countystring ), punct("_")
tab electioncounty, miss gen(ecountyfe_)
drop ecountyfe_1
foreach var of varlist ecountyfe_* {
  local v : variable label `var'
  local newlab = subinstr("`v'","electioncounty==","",.)
  label variable `var' "Election-county: `newlab'"
}
egen electioncountyfe = group(electioncounty)
label variable electioncountyfe "County in one election"
*Create a string that lists the elections in the sample.
local electionlist ""
levelsof electioncounty, local(elections)
foreach i of local elections {
  local electionlist "`electionlist' `i'"
}

gen election2010 = (electionyear == 2010)
label variable election2010 "Election: 2010"

gen election2008 = (electionyear == 2008)
label variable election2008 "Election: 2008"

gen priorpollingplace = (absenteetype == "N" | absenteetype == "U")
label variable priorpollingplace "Registered as polling place voter in pre-treatment election"
tab priorpollingplace electioncounty, col miss

gen priorabsentee = priorpollingplace == 0
label variable priorpollingplace "Registered as absentee voter in previous election"
tab priorabsentee electioncounty, col miss

gen priorpoll2006 = priorpollingplace & recordyear == 2006
label variable priorpoll2006 "Registered as polling place voter in 2006; 2008 election"
gen priorpoll2008 = priorpollingplace & recordyear == 2008
label variable priorpoll2008 "Registered as polling place voter in 2008; 2010 election"

gen treatedcounty = 0
replace treatedcounty = 1 if (countycode == "IS" | countycode == "KT") & recordyear == 2006
replace treatedcounty = 1 if (countycode == "KI") & recordyear == 2008
label variable treatedcounty "County switched to all-mail elections"
tab electioncounty treatedcounty, miss

gen treatedXpriorpoll = priorpollingplace*treatedcounty
label variable treatedXpriorpoll "Treated polling place registrant"
tab treatedXpriorpoll electioncounty , col miss
  
*Create a precinct variable by county and recordyear; include the decaycase variable
*so that decay case precinct fixed effects are different from non-decay case precincts
*when modeled together.
egen countyprec = concat( electionyear countycode precinctcode decaycase )
*xtreg requires the panel variable to be numeric.
egen precinct = group(countyprec)
label variable precinct "Precinct in one election"
bys electioncounty: summ precinct

*Covariates.
gen maleXelection2008 = (gender == "M" & electionyear == 2008)
gen maleXelection2010 = (gender == "M" & electionyear == 2010)
label variable maleXelection2008 "Male; 2008 election"
label variable maleXelection2010 "Male; 2010 election"
gen age = electionyear - year(birthdate)
gen age_sq = (age^2)/100
qui summ age
qui replace age = age - `r(mean)'
qui summ age_sq
qui replace age_sq = age_sq - `r(mean)'
label variable age "Age (mean-deviated)"
label variable age_sq "Age Squared (mean-deviated)"

gen ageXelection2008 = age*(electionyear == 2008)
gen ageXelection2010 = age*(electionyear == 2010)
gen age_sqXelection2008 = age_sq*(electionyear == 2008)
gen age_sqXelection2010 = age_sq*(electionyear == 2010)
label variable ageXelection2008 "Age (mean-deviated); 2008 election"
label variable age_sqXelection2008 "Age Squared (mean-deviated); 2008 election"
label variable ageXelection2010 "Age (mean-deviated); 2010 election"
label variable age_sqXelection2010 "Age Squared (mean-deviated); 2010 election"

gen priorvoteXelection2008 = priorvote*(electionyear == 2008)
gen priorvoteXelection2010 = priorvote*(electionyear == 2010)
label variable priorvoteXelection2008 "Voted in pre-treatment election; 2008 election"
label variable priorvoteXelection2010 "Voted in pre-treatment election; 2010 election"

*Age categories.
label define agecat 1 "<=25" 2 "26-35" 3 "36-45" 4 "46-55" 5 "56-65" 6 "66-75" 7 "76-89"
gen ageactual = electionyear - year(birthdate)
recode ageactual (18/25 = 1) (26/35 = 2) (36/45 = 3) (46/55 = 4) (56/65 = 5) (66/75 = 6) (76/89 = 7) (else = .), gen(agecat)
label values agecat agecat
tab ageactual agecat, miss

*Vote history categories.
egen votehiststring = concat( vote2007g vote2006g vote2008g )
gen votehiststring2 = subinstr(votehiststring,"0","N",.)
replace votehiststring2 = subinstr(votehiststring2,".","N",.)
replace votehiststring2 = subinstr(votehiststring2,"1","Y",.)
tab votehiststring votehiststring2
drop votehiststring
label define votehiststring 1 "No votes" 2 "Voted 2008 only" 3 "Voted 2006 but not 2007" 4 "Voted 2007"
gen votehiststring = 1 if (votehiststring2 == "NNN")
replace votehiststring = 2 if substr(votehiststring2,3,1) == "Y"
replace votehiststring = 3 if substr(votehiststring2,2,1) == "Y" 
replace votehiststring = 4 if substr(votehiststring2,1,1) == "Y"
label values votehiststring votehiststring
tab votehiststring votehiststring2, miss
tab voted votehiststring if electionyear == 2010, col
drop votehiststring2

*Decay cases variables.
*New prior polling place indicator.
replace priorpoll2008 = 0 if decaycase == 1
replace priorpoll2006 = 0 if decaycase == 1
gen decaypriorpoll2006 = priorpollingplace & decaycase
label variable decaypriorpoll2006 "Registered as polling place voter in 2006; 2010 election"

*New prior vote indicator.
replace priorvoteXelection2008 = 0 if decaycase == 1
replace priorvoteXelection2010 = 0 if decaycase == 1
gen priorvoteXdecay = priorvote & decaycase
label variable priorvoteXdecay "Voted in pre-treatment election; decay 2010 election"

*The 2008 recordyear, decay cases for Island and Klickitat are treated.
replace treatedcounty = 1 if (countycode == "IS" | countycode == "KT") & decaycase == 1
tab electioncounty treatedcounty, miss

*Treated decay cases get a one for being treated the same as the regular treated cases.
replace treatedXpriorpoll = priorpollingplace*treatedcounty if decaycase == 1
tab electioncounty treatedXpriorpoll, row miss

*Treated decay cases also get an offset for the decay (or increase) in the 
*effect in the second election after the treatment.
gen decaytreatment = treatedcounty*decaycase*priorpollingplace
label variable decaytreatment "Treated polling place registrant 2006; 2010 election"
tab decaytreatment priorpollingplace if decaycase
tab electioncounty decaytreatment

if (`debug' == 1) {
  save "DebugDataFile.dta",replace
}
else {
  *If not debugging, create a data file of turnout of polling place voters in the
  *previous election by precinct.
  preserve
  keep if priorpollingplace == 1
  keep electionyear countycode precinctcode decaycase priorvote
  collapse (mean) precinctpriorvotepollplacevoters=priorvote, by(electionyear countycode precinctcode decaycase) fast
  label variable precinctpriorvotepollplacevoters "Turnout rate of polling place voters in precinct in pre-treatment election"
  sort electionyear countycode precinctcode decaycase
  save "PriorTurnoutByPrecinct.dta", replace
  restore
}
codebook precinct
