eststo clear


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

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and VMT 
local spec1Controls = "ib51.stateGroup ib1995.year c.normYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local spec2RegControls = "`spec1Controls' i.transactionDataDummy"
local spec3RegControls = "`spec2RegControls' i.regDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regDummyExtSpec4"


di "`spec3RegControls'"

//VMT Controls
local spec2VMTControls = "`spec1Controls'"
local spec3VMTControls = "`spec2VMTControls' i.vmtDummyExtSpec3"
local spec4VMTControls = "`spec2VMTControls' i.vmtDummyExtSpec4"

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

//Establish Cluster Variable
local clusterVar = "stateGroup"

//Establish primary null
local primaryNull = 1.6
local altNull = 1
local altNull2 = 0.3
local altNull3 = 0
local altNull4 = -2.5


program LOO
version 16.1
	gettoken fsVars 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken rfVars 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken clusterVar 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken treatmentVar 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken modelTitle 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken primaryNull 0:0, parse(",")
	
	qui putexcel set "Tables/iv.xlsx", sheet("LOO", replace) modify
	
	tempname null tempPi tempDelta tempPiVar tempDeltaVar tempCovar
	
	local index = 2
	
	qui putexcel A1 = "State Removed"
	qui putexcel B1 = "Pi"
	qui putexcel C1 = "Pi Variance"
	qui putexcel D1 = "Delta"
	qui putexcel E1 = "Delta Variance"
	qui putexcel F1 = "Pi-Delta Covariance"
	qui putexcel G1 = "AR(Beta=`primaryNull')"
	qui putexcel H1 = "P-Value(Beta=`primaryNull')"
	
	qui levelsof statecode
	foreach tempSC in `r(levels)'{
		preserve
		di 
		qui keep if statecode=="`tempSC'"
		
			qui putexcel A`index' = "`tempSC'"
			
			jointEstimate `fsVars' if statecode=="`tempSC'", `rfVars', `clusterVar', `treatmentVar', "Working on `modelTitle' LOO w/o `tempSC'", `tempSC'
	
			scalar `tempPi' = `r(pi)'
			scalar `tempDelta' = `r(delta)'
			scalar `tempPiVar' = `r(piVar)'
			scalar `tempDeltaVar' = `r(deltaVar)'
			scalar `tempCovar' = `r(covar)'
			
			qui putexcel B`index' = `tempPi'
			qui putexcel C`index' = `tempPiVar'
			qui putexcel D`index' = `tempDelta'
			qui putexcel E`index' = `tempDeltaVar'
			qui putexcel F`index' = `tempCovar'
			
			testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `primaryNull'
			
			qui putexcel G`index' = `r(arStat)'
			qui putexcel H`index' = `r(pVal)'
			
		local index = `index'+1
		restore
	}
	

	
	
	
end



//non-normalized models
local treatmentVar = "1.nosafetyind"

local fsVarsSpec1 = "logregistrations i.nosafetyind `spec1Controls'"
local rfVarsSpec1 = "logvmt i.nosafetyind `spec1Controls'"
local modelTitleSpec1 = "Spec1"
local nullLowSpec1 = -3.6
local nullDeltaSpec1 = 0.001
local nullHighSpec1 = 1


runModel `fsVarsSpec1', `rfVarsSpec1', `clusterVar', `treatmentVar', `modelTitleSpec1', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsVarsSpec2 = "logregistrations i.nosafetyind `spec2RegControls'"
local rfVarsSpec2 = "logvmt i.nosafetyind `spec2VMTControls'"
local modelTitleSpec2 = "Spec2"
local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 1

runModel `fsVarsSpec2', `rfVarsSpec2', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'


local fsVarsSpec3 = "logregistrations i.nosafetyind `spec3RegControls'"
local rfVarsSpec3 = "logvmt i.nosafetyind `spec3VMTControls'"
local modelTitleSpec3 = "Spec3"
local nullLowSpec3 = -8
local nullDeltaSpec3 = 0.001
local nullHighSpec3 = 5

runModel `fsVarsSpec3', `rfVarsSpec3', `clusterVar', `treatmentVar', `modelTitleSpec3', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsVarsSpec4 = "logregistrations i.nosafetyind `spec4RegControls'"
local rfVarsSpec4 = "logvmt i.nosafetyind `spec4VMTControls'"
local modelTitleSpec4 = "Spec4"
local nullLowSpec4 = -5
local nullDeltaSpec4 = 0.001
local nullHighSpec4 = 4

runModel `fsVarsSpec4', `rfVarsSpec4', `clusterVar', `treatmentVar', `modelTitleSpec4', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'


//per capita models
//Registration Controls
local spec2RegControls = "`spec1Controls' i.transactionDataDummy"
local spec3RegControls = "`spec2RegControls' i.regPerCapitaDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regPerCapitaDummyExtSpec4"


di "`spec3RegControls'"

//VMT Controls
local spec2VMTControls = "`spec1Controls'"
local spec3VMTControls = "`spec2VMTControls' i.vmtPerCapitaDummyExtSpec3"
local spec4VMTControls = "`spec2VMTControls' i.vmtPerCapitaDummyExtSpec4"

local treatmentVar = "1.nosafetyind"

local fsVarsSpec1 = "logRegPerCapita i.nosafetyind `spec1Controls'"
local rfVarsSpec1 = "logVMTPerCapita i.nosafetyind `spec1Controls'"
local modelTitleSpec1 = "Spec1PerCapita"
local nullLowSpec1 = -1
local nullDeltaSpec1 = 0.001
local nullHighSpec1 = 34


runModel `fsVarsSpec1', `rfVarsSpec1', `clusterVar', `treatmentVar', `modelTitleSpec1', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsVarsSpec2 = "logRegPerCapita i.nosafetyind `spec2RegControls'"
local rfVarsSpec2 = "logVMTPerCapita i.nosafetyind `spec2VMTControls'"
local modelTitleSpec2 = "Spec2PerCapita"
local nullLowSpec2 = -1
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 30

runModel `fsVarsSpec2', `rfVarsSpec2', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'


local fsVarsSpec3 = "logRegPerCapita i.nosafetyind `spec3RegControls'"
local rfVarsSpec3 = "logVMTPerCapita i.nosafetyind `spec3VMTControls'"
local modelTitleSpec3 = "Spec3PerCapita"
local nullLowSpec3 = -9
local nullDeltaSpec3 = 0.001
local nullHighSpec3 = 1

runModel `fsVarsSpec3', `rfVarsSpec3', `clusterVar', `treatmentVar', `modelTitleSpec3', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsVarsSpec4 = "logRegPerCapita i.nosafetyind `spec4RegControls'"
local rfVarsSpec4 = "logVMTPerCapita i.nosafetyind `spec4VMTControls'"
local modelTitleSpec4 = "Spec4PerCapita"
local nullLowSpec4 = -1000
local nullDeltaSpec4 = 1
local nullHighSpec4 = 1000

runModel `fsVarsSpec4', `rfVarsSpec4', `clusterVar', `treatmentVar', `modelTitleSpec4', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'


python script "`pythonSaveDir'combineIV.py" 


constructTable VMTPerCapitaiv; Fleet Travel Distance Per Capita; Spec1PerCapita Spec2PerCapita Spec3PerCapita Spec4PerCapita; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
constructTable VMTiv; Fleet Travel Distance ; Spec1 Spec2 Spec3 Spec4; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
