//Event Study Models
eststo clear

if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\ES\"
	local plotSaveDir = "Plots\ES\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/ES/"
	local plotSaveDir = "Plots/ES/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
local spec1Controls = "ib51.stateGroup ib1995.year asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"
local spec2Controls = "`spec1Controls' i.transactionDataDummy"
local spec3Controls = "`spec2Controls' i.regDummyExtSpec3"
local spec4Controls = "`spec2Controls' i.regDummyExtSpec4"

//Establish Cluster Variable
local clusterVar = "stateGroup"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Event Study Table format
local fmtEventStudy = `"cells(b(fmt(3) star) se(par fmt(3))) keep(*shiftedBoundRelYear*) nocon label compress longtable mtitles("\makecell{No \\ DSCs}" "\makecell{Documented \\ DSCs}" "\makecell{Suspected \\ DSCs}" "\makecell{Potential \\ DSCs}") starlevels(* 0.1 ** 0.05 *** 0.01) addnotes("* p \textless 0.1, ** p \textless 0.05, *** p \textless 0.01" "Note: All models include state and year fixed effects as well as state-level time trends." "All models cluster standard errors  by state.")"'


//Loop through the number of dummies +- one year prior to the event
foreach bound of num 6 11{
	
	preserve
	
	//Add Dummy Variables for Event Study
	//Define Bounds
	local lowerBound = -`bound'
	local upperBound = `bound'
	
	//Define the relative time period that we will use
	local relYear = -1

	//Determine the dummy index of the relative year
	local relIndex = 1-`lowerBound'+`relYear'
	
	//Generate New Variable
	gen boundRelYear = yearrelativeend
	//Enforce Bounds
	replace boundRelYear = `lowerBound' if boundRelYear<=`lowerBound'
	replace boundRelYear = `upperBound' if boundRelYear>=`upperBound'
	replace boundRelYear = . if yearrelativeend==.
	
	//Create shifted Bounds
	gen shiftedBoundRelYear = boundRelYear - `lowerBound'+1
	replace shiftedBoundRelYear = `relIndex' if boundRelYear==.



	//Create Dummies
	qui tab boundRelYear, gen(boundRelYear)
	//Figure out which index for the dummy variables is the last one
	local finalInd = `upperBound'-`lowerBound'+1


	// Drop the relative year column for ease of use in the event study.
	qui drop boundRelYear`relIndex'


	local missingDataChar = uchar(8224)
	
	if `upperBound'==6{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 13.shiftedBoundRelYear) omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Registrations" "relative to {&tau}=-1")"'


		
		local plotTitle = "Event Study Relative Year Fixed Effects"
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "-Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -6" 2 "t=-5" 3 "t=-4" 4 "t=-3" 5 "t=-2" 6 "t=-1" 7 "t=0" 8 "t=1" 9 "t=2" 10 "t=3" 11 "t=4" 12 "t=5" 13 "t $\geq$ 6"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = ""
	}
	else{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 23.shiftedBoundRelYear) omitted baselevels xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0) labsize(small)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Registrations" "relative to {&tau}=-1")"'
		
		
		local plotTitle = ""
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -11" 2 "t=-10" 3 "t=-9" 4 "t=-8" 5 "t=-7" 6 "t=-6" 7 "t=-5" 8 "t=-4" 9 "t=-3" 10 "t=-2" 11 "t=-1" 12 "t=0" 13 "t=1" 14 "t=2" 15 "t=3" 16 "t=4" 17 "t=5" 18 "t=6" 19 "t=7" 20 "t=8" 21 "t=9" 22 "t=10" 23 "t $\geq$ 11"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = "-`missingDataChar' denotes a relative year with >1 state missing data"
	}
	
	

	//Preferred Models
	//Spec. 1
	qui reghdfe logregistrations ib`relIndex'.shiftedBoundRelYear `spec1Controls', vce (cluster `clusterVar') absorb(`absorbVars')
	coefplot, `plotFormat'  name("spec1WoTT`upperBound'") note("`plotNote'" "`plotNote2'") graphregion(color(white)) plotregion(color(white))
	graph export "`plotSaveDir'RegESSpec1WoTT`upperBound'.png", name("spec1WoTT`upperBound'") replace width(`widthPix') //title("No DSCs Spec.")
	eststo spec1
	
	//Spec. 2
	qui reghdfe logregistrations ib`relIndex'.shiftedBoundRelYear `spec2Controls', vce (cluster `clusterVar')  absorb(`absorbVars')
	coefplot, `plotFormat'  name("spec2WoTT`upperBound'") note("`plotNote'" "`plotNote2'") graphregion(color(white)) plotregion(color(white))
	graph export "`plotSaveDir'RegESSpec2WoTT`upperBound'.png", name("spec2WoTT`upperBound'") replace width(`widthPix') //title("Documented DSCs Spec.")
	graph export "`plotSaveDir'RegESSpec2WoTT`upperBound'.pdf", name("spec2WoTT`upperBound'") replace
	eststo spec2
	
	//Spec. 3
	qui reghdfe logregistrations ib`relIndex'.shiftedBoundRelYear `spec3Controls', vce (cluster `clusterVar') absorb(`absorbVars')
	coefplot, `plotFormat' name("spec3WoTT`upperBound'") note("`plotNote'" "`plotNote2'") graphregion(color(white)) plotregion(color(white))
	graph export "`plotSaveDir'RegESSpec3WoTT`upperBound'.png", name("spec3WoTT`upperBound'") replace width(`widthPix') // title("Suspected DSCs Spec.")
	eststo spec3
	//Spec. 4
	qui reghdfe logregistrations ib`relIndex'.shiftedBoundRelYear `spec4Controls', vce (cluster `clusterVar') absorb(`absorbVars')
	coefplot, `plotFormat'  name("spec4WoTT`upperBound'") note("`plotNote'" "`plotNote2'") graphregion(color(white)) plotregion(color(white))
	graph export "`plotSaveDir'RegESSpec4WoTT`upperBound'.png", name("spec4WoTT`upperBound'") replace width(`widthPix')  //title("Potential DSCs Spec.")
	eststo spec4
	
	
	
	//Store Table

	if `upperBound'==6{
		local title = "Registrations Event Study Results Small Window w/o Time Trends \label{tab:ESWoTT`upperBound'}"
	}
	else{
		local title = "Registrations Event Study Results Large Window w/o Time Trends \label{tab:ESWoTT`upperBound'}"
	}
	
	graph combine spec1WoTT`bound' spec2WoTT`bound' spec3WoTT`bound' spec4WoTT`bound', ycommon
	graph export "`plotSaveDir'RegESCombinedWoTT`upperBound'.png", replace width(`widthPix')

	esttab spec1 spec2 spec3 spec4 using "`tableSaveDir'RegESWoTT`upperBound'.tex", `fmtEventStudy' title(`title') replace
	
	restore
}
