-
Notifications
You must be signed in to change notification settings - Fork 0
/
12 - Panel Nighttime Inversion Analysis.do
144 lines (101 loc) · 5.36 KB
/
12 - Panel Nighttime Inversion Analysis.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
* Start with a clean slate.
log close _all
clear all
***************** Make sure the needed packages are installed ******************
* Install estout to get nice output from regressions
ssc install estout
* Install ftools (remove program if it existed previously)
cap ado uninstall ftools
net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")
* Install reghdfe
cap ado uninstall reghdfe
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")
* Install boottest (Stata 11 and 12)
if (c(version)<13) cap ado uninstall boottest
if (c(version)<13) ssc install boottest
* Install moremata (sometimes used by ftools but not needed for reghdfe)
cap ssc install moremata
* Install ivreg2, the core package
cap ado uninstall ivreg2
ssc install ivreg2
* Finally, install this package
cap ado uninstall ivreghdfe
net install ivreghdfe, from(https://raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/)
************************** Done installing packages ****************************
********* Make changes below to switch between local and ACCRE *****************
* Set the working directory.
cd "E:/Research Projects/Worker Accidents and Pollution/Regression Models"
* Start logging
log using nighttime_linear_analysis.log, replace name(main)
* Import the clean data file, produced using R. I'm using Stata for the analysis.
* because Stata works with panel data a little easier.
import delimited "../Data/Data for Regression Models/Nighttime Inversions/construction_accidents_2003_to_2015.csv", varnames(1) numericcols(3/10)
* Take a smaller sample for local code testing
keep if strpos(date, "2005")
********* End of section to change when switching between local and ACCRE ******
********* Basic fixes to data file, applicable to every regression *************
* Replace the string date variable with one readable by Stata.
gen temporary_date = date(date, "YMD")
drop date
rename temporary_date date
format date %td
* Create a month variable so I can absorb month-of-year fixed effects
gen month = month(date)
* Create weekday dummy variables, since ivreg2 can't handle factor variables
tabulate weekday, generate(weekday_dummy_)
drop weekday_dummy_1
* Destring mean_pm25 because somehow it imported as a date.
destring mean_pm25, force replace
* Drop any observations from Alaska, Hawaii, or Puerto Rico.
drop if floor(fips / 1000) == 2 | floor(fips / 1000) == 15 | floor(fips / 1000) == 72
* Declare the data as panel data.
xtset fips date
* Make a binary for an accident occurring.
gen accident_occurred = 1 if num_accidents > 0
replace accident_occurred = 0 if accident_occurred == .
********* End basic fixes to data file, applicable to every regression *********
rename inversion_coverage nighttime_inversion_coverage
keep fips date nighttime_inversion_coverage
save nighttime_temporary, replace
clear
* Import the file with all inversions
import delimited "../Data/Data for Regression Models/construction_accidents_2003_to_2015.csv", varnames(1) numericcols(3/10)
************* Redo the basic fixes to the all inversion file *******************
* Replace the string date variable with one readable by Stata.
gen temporary_date = date(date, "YMD")
drop date
rename temporary_date date
format date %td
* Create a month variable so I can absorb month-of-year fixed effects
gen month = month(date)
* Create weekday dummy variables, since ivreg2 can't handle factor variables
tabulate weekday, generate(weekday_dummy_)
drop weekday_dummy_1
* Destring mean_pm25 because somehow it imported as a date.
destring mean_pm25, force replace
* Drop any observations from Alaska, Hawaii, or Puerto Rico.
drop if floor(fips / 1000) == 2 | floor(fips / 1000) == 15 | floor(fips / 1000) == 72
* Declare the data as panel data.
xtset fips date
* Make a binary for an accident occurring.
gen accident_occurred = 1 if num_accidents > 0
replace accident_occurred = 0 if accident_occurred == .
******************** End basic fixes to all inversion file *********************
* merge in the nighttime inversion data, then delete that temporary file
merge 1:1 fips date using nighttime_temporary, keep(match)
erase nighttime_temporary.dta
* inversion coverage = 0.375 * nighttime_inversion_coverage + 0.625 * daytime_inversion_coverage
* since "nighttime" is hours 0000, 0300, and 0600, with "daytime" being the rest.
gen daytime_inversion_coverage = 1.6 * (inversion_coverage - (0.375 * nighttime_inversion_coverage))
* Use days when there is "high" inversion coverage at night and "low" inversion coverage during the day
* as an instrument for PM 2.5 that shouldn't be causing fog or decreased visibility.
gen nighttime_only = 1 if nighttime_inversion_coverage > 0.75 & daytime_inversion_coverage < 0.25
replace nighttime_only = 0 if nighttime_only == .
* Do regressions using other PM 2.5 measures
eststo, title(".25-.75"): ivreghdfe accident_occurred mean_temperature mean_precipitation employment weekday_dummy_* (mean_pm25 = nighttime_only), absorb(fips) cluster(fips) first
* Save the stored regressions as latex and rtf tables, then clear them so I can save the next batch.
esttab using nighttime_linear_analysis.tex, replace label mtitles booktabs alignment(D{.}{.}{-1}) title(Exposure, Shock, and Moving Average Experiments\label{tab1})
esttab using nighttime_linear_analysis.rtf, replace label mtitles nogap onecell
eststo clear
* Close the log
log close main