/**************
* derived from the "blocktables" do-files,
* this is county-level analysis.
* FILE 1: MAIN SPECIFICAITON, PRIMARY SAMPLE
***************/
version 11
capture log close

log using "$output/table2_final.txt", replace text

ssc install pcorr2, replace

use "$data2/poverty_full_county2", clear
local outopts "bracket label bdec(2)"
keep if sample1==1		// All counties except AK, HI, DC, two problem cos. in AR
local samplenum=1		// number sample in outreg files

if `samplenum'==1 {
	local sample_label "Excl. AK HI DC and 2 AR counties"
}

gen inv_pop=1000/totpop

egen group=group(stfips)

**********************************************
*MAIN REGRESSION FILES
**********************************************
local P "pct_inc_lt3k pct_nonwhite " 
        

local D "pct_age_lt5yr pct_age_mt64yr pct_urban pct_rurf pct_inc_ge10k " 
    
local P64 "inv_pop dem_share_p6460 dem_share_p64 dwin_p64 close_p64 dwin_close_p64"
		  

local P60 " dwin_p60 close_p60 dwin_close_p60 "
		  


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' `A' `R' `V' "

corr `P' `P64' `P60' `H' `R' `V' `A' `X' 


*************REMOVE P60 VARIABLES
reg fedc6568_pc i.stfips 
predict fed6568hat, residuals


xi i.stfips
**********TABLE 2
local X1 "pct_inc_lt3k `X'"
local X2 "pct_1kput `X'"
local X3 "pct_2kput `X'" 
local X4 "pct_nonwhite `X'" 
local X5 "`P64' `H' `X'"
local X6 "`X1' pct_nonwhite `X5' "
local X7 "`X2' pct_nonwhite `X5' "
local X8 "`X3' pct_nonwhite `X5' "
local X0 "pct_inc_lt3k pct_1kput pct_2k pct_nonwhite `P64' `H' `X'"


*Regressions with state f.e. and only one hypothesis at a time
local r "replace"
local i 0
	while `i'<=8 {			

		areg fed6568hat `X`i'' , robust a(stfips)
			outreg2 `X`i'' using  "$output/table2_new.xls", `r' `outopts' 	///
							ctitle("Federal CAP Funds PC `y'")				/// EXCEL
							addtext("State FE", "Yes", 						///
								"Sample", `"`sample_label'"')				//"""

		pcorr2 fed6568hat  `X`i'' 

local r "append"						

local i = `i'+1
}

	// NON-SOUTH ONLY
local r "replace"
local i 6
	while `i'<=8 {			

		areg fed6568hat `X`i'' if region!=3, robust a(stfips)
			outreg2 `X`i'' using  "$output/table3_new.xls", `r' `outopts' 	///
							ctitle("Federal CAP Funds PC `y'")				/// EXCEL
							addtext("State FE", "Yes", 						///
								"Sample", `"`sample_label'"')				//"""

		pcorr2 fed6568hat `X`i'' if region!=3

local r "append"						

local i = `i'+1
}


	// SOUTH ONLY
local r "append"
local i 6
	while `i'<=8 {			

		areg fed6568hat `X`i'' if region==3, robust a(stfips)
			outreg2 `X`i'' using  "$output/table3_new.xls", `r' `outopts' 	///
							ctitle("Federal CAP Funds PC `y'")				/// EXCEL
							addtext("State FE", "Yes", 						///
								"Sample", `"`sample_label'"')				//"""

		pcorr2 fed6568hat `X`i'' if region==3

local r "append"						

local i = `i'+1
}

log close
exit
