frame create temp
frame change temp

//Load data for the treated state coefficients
if("`c(os)'"=="Windows"){
	import excel "Tables\RandTreatSim.xlsx", sheet("RandTreatRegTreatedStates") firstrow
	local plotSaveDir = "Plots\RandTreatSim\"
	local tableSaveDir = "Tables\RandTreatSim\"
}
else{
	import excel "Tables/RandTreatSim.xlsx", sheet("RandTreatRegTreatedStates") firstrow
	local plotSaveDir = "Plots/RandTreatSim/"
	local tableSaveDir = "Tables/RandTreatSim/"
}

//Label variables
label var Spec1Coefficient "Spec. 1 Coef."
label var Spec2Coefficient "Spec. 2 Coef."
label var Spec3Coefficient "Spec. 3 Coef."
label var Spec4Coefficient "Spec. 4 Coef."


//Export histograms of the coefficient
hist Spec1Coefficient, name(Spec1TreatedStates) freq graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegTreatedStatesSpec1.png", width(`widthPix') name(Spec1TreatedStates) replace

hist Spec2Coefficient, name(Spec2TreatedStates) freq graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegTreatedStatesSpec2.png", width(`widthPix') name(Spec2TreatedStates) replace

hist Spec3Coefficient, name(Spec3TreatedStates) freq graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegTreatedStatesSpec3.png", width(`widthPix') name(Spec3TreatedStates) replace

hist Spec4Coefficient, name(Spec4TreatedStates) freq graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegTreatedStatesSpec4.png", width(`widthPix') name(Spec4TreatedStates) replace

graph combine Spec1TreatedStates Spec2TreatedStates Spec3TreatedStates Spec4TreatedStates, ycomm xcomm title("Random Treatment Analysis Within Treated States" "Histogram of Coefficients by Spec.") graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegTreatedStatesCombined.png", width(`widthPix') replace



//Export summary table
local dispStats = "mean sd sem count"
local dispStatsFmt = "mean(fmt(3) label(Mean)) sd(fmt(3) label(Std. Dev.)) semean(fmt(3) label(Std. Error)) count(fmt(0) label(N))"

local dispVars = "Spec1Coefficient Spec2Coefficient Spec3Coefficient Spec4Coefficient"
tabstat `dispVars', stat(`dispStats') save
mat stats = r(StatTotal)

//Create table for export 
gen y = 1
gen x = 1
//Add Spec1 stats
reg y x
eststo Spec1 
estadd scalar mu = stats["Mean", "Spec1Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec1Coefficient"]
estadd scalar iterCount = stats["N", "Spec1Coefficient"]

estadd scalar tStat = stats["Mean", "Spec1Coefficient"]/stats["se(mean)", "Spec1Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec1Coefficient"]-1, abs(stats["Mean", "Spec1Coefficient"]/stats["se(mean)", "Spec1Coefficient"]))*2
//Add Spec2 stats
reg y x
eststo Spec2 
estadd scalar mu = stats["Mean", "Spec2Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec2Coefficient"]
estadd scalar iterCount = stats["N", "Spec2Coefficient"]

estadd scalar tStat = stats["Mean", "Spec2Coefficient"]/stats["se(mean)", "Spec2Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec2Coefficient"]-1, abs(stats["Mean", "Spec2Coefficient"]/stats["se(mean)", "Spec2Coefficient"]))*2
//Add Spec3 stats
reg y x
eststo Spec3  
estadd scalar mu = stats["Mean", "Spec3Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec3Coefficient"]
estadd scalar iterCount = stats["N", "Spec3Coefficient"]

estadd scalar tStat = stats["Mean", "Spec3Coefficient"]/stats["se(mean)", "Spec3Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec3Coefficient"]-1, abs(stats["Mean", "Spec3Coefficient"]/stats["se(mean)", "Spec3Coefficient"]))*2
//Add Spec4 stats
reg y x
eststo Spec4 
estadd scalar mu = stats["Mean", "Spec4Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec4Coefficient"]
estadd scalar iterCount = stats["N", "Spec4Coefficient"]

estadd scalar tStat = stats["Mean", "Spec4Coefficient"]/stats["se(mean)", "Spec4Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec4Coefficient"]-1, abs(stats["Mean", "Spec4Coefficient"]/stats["se(mean)", "Spec4Coefficient"]))*2


//Output table
esttab Spec1 Spec2 Spec3 Spec4 using "`tableSaveDir'RandTreatRegTreatedStates.tex", title("Random Treatment Coefficient Summary Statistics - Treated States \label{tab: RandTreatRegTreatedStates}") stats(mu stdErr tStat pValue iterCount, labels("Mean" "Standard Error" "t-Statistic" "p-value" "N")fmt(%8.4f %8.4f %8.2f %8.3f %8.0f)) drop(*) mtitles("\makecell{No \\ DSCs}" "\makecell{Documented \\ DSCs}" "\makecell{Suspected \\ DSCs}" "\makecell{Potential \\ DSCs}") not nostar replace // addnote("`tableNote1'" "`tableNote2'" "`tableNote3'" "`tableNote4'") 




frame change default
frame drop temp
frame create temp
frame change temp

//Load data for all state coefficients
if("`c(os)'"=="Windows"){
	import excel "Tables\RandTreatSim.xlsx", sheet("RandTreatRegAllStates") firstrow
	local plotSaveDir = "Plots\RandTreatSim\"
	local tableSaveDir = "Tables\RandTreatSim\"
}
else{
	import excel "Tables/RandTreatSim.xlsx", sheet("RandTreatRegAllStates") firstrow
	local plotSaveDir = "Plots/RandTreatSim/"
	local tableSaveDir = "Tables/RandTreatSim/"
}

//Label variables
label var Spec1Coefficient "Spec. 1 Coef."
label var Spec2Coefficient "Spec. 2 Coef."
label var Spec3Coefficient "Spec. 3 Coef."
label var Spec4Coefficient "Spec. 4 Coef."

//Export histograms of the coefficient
hist Spec1Coefficient, name(Spec1AllStates) freq graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegAllStatesSpec1.png", width(`widthPix') name(Spec1AllStates) replace

hist Spec2Coefficient, name(Spec2AllStates) freq graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegAllStatesSpec2.png", width(`widthPix') name(Spec2AllStates) replace

hist Spec3Coefficient, name(Spec3AllStates) freq  graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegAllStatesSpec3.png", width(`widthPix') name(Spec3AllStates) replace

hist Spec4Coefficient, name(Spec4AllStates) freq  graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegAllStatesSpec4.png", width(`widthPix') name(Spec4AllStates) replace

graph combine Spec1AllStates Spec2AllStates Spec3AllStates Spec4AllStates, ycomm xcomm title("Random Treatment Analysis Across All States" "Histogram of Coefficients by Spec.") graphregion(color(white)) plotregion(color(white))
graph export "`plotSaveDir'RandTreatRegAllStatesCombined.png", width(`widthPix') replace


local dispVars = "Spec1Coefficient Spec2Coefficient Spec3Coefficient Spec4Coefficient"
tabstat `dispVars', stat(`dispStats') save
mat stats = r(StatTotal)

eststo clear
//Create table for export 
gen y = 1
gen x = 1
//Add Spec1 stats
reg y x
eststo Spec1 
estadd scalar mu = stats["Mean", "Spec1Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec1Coefficient"]
estadd scalar iterCount = stats["N", "Spec1Coefficient"]

estadd scalar tStat = stats["Mean", "Spec1Coefficient"]/stats["se(mean)", "Spec1Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec1Coefficient"]-1, abs(stats["Mean", "Spec1Coefficient"]/stats["se(mean)", "Spec1Coefficient"]))*2
//Add Spec2 stats
reg y x
eststo Spec2 
estadd scalar mu = stats["Mean", "Spec2Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec2Coefficient"]
estadd scalar iterCount = stats["N", "Spec2Coefficient"]

estadd scalar tStat = stats["Mean", "Spec2Coefficient"]/stats["se(mean)", "Spec2Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec2Coefficient"]-1, abs(stats["Mean", "Spec2Coefficient"]/stats["se(mean)", "Spec2Coefficient"]))*2
//Add Spec3 stats
reg y x
eststo Spec3  
estadd scalar mu = stats["Mean", "Spec3Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec3Coefficient"]
estadd scalar iterCount = stats["N", "Spec3Coefficient"]

estadd scalar tStat = stats["Mean", "Spec3Coefficient"]/stats["se(mean)", "Spec3Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec3Coefficient"]-1, abs(stats["Mean", "Spec3Coefficient"]/stats["se(mean)", "Spec3Coefficient"]))*2
//Add Spec4 stats
reg y x
eststo Spec4 
estadd scalar mu = stats["Mean", "Spec4Coefficient"]
estadd scalar stdErr = stats["se(mean)", "Spec4Coefficient"]
estadd scalar iterCount = stats["N", "Spec4Coefficient"]

estadd scalar tStat = stats["Mean", "Spec4Coefficient"]/stats["se(mean)", "Spec4Coefficient"]
estadd scalar pValue = ttail(stats["N", "Spec4Coefficient"]-1, abs(stats["Mean", "Spec4Coefficient"]/stats["se(mean)", "Spec4Coefficient"]))*2

di "`tableSaveDir'"


//Output table
esttab Spec1 Spec2 Spec3 Spec4 using "`tableSaveDir'RandTreatRegAllStates.tex", title("Random Treatment Coefficient Summary Statistics - All States \label{tab: RandTreatRegAllStates}") stats(mu stdErr tStat pValue iterCount, labels("Mean" "Standard Error" "t-Statistic" "p-value" "N")fmt(%8.4f %8.4f %8.2f %8.3f %8.0f)) drop(*) mtitles("\makecell{No \\ DSCs}" "\makecell{Documented \\ DSCs}" "\makecell{Suspected \\ DSCs}" "\makecell{Potential \\ DSCs}")  not nostar replace // addnote("`tableNote1'" "`tableNote2'" "`tableNote3'" "`tableNote4'") 


frame change default
frame drop temp


//Export proportion of rejection
frame create allStates
frame change allStates
import excel "Tables\RandTreatSim.xlsx", sheet("RandTreatRegAllStates") firstrow
replace Spec1P = Spec1P<0.05
replace Spec2P = Spec2P<0.05
replace Spec3P = Spec3P<0.05
replace Spec4P = Spec4P<0.05
frame create treatedStates
frame change treatedStates
import excel "Tables\RandTreatSim.xlsx", sheet("RandTreatRegTreatedStates") firstrow
replace Spec1P = Spec1P<0.05
replace Spec2P = Spec2P<0.05
replace Spec3P = Spec3P<0.05
replace Spec4P = Spec4P<0.05

//build table 
gen y = 1
gen x = 1

frame change treatedStates
qui reg y x
qui eststo Spec1 
sum Spec1P
estadd scalar treatStateP = `r(mean)'
frame change allStates
sum Spec1P
estadd scalar allStateP = `r(mean)'

frame change treatedStates
qui reg y x
qui eststo Spec2 
sum Spec2P
estadd scalar treatStateP = `r(mean)'
frame change allStates
sum Spec2P
estadd scalar allStateP = `r(mean)'

frame change treatedStates
qui reg y x
qui eststo Spec3 
sum Spec3P
estadd scalar treatStateP = `r(mean)'
frame change allStates
sum Spec3P
estadd scalar allStateP = `r(mean)'

frame change treatedStates
qui reg y x
qui eststo Spec4 
sum Spec4P
estadd scalar treatStateP = `r(mean)'
frame change allStates
sum Spec4P
estadd scalar allStateP = `r(mean)'

//Output table
esttab Spec1 Spec2 Spec3 Spec4 using "`tableSaveDir'RandTreatRejectionRates.tex", title("Random Treatment Simulation Rejection Rates at 5\% Significance \label{tab: RandTreatmentRejectionRates}") stats(treatStateP allStateP, labels("\makecell{Rejection Rate: \nextline Treated State Randomization}" "\hline \makecell{Rejection Rate: \nextline All State Randomization}")fmt(%8.3f %8.3f)) drop(*) mtitles("\makecell{Spec. 1}" "\makecell{Spec. 2}" "\makecell{Spec. 3}" "\makecell{Spec. 4}")  not nostar replace





