/************
* create data for county election panel
*************/
local P "pct_inc_lt3k pct_nonwhite " //pct_1kput
local D "pct_age_lt5yr pct_age_mt64yr pct_urban pct_rurf pct_inc_ge10k " //pct_1kput
local P64 "dem_share_p6460 dem_share_p64 dwin_p64 close_p64 dwin_close_p64"
local P60 " dwin_p60 close_p60 dwin_close_p60 "
local H1 "house_dem88 com_maj_lead88 com_maj_dem88  chair_lead88 chair_lead_dem88 house_dem89 com_maj_lead89 com_maj_dem89  chair_lead89 chair_lead_dem89"
local H "house_dem89 com_maj_lead89 com_maj_dem89  chair_lead89 chair_lead_dem89"
local C "dge_pc tax_pc62 pwe_pc62"
local R "riot6468"
local V "viet_death_sh_6070"
local A "scrp_op30 " 
local X "`D' `C' "
local turnout "cong_tout*"  // pres_tout*

use "$data2/poverty_full_county_panel_turnout", clear //
keep if sample1==1
drop if year<50 | year>72

	// Balance the county panel
sort stfips_f cofips_f
gen temp1=cond(cong_tout!=. & cong_dpct!=.,1,0)
by stfips_f cofips_f: egen ycount=sum(temp1)
sum ycount
assert `r(max)'==12
keep if ycount==12
drop ycount temp1

	// Save to output summary statistics
save "$temp/balpal_turnout_tabled2", replace



local Xvarlist "`P' `D' `C'  `A' `R' `V' "

local interactions ""
foreach X in `Xvarlist' {
	local interactions "`interactions' i.year|`X'" //c.`X'#i.year
	display "`interactions'"
}

gen fips=stfips_f*1000+cofips_f
local outopts "label bracket bdec(3)"

sort fips
tsset fips year

char year[omit] 60

local rule1 ""
local rule2 "if region==3"
local rule3 "if region!=3"

forval i=1/3 {
	xi: xtreg cong_tout `interactions'  i.year `rule`i'', fe  cluster(fips)
	outreg2 using "$output/turnout_panel_`i'.xls", `outopts' replace

	xi: xtreg cong_tout  `interactions' i.year*i.stfips `rule`i'', fe  cluster(fips)
	outreg2 using   "$output/turnout_panel_`i'.xls", `outopts' append

	xi: xtreg cong_dpct`interactions'   i.year  `rule`i'', fe  cluster(fips)
	outreg2 using "$output/turnout_panel_`i'.xls", `outopts' append
 
	xi: xtreg cong_dpct `interactions'  i.year*i.stfips `rule`i'', fe  cluster(fips)
	outreg2 using  "$output/turnout_panel_`i'.xls", `outopts' append
}

