Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
****&&&&& MUST RUN THE FOLLOWING BEFORE RUNNING:
****&&&& ssc install iptATE (this is case-sensitive)
****&&&& ssc install estout
****&&&& ssc install graph3d
****&&&& change directory to be the same one where the data are located
log using graph300, text replace
*expand matsize
set matsize 300
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300"
**have one set of 500 repetitions (i.e., observationsnum=500) so that we can just draw the graphs for the first repetition
local repetitions_300=1
local observationsnum=300*500
****&&&& NOTE: MUST CHANGE THE DIRECTORY BELOW DEPENDING ON THE PATH OF THE USER
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\"
*******************************************NOW USE THIS OPTIMAL BANDWIDTH TO RUN GIPT WITH THE 500 REPETITIONS********************
forvalues j=1/`repetitions_300' {
****&&&& NOTE: MUST CHANGE THE DIRECTORY BELOW DEPENDING ON THE PATH OF THE USER
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\data_300"
import delimited using cross_section_data300100.csv, clear
gen optimal_b=0.85
gen z=0
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph"
file open applic_file_300_`j' using "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_`j'.txt", write
****the next 2 lines create a block of 300 observations, so the first 300 have a=1, the next 300 have a=2, etc
set obs `observationsnum'
egen a = seq(), b(300)
label data data_300
local datasample= _N
gen id= _n
gen Lat=g1
gen Long=g2
gen y1=y
gen x1=x
gen x12=x^2
*d_treatnew is the treatment dummy, i.e., properties in the city (d_treat) after the storm (d_time)
gen d_treat=dtreat
gen d_time=dtime
gen d_treatnew=d_treat*d_time
keep if `j' == a
file write applic_file_300_`j' "number" _tab "b_ate" _tab "se_ate" _tab "b_trueate" _n
forvalues i = 1/300 {
gen dist`i' = ((Long-Long[`i'])^2+(Lat-Lat[`i'])^2)^0.5
gen paren`i'=exp(-0.5*(dist`i'/(optimal_b))^2)
gen w_w_`i'=(paren`i')^0.5
label variable y1 "y"
label variable x1 "x"
label variable d_treatnew "Treatment Dummy"
*create w times x
gen x1_w_w_`i' = w_w_`i'*x1
gen x12_w_w_`i' =((w_w_`i')*x1)^2
gen dum_treat_w_w_`i' =(w_w_`i')*d_treatnew
*run iptATE
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300"
iptATE y dum_treat_w_w_`i' x1_w_w_`i' x12_w_w_`i' , optroutine(e2)
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph"
*save coefficients and V-cov matrix
mat b_w_w_`i'=e(b)
mat v_w_w_`i'=e(V)
*note h_b and h_v is the column number where the ate is listed, in both the parameter vector and the vcov vector
scalar h_b=colnumb(b_w_w_`i', "ate:gamma")
scalar h_v=colnumb(v_w_w_`i', "ate:gamma")
*here use `i' because the ATE varies with each observation in GIPT
scalar v_ATE`i'=v_w_w_`i'[h_v,h_v]
scalar b_ate`i'=b_w_w_`i'[1,h_b]
scalar trueate`i' = trueate[`i']
*take the square root of the ate variance to get the standard error (se) of the ate
scalar se_ate`i'=(v_ATE`i')^0.5
*write b and se to txt file
file write applic_file_300_`j' ("`i'") _tab (b_ate`i') _tab (se_ate`i') _tab (trueate`i') _n
drop dist`i'
drop paren`i'
drop w_w_`i'
drop x1_w_w_`i'
drop x12_w_w_`i'
drop dum_treat_w_w_`i'
}
file close applic_file_300_`j'
save "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\data_300_`j'.dta"
label data bw_select_300_July2018_with_weights_300.dta
**********************************************
clear
import delimited "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_`j'.txt"
*since the id is 1 thru 300 in all iterations of myfile_`bw'_`j' but id is the actual number from the original data file in bwselect_300_July2018_with_weights_`bw'_`j'.dta we need to make the transforation in the next line
gen id=number +(300*(`j'-1))
label variable b_ate "ATE"
label variable se_ate "Standard Errors of ATE"
label variable b_trueate "True ATE"
merge 1:1 id using "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\data_300_`j'.dta"
drop _merge
export delimited "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\data_for_graph_300_`j'.csv",
*now calculate MASE, noting that since the 100 reps are all in one, calculating the ASE for all obs is the same as doing the ASE for each sample, then taking the mean of the ASEs
gen difference_300=b_ate-trueate
gen difference2_300=difference_300^2
sum difference2_300
mat ASE_mat_300_`j'=r(mean)
scalar ASE_300_`j'=ASE_mat_300_`j'[1,1]
sum difference_300
mat bias_mat_300_`j'=r(mean)
scalar bias_300_`j'=bias_mat_300_`j'[1,1]
set obs 300
drop id
drop b_ate
drop se_ate
drop b_trueate
}
scalar MASE_sum_300=0
scalar MASE_300=0
scalar bias_300=0
scalar bias_sum_300=0
forvalues j=1/`repetitions_300' {
scalar MASE_sum_300 = ASE_300_`j' + MASE_300
scalar MASE_300 = MASE_sum_300
scalar bias_sum_300 = bias_300_`j' + bias_300
scalar bias_300 = bias_sum_300
}
scalar MASE_300 = MASE_sum_300/`repetitions_300'
scalar bias_300 = bias_sum_300/`repetitions_300'
forvalues j=1/`repetitions_300' {
clear
import delimited "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_`j'.txt"
save "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\stata_myfile_300_`j'", replace
}
clear
use "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\stata_myfile_300_1"
save "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_all", replace
clear
use "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_all"
forvalues j=2/`repetitions_300' {
append using "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\stata_myfile_300_`j'"
save "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_all", replace
}
export delimited "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\all_data_300.txt", delimiter(tab)
*rename applic_file so that there won't be problems the next time this program runs
shell ren "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps_makegraph\myfile_300_`j'.txt" "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300\dir_300_500reps\testfile_300_`j'.txt", replace
cd "C:\Users\jcohen2\Desktop\Bryan Graham Stata software\bandwidth_selection_300"
log close