clear all

*** This is part of the replication files for 
* Cecchetti, Feroli, Kashyap, Mann and Schoenholtz (September 2020)
* 
*
* Julien Weber (julien.weber@chicagobooth.edu) 2020-09-14
***


*clean data
use "results/cleandata/data_with_conditions_and_nmp.dta",clear

*sample to start in April 1995
drop in 1/78


*regressions for table 1:
foreach var in fgd fgs qe me m0 nrd pc1_coin pc2_coin {
	gen `var'=.
}

forvalues n=1/2{ //loop over domestic pc1 and pc2

	eststo clear
	foreach country in us fra deu it jpn swe che uk{
	//foreach country in fra {
		display "This is the regression for `country'"
		display "This is the regression for domestic component `n'"

		local varlist = "fgd fgs qe me m0"
		if "`country'"=="che" local varlist = "qe me m0"
		if "`country'"=="jpn" local varlist = "fgs qe me m0"
		if "`country'"=="swe" local varlist = "fgd qe me m0" 
		
		local varlist_nrd = "nrd"
		if "`country'"=="us"|"`country'"=="uk" local varlist_nrd = ""
		
		local indicators = ""
		foreach var in `varlist' {
			replace `var'=`var'_`country' // nicer regression table
			local indicators = "`indicators'" + " L(0/3).`var'"
		}
		foreach var in `varlist_nrd' {
			replace `var'=`var'_`country' // nicer regression table
			local indicators = "`indicators'" + " `var'"
		}
				
		*replace the domestic index with our versions
		replace pc`n'_coin = pc`n'_dfci_`country' //julien
		
		capture drop pc`n'_coinp

		*regression with newey west standard errors
		eststo `country': newey pc`n'_coin L1.pc`n'_coin pc1_gfci `indicators', lag(6)
	
		*need to calculate r2 separately:
		predict pc`n'_coinp if e(sample) 
		corr pc`n'_coin pc`n'_coinp if e(sample) 
		estadd scalar r2 = `r(rho)'^2

		*linear combination of coefficients
		foreach var in `varlist'{
			lincom L0.`var' + L1.`var' + L2.`var' + L3.`var'
			estadd scalar `var'_b = `r(estimate)'
			estadd scalar `var'_t = `r(t)'
		}
		foreach var in `varlist_nrd'{
			lincom `var'
			estadd scalar `var'_b = `r(estimate)'
			estadd scalar `var'_t = `r(t)'
		}	
	}

	esttab using "results/step2/2_regression_table1_pc`n'.csv", stats(fgd_b fgd_t fgs_b fgs_t qe_b qe_t me_b me_t m0_b m0_t nrd_b nrd_t N r2 , fmt(%9.2f) layout(@ `"(@)"' @ `"(@)"' @ `"(@)"' @ `"(@)"' @ `"(@)"' @ `"(@)"' @ @)) drop(_cons pc1_gfci L* fgd fgs qe me m0 nrd) nostar mtitles replace

}
