-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression_model.R
327 lines (257 loc) · 10.9 KB
/
regression_model.R
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#create a regression model to communicate the claim that higher temperatures cause higher mortality
setwd("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5")
##############
#LOAD PACKAGES
##############
library(ggplot2)
library(dplyr)
library(readr)
library(janitor)
library(lubridate)
library(maps)
library(mapdata)
library(ggmap)
library(sf)
library(spatialEco)
library(taRifx)
####################
#CLEAN CDC MORT DATA
####################
#read in tab delimited file
cdc_california_1999_2018 <- read_delim("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/cdc/cdc_california_1999_2018.txt",
"\t", escape_double = FALSE, col_types = cols(`Month Code` = col_date(format = "%Y/%m")), trim_ws = TRUE)
#lowercase variable names
cdc_california_1999_2018 <- clean_names(cdc_california_1999_2018)
#extract year and month from date variable
cdc_california_1999_2018 <- mutate(cdc_california_1999_2018, year=year(month_code), month=month(month_code))
#remove unnecessary columns
cdc_california_1999_2018 <- select(cdc_california_1999_2018, county_code, deaths, year, month)
###################
#CLEAN CDC POP DATA
###################
#read in tab delimited file
pop_california_1990_2018 <- read_delim("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/pop/pop_california_1990_2018.txt",
"\t", escape_double = FALSE, trim_ws = TRUE)
#lowercase variable names
pop_california_1990_2018 <- clean_names(pop_california_1990_2018)
#rename variables
pop_california_1990_2018 <- rename(pop_california_1990_2018, year=yearly_july_1st_estimates)
#remove unnecessary columns
pop_california_1990_2018 <- select(pop_california_1990_2018, county_code, year, population)
#remove rows from before 1999
pop_california_1999_2018 <- filter(pop_california_1990_2018, year>=1999)
######################
#CREATE JOINED DATASET
######################
#join population and mortality data
cdc_pop_1999_2018 <- full_join(pop_california_1999_2018,
cdc_california_1999_2018,
by = c("county_code", "year"))
#destring variables
cdc_pop_1999_2018 <- mutate(cdc_pop_1999_2018, county_code=destring(county_code), population=destring(population), deaths=destring(deaths))
#create countyfip column
cdc_pop_1999_2018 <- mutate(cdc_pop_1999_2018, countyfip=county_code-6000)
#create deathrate column
cdc_pop_1999_2018 <- mutate(cdc_pop_1999_2018, deathrate=deaths/population*100000)
#summarize
summary(cdc_pop_1999_2018$deathrate)
###################
#CLEAN WEATHER DATA
###################
#read in weather data
ghcn_california_1999_2019 <- read_csv("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/ghcn/ghcn_california_1999_2019.csv",
col_types = cols(DATE = col_date(format = "%Y-%m-%d")))
#clean variable names
ghcn_california_1999_2019 <- clean_names(ghcn_california_1999_2019)
#remove unnecessary columns
ghcn_california_1999_2019 <- select(ghcn_california_1999_2019, -name, -latitude, -longitude, -elevation)
#create year and month column
ghcn_california_1999_2019 <- mutate(ghcn_california_1999_2019, year=year(date), month=month(date))
#remove 2019 since we don't have mortality data for this year
ghcn_california_1999_2018 <- filter(ghcn_california_1999_2019, year<2019)
#summarize
summary(ghcn_california_1999_2018$tmax)
################################
#IMPORT STATION-COUNTY CROSSWALK
################################
crosswalk_100km <- read_csv("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/station_county_crosswalk_100km.csv")
########################################
#JOIN WEATHER DATA WITH COUNTY CROSSWALK
########################################
ghcn_stn_100km <- full_join(crosswalk_100km, ghcn_california_1999_2018, by = "station")
#########################
#INVERSE DISTANCE WEIGHTS
#########################
ghcn_stn_100km <- mutate(ghcn_stn_100km, invdist=1/distance)
############################################
#SUMMARIZE WEATHER COUNTY-YEAR-MONTH AVERAGE
############################################
#group
ghcn_stn_100km <- group_by(ghcn_stn_100km, countyfip, year, month)
#summarize
ghcn_county_100km <- summarise(ghcn_stn_100km,
tmax=weighted.mean(tmax, invdist, na.rm = TRUE))
#ungroup
ghcn_stn_100km <- ungroup(ghcn_stn_100km)
#save
write_csv(ghcn_county_100km, "~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/outputs/ghcn_county_100km_california_1999_2018.csv")
########################################
#JOIN WITH CDC DATA AT COUNTY-YEAR-MONTH
########################################
cdc_ghcn_100km <- full_join(ghcn_county_100km,
cdc_pop_1999_2018,
by = c("countyfip", "year", "month"))
#save
write_csv(cdc_ghcn_100km, "~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/outputs/cdc_ghcn_100km_california_1999_2018.csv")
#######
#GRAPHS
#######
#scatterplot - all counties
ggplot(cdc_ghcn_100km,
aes(y=deathrate,
x=tmax,
size=population,
weight=population)) +
geom_point(color="plum1") +
geom_smooth(method = lm,
se = FALSE,
color="darkorchid4",
size=1) +
labs(y = "Death Rate",
x = "Max Temperature in CA Year-Round") +
theme(panel.background = element_rect(fill="white"),
legend.position = "none",
axis.line = element_line(colour = "darkorchid4") )
#scatterplot - all counties, August
ggplot(filter(cdc_ghcn_100km,
month==8),
aes(y=deathrate,
x=tmax,
size=population,
weight=population)) +
geom_point(color="plum1") +
geom_smooth(method = lm,
se = FALSE,
color="darkorchid4",
size=1) +
labs(y = "Death Rate",
x = "Max Temperature in CA in August") +
theme(panel.background = element_rect(fill="white"),
legend.position = "none",
axis.line = element_line(colour = "darkorchid4") )
#scatterplot - LA, August
ggplot(filter(cdc_ghcn_100km,
month==8 & countyfip==37),
aes(y=deathrate,
x=tmax,
size=population,
weight=population)) +
geom_point(color="plum1") +
geom_smooth(method = lm,
se = FALSE,
color="darkorchid4",
size=1) +
labs(y = "Death Rate",
x = "Max Temperature in Los Angeles in August") +
theme(panel.background = element_rect(fill="white"),
legend.position = "none",
axis.line = element_line(colour = "darkorchid4") )
############
#REGRESSIONS
############
#save a log file of regression estimates
sink("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/outputs/log_tutorial14.txt")
#all counties
regression1 <- lm(deathrate ~ tmax,
data=cdc_ghcn_100km,
weight=population)
summary(regression1)
#all counties, August only
regression2 <- lm(deathrate ~ tmax,
data=filter(cdc_ghcn_100km, month==8),
weight=population)
summary(regression2)
#LA, August only
regression3 <- lm(deathrate ~ tmax,
data=filter(cdc_ghcn_100km, month==8 & countyfip==37),
weight=population)
summary(regression3)
sink()
###############################################
#REMOVE AVG (FIXED) DIFFERENCES ACROSS COUNTIES
###############################################
#visualize data in three sample counties: LA, SB, Fresno
ggplot(filter(cdc_ghcn_100km,
(countyfip==37 | countyfip==83 | countyfip==19) & month==8),
aes(y=deathrate, x=tmax, color=factor(countyfip))) +
geom_point() +
geom_smooth(method = lm,
se = FALSE,
color="black",
size=1) +
labs(y = "Death Rate",
x = "Max Temperature") +
scale_colour_manual(values=c("dodgerblue1", "goldenrod1","limegreen")) +
theme(panel.background = element_rect(fill="white"),
legend.position = "none",
axis.line = element_line(colour = "black"))
#determine the average for a given county month
cdc_ghcn_100km <- group_by(cdc_ghcn_100km, countyfip, month)
cdc_ghcn_100km <- mutate(cdc_ghcn_100km,
tmax_avg=weighted.mean(tmax, population, na.rm = TRUE),
deathrate_avg=weighted.mean(deathrate, population, na.rm = TRUE))
#calculate the differences from the mean for each variable
cdc_ghcn_100km <- mutate(cdc_ghcn_100km,
tmax_dev=tmax-tmax_avg,
deathrate_dev=deathrate-deathrate_avg)
cdc_ghcn_100km <- ungroup(cdc_ghcn_100km)
#visualize deviations: LA, SB, Fresno
ggplot(filter(cdc_ghcn_100km, (countyfip==37 | countyfip==83 | countyfip==19) & month==8),
aes(y=deathrate_dev, x=tmax_dev, color=factor(countyfip))) +
geom_point() +
labs(y = "Death Rate",
x = "Max Temperature") +
scale_colour_manual(values=c("dodgerblue1", "goldenrod1","limegreen")) +
theme(panel.background = element_rect(fill="white"),
legend.position = "none",
axis.line = element_line(colour = "black"))
####################
#DEMEANED REGRESSION
####################
#save a log file of regression estimates
sink("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/outputs/log_tutorial15.txt")
#demeaned, August only
regression4 <- lm(deathrate_dev ~ tmax_dev,
data=filter(cdc_ghcn_100km, month==8),
weight=population)
summary(regression4)
############################################
#FIXED EFFECTS (DEMEAN ALL COUNTIES QUICKLY)
############################################
#fixed effect for county, August only
regression5 <- lm(deathrate ~ tmax + factor(countyfip),
data=filter(cdc_ghcn_100km, month==8),
weight=population)
summary(regression5)
sink()
######################
#CREATE DUMMY VARIABLE
######################
#create hotmonth dummy variable
dummy_august <- mutate(filter(cdc_ghcn_100km, month==8), hotmonth = ifelse(tmax>88, 1, 0))
#################################
#REGRESSIONS USING DUMMY VARIABLE
#################################
#save a log file of regression estimates
sink("~/Library/Mobile Documents/com~apple~CloudDocs/Env 175 Project 5/outputs/log_tutorial16.txt")
#all counties, August only, no fixed effect
regression6 <- lm(deathrate ~ hotmonth,
data = dummy_august,
weight=population)
summary(regression6)
#fixed effect for county, August only
regression7 <- lm(deathrate ~ hotmonth + factor(countyfip),
data = dummy_august,
weight=population)
summary(regression7)
sink()