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 fuel use 
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'"

//Gas use Controls
local spec2GasUseControls = "`spec1Controls'"
local spec3GasUseControls = "`spec2GasUseControls' i.gasUseDummyExtSpec3"
local spec4GasUseControls = "`spec2GasUseControls' i.gasUseDummyExtSpec4"

//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 = 11
local altNull = round(9.7, .1)
local altNull2 = 1
local altNull3 = -4.8
local altNull4 = -6.1

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

local fsVarsSpec1 = "logregistrations i.nosafetyind `spec1Controls'"
local rfVarsSpec1 = "loghighwaygasuse i.nosafetyind `spec1Controls'"
local modelTitleSpec1 = "Spec1GasUse"
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 = "loghighwaygasuse i.nosafetyind `spec2GasUseControls'"
local modelTitleSpec2 = "Spec2GasUse"
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 = "loghighwaygasuse i.nosafetyind `spec3GasUseControls'"
local modelTitleSpec3 = "Spec3GasUse"
local nullLowSpec3 = -1
local nullDeltaSpec3 = 0.001
local nullHighSpec3 = 8.8

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

local fsVarsSpec4 = "logregistrations i.nosafetyind `spec4RegControls'"
local rfVarsSpec4 = "loghighwaygasuse i.nosafetyind `spec4GasUseControls'"
local modelTitleSpec4 = "Spec4GasUse"
local nullLowSpec4 = 0
local nullDeltaSpec4 = 0.001
local nullHighSpec4 = 14

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


//per capita models

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and fuel use 
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.regPerCapitaDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regPerCapitaDummyExtSpec4"


di "`spec3RegControls'"

//Gas use Controls
local spec2GasUseControls = "`spec1Controls'"
local spec3GasUseControls = "`spec2GasUseControls' i.gasPerCapitaDummyExtSpec3"
local spec4GasUseControls = "`spec2GasUseControls' i.gasPerCapitaDummyExtSpec4"

local treatmentVar = "1.nosafetyind"

local fsVarsSpec1 = "logRegPerCapita i.nosafetyind `spec1Controls'"
local rfVarsSpec1 = "logGasUsePerCapita i.nosafetyind `spec1Controls'"
local modelTitleSpec1 = "Spec1GasUsePerCapita"
local nullLowSpec1 = -1
local nullDeltaSpec1 = 0.001
local nullHighSpec1 = 20


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

local fsVarsSpec2 = "logRegPerCapita i.nosafetyind `spec2RegControls'"
local rfVarsSpec2 = "logGasUsePerCapita i.nosafetyind `spec2GasUseControls'"
local modelTitleSpec2 = "Spec2GasUsePerCapita"
local nullLowSpec2 = -1
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 20

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


local fsVarsSpec3 = "logRegPerCapita i.nosafetyind `spec3RegControls'"
local rfVarsSpec3 = "logGasUsePerCapita i.nosafetyind `spec3GasUseControls'"
local modelTitleSpec3 = "Spec3GasUsePerCapita"
local nullLowSpec3 = -5.8
local nullDeltaSpec3 = 0.001
local nullHighSpec3 = 0.1

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

local fsVarsSpec4 = "logRegPerCapita i.nosafetyind `spec4RegControls'"
local rfVarsSpec4 = "logGasUsePerCapita i.nosafetyind `spec4GasUseControls'"
local modelTitleSpec4 = "Spec4GasUsePerCapita"
local nullLowSpec4 = -1
local nullDeltaSpec4 = 0.001
local nullHighSpec4 = 2.3

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

//Combine all of the IV information
python script "`pythonSaveDir'combineIV.py"

//Output tables
constructTable GasUsePerCapitaiv; Fleet Gas Use Per Capita; Spec1GasUsePerCapita Spec2GasUsePerCapita Spec3GasUsePerCapita Spec4GasUsePerCapita; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
constructTable GasUseiv; Fleet Gas Use; Spec1GasUse Spec2GasUse Spec3GasUse Spec4GasUse; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
