set matsize 10000
estimates drop _all 
set more off 


**********************************************************************
* Paths
**********************************************************************

global mainPath "C:\Users\blaca\Dropbox\livro minsaude\\\"

global data "${mainPath}data\\"
global results "${mainPath}results\\"




**********************************************************************
* Main treatment variable
**********************************************************************

global Z "POLICY" /* Post x Treatment*/


**********************************************************************
* OUTCOMES 
**********************************************************************
#delimit ;

global outcomes_supply "physician_cpta physician_public_cpta physician_private_cpta physician_BHU_cpta 
						physician_nonBHU_cpta physician_family_cpta clinician_cpta gynecologist_cpta  pediatrician_cpta physician_others_cpta";

global outcomes_utilization "con_medica_basica_cpta  con_prenatal_cpta con_pre_medico_cpta con_pre_enfer_cpta  
							con_medica_basica_g1_cpta con_medica_basica_g2_cpta con_medica_basica_g3_cpta con_medica_basica_g4_cpta 
							con_medica_basica_g5_cpta con_medica_basica_g6_cpta";

global outcomes_infant_health "prematuro LBW IMR"; 
#delimit cr 

**********************************************************************
* Control variables 
**********************************************************************

#delimit ;
global basic "GDP_cpta lnPopulation2010 Iliteracy_rate2010    indigenous_share2010 gini2010 unemployment_rate2010 rural_population_share2010 
				municipality_area altitude_100 distance_to_capital temperature rainfall amazonia_legal semiarido ";
#delimit cr

global Me "medico_preMPP "
  
global SE "bolsafamilia spending_education spending_sanitation_health"

global MC "low_education solteira idademae" /* Maternal characteristics - only for the infant health analysis*/


**********************************************************************
// Physician results 
**********************************************************************

use "${data}physician_database.dta", replace 


gen Tempo=date-35

local Max =19 
local Min=19

keep if abs(Tempo)<=19
forval i=1/`Min' {
gen pre`i' = Tre*(Tempo==-`i')
}

forval i=0/`Max' {
gen post`i' = Tre*(Tempo==`i')
}




*****************************
* descriptive statistics 
*****************************

preserve 
keep if Tempo==0

gen log_renda_cpta=GDP_cpta
gen analfabetismo_taxa=Iliteracy_rate2010
ttest log_renda_cpta, by(Treatment)
ttest analfabetismo_taxa, by(Treatment)

ttest gini2010 , by(Treatment)
restore 

*****************************
* Modelo 
*****************************

gen Post=Tempo>0 

reghdfe  physician_BHU_cpta   c.Post#c.Treatment, a(date codigo ) cluster(codigo)

reghdfe  physician_BHU_cpta c.date#c.(${basic})  c.Post#c.Treatment, a(date codigo ) cluster(codigo)


	qui reghdfe  physician_BHU_cpta  pre2-pre2 pre3-pre`Min' post0-post`Max'     c.(${basic})#c.date   , a(codigo date) cluster(codigo)
	
	gen C_`v' = .
	gen L_`v' =.
	gen H_`v'=.

		foreach i of numlist 2/2 3/`Min' {
		replace C_`v'= _b[pre`i'] if Tempo==-`i'
		replace L_`v' = _b[pre`i']-1.96*_se[pre`i'] if Tempo==-`i'
		replace H_`v' = _b[pre`i']+1.96*_se[pre`i'] if Tempo==-`i'
		}

		forval i=0/`Max' {
		replace C_`v' = _b[post`i'] if Tempo==`i'
		replace L_`v' = _b[post`i']-1.96*_se[post`i'] if Tempo==`i'
		replace H_`v' = _b[post`i']+1.96*_se[post`i'] if Tempo==`i'
		}

		replace C_`v' = . if Tempo==-1
		replace L_`v' = . if Tempo==-1
		replace H_`v' = . if Tempo==-1


collapse (mean) C_* L_* H_*, by(Tempo)

#delimit ;

twoway (scatter C_ H_  L_
		Tempo  ,					

		xline(-1, lcolor(black)) 	
		yline(0, lcolor(black)) 	
		connect(l l l l l l l l l)	
		cmissing(n n n n n n n n n)	
		msymbol(Oh i i i i)		
		mcolor(forest_green  forest_green forest_green )	
		lpattern( solid dash dash )		
		lwidth( mthin  medthick medthick )			
		lcolor(forest_green forest_green forest_green)		
		legend(off)
		xlabel(-20(4)20, labsize(medlarge))  			
		xtitle("Bimestres antes do PMM", size(medlarge))	
		ytitle("", size(medlarge))
		title("", size(vlarge) color(black))			
		graphregion(fcolor(white) color(white) icolor(white))  ) 		
		(pcarrowi 0.12 -5 0.12  -2, lcolor(black) mcolor(black) lwidth(medthick) mlwidth(medthick) text(0.12 -12 "Bimestres antes do PMM", j(left)));
		

#delimit cr;


*** collapse 

collapse (mean) physician_cpta physician_BHU_cpta, by(Treatment Post)


gen counterfactual_y=physician_BHU_cpta

reg physician_BHU_cpta i.Treatment i.Post c.Treatment#c.Post
local a=_b[c.Treatment#c.Post]
replace counterfactual_y=physician_BHU_cpta-(`a') if Treatment==1 & Post==1 

twoway (scatter physician_BHU_cpta Post if Treatment==1, lp(solid) msymbol(o) mcolor(black) connect(l) lw(thin) lcolor(black)) ///
(scatter physician_BHU_cpta Post if Treatment==0, lp(solid) lw(medthick) msymbol(o) mcolor(maroon) connect(l) lcolor(maroon) ) ///
(pcarrowi  0.22 1.1 0.31 1.1, mcolor(black) lcolor(black) ) ///
(pcarrowi  0.31 1.1 0.22 1.1 , mcolor(black) lcolor(black) ) ///
(scatter counterfactual_y Post if Treatment==1, lp(dash) lcolor(black) mcolor(black) connect(l) msymbo(o)), graphregion(color(white))  ylabel(0.15 0.20 0.25 0.3) xlabel( -0.3 " " 0 "t=1" 1 "t=2" 1.3 " ", noticks) xtick(0 1) xtitle("") legend(off) ////
text(0.19 0 "Y{sub:1}|D=1") ///
text(0.273 0 "Y{sub:1}|D=0") ///
text(0.31 1 "Y{sub:2}|D=1") ///
text(0.296 1 "Y{sub:2}|D=0") ///
text(0.27 1.15 "{&beta}{sub:diff}") ytitle("Medicos em UBS" "(x 100,000 habitantes)", size(small))



