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/"
}

if("`c(os)'"=="Windows"){
	local dataFile = "Tables\iv.xlsx"
}
else{
	
	local dataFile = "Tables/iv.xlsx"
}


//Create new data frame
frame create vmtResults
frame change vmtResults

//Import externality information
import excel "`dataFile'", sheet("Spec2")

qui sum B if A=="pi"
local piVMT = r(mean)

qui sum B if A=="beta"
local beta = r(mean)

qui sum C if A=="95% Confidence Set"
local betaHigh = r(mean)

qui sum B if A=="95% Confidence Set"
local betaLow = r(mean)

local betaZero = 0

local priaNull = 1.6
local friaNull = -2.5
local priaStarNull = 0.3
local friaStarNull = 0
local jacobsenNull = 1

//Save matrix with travel elasticity information
mat betaList = [`betaLow', `beta', `betaZero', `betaHigh', `jacobsenNull', `priaNull', `friaNull', `priaStarNull', `friaStarNull']


frame create gasUseResults
frame change gasUseResults

import excel "`dataFile'", sheet("Spec2GasUse")

qui sum B if A=="pi"
local piGasUse = r(mean)

qui sum B if A=="beta"
local beta = r(mean)

qui sum C if A=="95% Confidence Set"
local betaHigh = r(mean)

qui sum B if A=="95% Confidence Set"
local betaLow = r(mean)

local betaZero = 0

di "`pi'"
di "`beta'"

local priaNull = -4.8
local friaNull = 9.7
local priaStarNull = -6.1
local friaStarNull = 11
local jacobsenNull = 1

//Save matrix with gas use elasticity information
mat gammaList = [`betaLow', `beta', `betaZero', `betaHigh', `jacobsenNull', `priaNull', `friaNull', `priaStarNull', `friaStarNull']



frame create analysisData
frame change analysisData

do LoadDataVMT

keep if year>=endedsafety

sort year


//From I. W. H. Parry, M. Walls, and W. Harrington, “Automobile externalities and policies,” J. Econ. Lit., vol. 45, no. 2, pp. 373–399, 2007.
//Table 2
local mileageCosts = .1 //Dollars/mile
local fuelCosts = .18 //Dollars/gallon

//Get number of years used in each 
qui sum logvmt 
local stateYearsVMT = r(N)

qui sum loghighwaygasuse
local stateYearsGasUse = r(N)

//Policy in this context is the removal of safety inspections
foreach index of num 1(1)9{
	
	local tempBeta = betaList[1,`index']
	local tempGamma = gammaList[1,`index']
	
	local tempVMTDelta = `piVMT'*`tempBeta'
	local tempGasUseDelta = `piGasUse'*`tempGamma'
	
	di "`index':`tempVMTDelta':`tempVMTDelta'"
	
	if(`index'==1){
		//generate the different fleet sizes for VMT model
		qui gen policyRegVMT = exp(logregistrations)

		qui gen noPolicyRegVMT = exp(logregistrations)*(1-`piVMT')
		qui gen deltaRegVMT = policyRegVMT-noPolicyRegVMT

		//generate the different VMT amounts
		qui gen policyVMT = exp(logvmt)

		qui gen noPolicyVMT = exp(logvmt)*(1-`tempVMTDelta')
		qui gen deltaVMT = policyVMT-noPolicyVMT
		
		//generate the different fleet sizes for gas use model
		qui gen policyRegGasUse = exp(logregistrations)

		qui gen noPolicyRegGasUse = exp(logregistrations)*(1-`piGasUse')
		qui gen deltaRegGasUse = policyRegGasUse-noPolicyRegGasUse
		
		//generate the different gas use amounts
		qui gen policyGasUse = exp(loghighwaygasuse)

		qui gen noPolicyGasUse = exp(loghighwaygasuse)*(1-`tempGasUseDelta')
		qui gen deltaGasUse = policyGasUse-noPolicyGasUse
		
		//VMT per vehicle added
		qui gen vmtPerVehAdded = deltaVMT/deltaRegVMT
		
		//VMT per vehicle added
		qui gen gasUsePerVehAdded = deltaGasUse/deltaRegGasUse
		
		//VMT State years
		qui gen stateYearsVMT = `stateYearsVMT'
		qui gen stateYearsGasUse = `stateYearsGasUse'
	}
	else{
		//generate the different fleet sizes
		qui replace policyRegVMT = exp(logregistrations)

		qui replace noPolicyRegVMT = exp(logregistrations)*(1-`piVMT')
		qui replace deltaRegVMT = policyRegVMT-noPolicyRegVMT

		//generate the different VMT amounts
		qui replace policyVMT = exp(logvmt)

		qui replace noPolicyVMT = exp(logvmt)*(1-`tempVMTDelta')
		qui replace deltaVMT = policyVMT-noPolicyVMT
		
		//generate the different fleet sizes for gas use model
		qui replace policyRegGasUse = exp(logregistrations)

		qui replace noPolicyRegGasUse = exp(logregistrations)*(1-`piGasUse')
		qui replace deltaRegGasUse = policyRegGasUse-noPolicyRegGasUse
		
		
		//generate the different gas use amounts
		qui replace policyGasUse = exp(loghighwaygasuse)

		qui replace noPolicyGasUse = exp(loghighwaygasuse)*(1-`tempGasUseDelta')
		qui replace deltaGasUse = policyGasUse-noPolicyGasUse

		//VMT per vehicle added
		qui replace vmtPerVehAdded = deltaVMT/deltaRegVMT
		
		//VMT per vehicle added
		qui replace gasUsePerVehAdded = deltaGasUse/deltaRegGasUse
		
		//VMT State years
		qui replace stateYearsVMT = `stateYearsVMT'
		qui replace stateYearsGasUse = `stateYearsGasUse'
		
	}
	
	
	
	
	frame copy analysisData externalities`index'
	
}

//Run relevant python scripts
python script "`pythonSaveDir'calculateExternalities.py"
python script "`pythonSaveDir'plotExternalities.py"
python script "`pythonSaveDir'plotElasticityComparisons.py"
