-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject2_RCode_to_actually_use.R
386 lines (241 loc) · 15.6 KB
/
Project2_RCode_to_actually_use.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
library(tidyverse)
library(R.utils)
library(lubridate)
if(!file.exists("./Proj2data")){dir.create("./Proj2data")} # Check to see if the subdirectory for storing our data exists.
# If it does not, create it.
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2" # Set the DL link url.
download.file(fileUrl,destfile="./Proj2data/StormData.csv.bz2",method="curl") # Actually download the file.
gunzip(filename = "./Proj2data/StormData.csv.bz2", destname = "./Proj2data/StormData.csv",
skip = TRUE, ext = "bz2")
stormdata <- as_tibble(data.table::fread(file = "./Proj2data/StormData.csv", stringsAsFactors = FALSE, strip.white = TRUE))
glimpse(stormdata)
stormdata$BGN_DATE <- mdy_hms(stormdata$BGN_DATE)
stormdata$BGN_DATE <- as.Date(stormdata$BGN_DATE)
range(stormdata$BGN_DATE)
Katrina <- filter(stormdata, REFNUM == "577615")
Katrina$REMARKS
Katrina$EVTYPE
# Number of fatalities = 1097. Let's update the row for Katarina to reflect this.
stormdata[stormdata$REFNUM == 577615,23]
Katrina <- stormdata[stormdata$REFNUM == 577615,]
Katrina$REMARKS
stormdata[stormdata$REFNUM == 577615,23] <- 1097
stormdata[stormdata$REFNUM == 577615,23]
max(stormdata$FATALITIES)
stormdata[stormdata$REFNUM == 577615,23] <- 0
stormdata
maxfatal <- stormdata[stormdata$FATALITIES == max(stormdata$FATALITIES), ]
glimpse(maxfatal[c(2,7,8,23)])
glimpse(maxfatal)
which(colnames(maxfatal) == "FATALITIES")
# Pick up from here with actual transformations.
# Take only observations where someone actually died or got hurt, or that caused economic damages
damage_events <- filter(stormdata, FATALITIES > 0 | INJURIES > 0
| PROPDMG > 0 | CROPDMG > 0)
human_events <- filter(stormdata, FATALITIES > 0 | INJURIES > 0)
property_events <- filter(stormdata, PROPDMG > 0 | CROPDMG > 0)
# Create the pie chart
pie(data, labels = c("Weather Events Which \nHurt or Killed a Human: \n 21930",
"Weather Events Which \nCaused Property Damage: \n 254633"),
main = "Comparison of Number of Weather Events \nWith Human vs Property Damage",
col = c("red2","green3"))
stormdata$CROPDMGEXP
cropdates <- range(stormdata[stormdata$CROPDMG >0, stormdata$BGN_DATE])
crops <- filter(stormdata, CROPDMG > 0)
range(crops$BGN_DATE)
# Might not use that chart. Anyway.
# Let's focus on which events hurt humans the most:
humanhealth <- summarise(FATALITIES = sum(human_events$FATALITIES), INJURIES = sum(human_events$INJURIES))
# Nah I don't like this code:
human_events %>%
summarise(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES))%>%
arrange(desc(FATALITIES + INJURIES))
# Oh wait you have to group_by first or it's stupid
human_events %>% group_by(EVTYPE) %>%
summarise(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES))%>%
arrange(desc(FATALITIES + INJURIES)) -> summed_human_events
length(unique(summed_human_events$EVTYPE))
length(unique(stormdata$EVTYPE))
# We're down to 220 EVTypes from 985, but lets reduce it to the 48 being used currently:aaaaaasdas
summed_human_events$EVTYPE_48 <- NULL
# Length 42. Much nicer. And now:
summed_human_events %>%
arrange(desc(FATALITIES + INJURIES)) -> summed_human_events
head(summed_human_events,10) -> human10
human10
ggplot(data = human10) +
geom_col(mapping = aes(x = fct_reorder(EVTYPE_48, FATALITIES, .desc = TRUE), y = INJURIES, fill = FATALITIES)) +
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))
# Best way to present this is with a stacked bar chart, with Injuries as the base bar because it's taller.
ggplot(data = human10) +
geom_col(mapping = aes(x = EVTYPE_48, y = INJURIES + FATALITIES)) +
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))
# Okay, and then we can move on to property damage.
# Not what I wanted, trying again:
ggplot(data = human10) +
geom_col(mapping = aes(x = fct_reorder(EVTYPE_MATCHED, FATALITIES + INJURIES, .desc = TRUE), y = FATALITIES, fill = "Fatalities")) +
geom_col(mapping = aes(x = fct_reorder(EVTYPE_MATCHED, FATALITIES + INJURIES, .desc = TRUE), y = INJURIES, fill = "Injuries", alpha = 0.5)) +
scale_fill_manual(values = c("Fatalities" = "red", "Injuries" = "yellow")) +
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))+
theme(axis.text.x = element_text(angle = 45, size = 6, margin = margin(10)))
# Actually, we can't group like this until I fix PROPDMG and CROPDMG
# wtf is this bing
human10_long <- human10 %>%
pivot_longer (cols = c (INJURIES, FATALITIES), names_to = "type", values_to = "value")
# Create stacked bar chart
ggplot (data = human10_long) +
geom_col (mapping = aes (x = EVTYPE_48, y = value, fill = type), stat = "identity") +
theme (plot.title = element_text (hjust = 0.5), plot.caption = element_text (hjust = 0.5))
# Damn it actually worked.
####
property_events <- filter(stormdata, PROPDMG > 0 | CROPDMG > 0)
cropsonly <- filter(property_events, CROPDMG > 0)
range(cropsonly$BGN_DATE)
# Huh, they only started tracking crop damage in 1993.
property_events <- filter(stormdata, PROPDMG > 0 | CROPDMG > 0)
property_events %>% group_by(EVTYPE)
property_events_backup <- property_events
property_events <- property_events[1:10000,]
property_events_subset_compare <- property_events
## To have a look at what type of exponents we are dealing with, look at: table(data_cd0$PROPDMGEXP) and table(data_cd0$CROPDMGEXP).
## It was seen at the time of this analysis that the only values were blanks, "K"'s, "M"'s, and "B"'s.
## The following are two for loops to find the total amount of property damage per event.
## ******* These for loops take about 3 minutes each, they should be optimized somehow ****************
for (i in 1:length(data_cd0$PROPDMG)) {
if (data_cd0$PROPDMGEXP[i] == "K") {
data_cd0$PROPDMG_TOTAL[i] <- data_cd0$PROPDMG[i]*10^3
}
else if (data_cd0$PROPDMGEXP[i] == "M") {
data_cd0$PROPDMG_TOTAL[i] <- data_cd0$PROPDMG[i]*10^6
}
else if (data_cd0$PROPDMGEXP[i] == "B") {
data_cd0$PROPDMG_TOTAL[i] <- data_cd0$PROPDMG[i]*10^9
}
else data_cd0$PROPDMG_TOTAL[i] <- 0
}
## For loop to find the total amount of crop damage per event.
property_eventstest <- property_events
property_events$CROPDMG_TOTAL <- list(0)
for (i in 1:length(property_events$CROPDMG)) {
if (property_events$CROPDMGEXP[i] == "K") {
property_events$CROPDMG_TOTAL[i] <- property_events$CROPDMG[i]*10^3
}
else if (property_events$CROPDMGEXP[i] == "M") {
property_events$CROPDMG_TOTAL[i] <- property_events$CROPDMG[i]*10^6
}
else if (property_events$CROPDMGEXP[i] == "B") {
property_events$CROPDMG_TOTAL[i] <- property_events$CROPDMG[i]*10^9
}
else property_events$CROPDMG_TOTAL[i] <- 0
}
# Same but for property damage
property_events$PROPDMG_TOTAL <- list(0)
# This is too slow because it is indexing the data frame inside the loop over and over
for (i in 1:length(property_events$PROPDMG)) {
if (property_events$PROPDMGEXP[i] == "K") {
property_events$PROPDMG_TOTAL[i] <- property_events$PROPDMG[i]*10^3
}
else if (property_events$PROPDMGEXP[i] == "M") {
property_events$PROPDMG_TOTAL[i] <- property_events$PROPDMG[i]*10^6
}
else if (property_events$PROPDMGEXP[i] == "B") {
property_events$PROPDMG_TOTAL[i] <- property_events$PROPDMG[i]*10^9
}
else property_events$PROPDMG_TOTAL[i] <- 0
}
# I think it worked but is everything 25 for some reason?
unique(stormdata$PROPDMG)
table(stormdata$PROPDMG)
# Let's vectorize the operation instead. We could use a nested chain of ifelse() commands,
# but dplyr's case_when is neater:
PROPDMG_TOTAL <- numeric()
PROPDMG_TOTAL <- case_when (
property_events$PROPDMGEXP == "K" ~ property_events$PROPDMG * 10^3,
property_events$PROPDMGEXP == "M" ~ property_events$PROPDMG * 10^6,
property_events$PROPDMGEXP == "B" ~ property_events$PROPDMG * 10^9,
TRUE ~ 0
)
## Now lets find the total monetary damage, giving equal weight to property and crop damage.
data_cd0$TOTALDMG <- data_cd0$CROPDMG_TOTAL + data_cd0$PROPDMG_TOTAL
PROPDMG_TOTAL
PROPDMG_TOTAL_IFELSE <- ifelse (property_events$PROPDMGEXP == "K", property_events$PROPDMG * 10^3,
ifelse (property_events$PROPDMGEXP == "M", property_events$PROPDMG * 10^6,
ifelse (property_events$PROPDMGEXP == "B", property_events$PROPDMG * 10^9, 0)))
# Both ran really fast. I just like the case when more, it makes more intuitive sense.
property_events$PROPDMG_TOTAL <- case_when (
property_events$PROPDMGEXP == "K" ~ property_events$PROPDMG * 10^3,
property_events$PROPDMGEXP == "M" ~ property_events$PROPDMG * 10^6,
property_events$PROPDMGEXP == "B" ~ property_events$PROPDMG * 10^9,
TRUE ~ 0
)
max(property_events$PROPDMG_TOTAL)
maxdamage <- property_events[property_events$PROPDMG_TOTAL == max(property_events$PROPDMG_TOTAL),]
maxdamage
#Same for CROPDMG
property_events$CROPDMG_TOTAL <- case_when (
property_events$CROPDMGEXP == "K" ~ property_events$CROPDMG * 10^3,
property_events$CROPDMGEXP == "M" ~ property_events$CROPDMG * 10^6,
property_events$CROPDMGEXP == "B" ~ property_events$CROPDMG * 10^9,
TRUE ~ 0
)
maxcrops <- property_events[property_events$CROPDMG_TOTAL == max(property_events$CROPDMG_TOTAL),]
maxcrops$REMARKS
maxcrops$CROPDMG_TOTAL
# Finally, create our sum:
property_events$TOTALDMG <- (property_events$PROPDMG_TOTAL + property_events$CROPDMG_TOTAL)
# And our summed totals by EVTYPE
property_events %>% group_by(EVTYPE) %>%
summarise(TotalDamage = sum(TOTALDMG)) %>%
arrange(desc(TotalDamage)) -> summed_property_events
length(unique(summed_property_events$EVTYPE))
# 431 event types
dictionary <- c("Astronomical Low Tide","Avalanche","Blizzard","Coastal Flood","Cold/Wind Chill","Debris Flow","Dense Fog","Dense Smoke","Drought","Dust Devil","Dust Storm","Excessive Heat","Extreme Cold/Wind Chill","Flash Flood","Flood","Freezing Fog","Frost/Freeze","Funnel Cloud","Hail","Heat","Heavy Rain","Heavy Snow","High Surf","High Wind","Hurricane","Typhoon","Hurricane/Typhoon","Ice Storm","Lakeshore Flood","Lake-Effect Snow","Lightning","Marine Hail","Marine High Wind","Marine Strong Wind","Marine Thunderstorm Wind","Rip Current","Sleet","Storm Tide","Strong Wind","Thunderstorm Wind","Tornado","Tropical Depression","Tropical Storm","Tsunami","Volcanic Ash","Waterspout","Wildfire","Winter Storm","Winter Weather")
dictionary_hurricaneonly <- c("Astronomical Low Tide","Avalanche","Blizzard","Coastal Flood","Cold/Wind Chill","Debris Flow","Dense Fog","Dense Smoke","Drought","Dust Devil","Dust Storm","Excessive Heat","Extreme Cold/Wind Chill","Flash Flood","Flood","Freezing Fog","Frost/Freeze","Funnel Cloud","Hail","Heat","Heavy Rain","Heavy Snow","High Surf","High Wind","Hurricane/Typhoon","Ice Storm","Lakeshore Flood","Lake-Effect Snow","Lightning","Marine Hail","Marine High Wind","Marine Strong Wind","Marine Thunderstorm Wind","Rip Current","Sleet","Storm Tide","Strong Wind","Thunderstorm Wind","Tornado","Tropical Depression","Tropical Storm","Tsunami","Volcanic Ash","Waterspout","Wildfire","Winter Storm","Winter Weather")
dictionary <- c("Astronomical Low Tide","Avalanche","Blizzard","Coastal Flood","Cold/Wind Chill","Debris Flow","Dense Fog","Dense Smoke","Drought","Dust Devil","Dust Storm","Excessive Heat","Extreme Cold/Wind Chill","Flash Flood","Flood","Freezing Fog","Frost/Freeze","Funnel Cloud","Hail","Heat","Heavy Rain","Heavy Snow","High Surf","High Wind","Hurricane","Typhoon","Hurricane/Typhoon","Ice Storm","Lakeshore Flood","Lake-Effect Snow","Lightning","Marine Hail","Marine High Wind","Marine Strong Wind","Marine Thunderstorm Wind","Rip Current","Sleet","Storm Tide","Strong Wind","Thunderstorm Wind","Tornado","Tropical Depression","Tropical Storm","Tsunami","Volcanic Ash","Waterspout","Wildfire","Winter Storm","Winter Weather")
dictionary
dictionary <- tolower(dictionary)
summed_property_events$EVTYPE <- tolower(summed_property_events$EVTYPE)
summed_property_events$EVTYPE_MATCHED <- dictionary[amatch(summed_property_events$EVTYPE,dictionary,method="lcs", maxDist=60)]
length(unique(summed_property_events$EVTYPE_MATCHED))
summed_property_events %>%
filter(EVTYPE_MATCHED == "marine high wind")
# Trying to fix the hurricane thing
dictionary_hurricaneonly <- c("Astronomical Low Tide","Avalanche","Blizzard","Coastal Flood","Cold/Wind Chill","Debris Flow","Dense Fog","Dense Smoke","Drought","Dust Devil","Dust Storm","Excessive Heat","Extreme Cold/Wind Chill","Flash Flood","Flood","Freezing Fog","Frost/Freeze","Funnel Cloud","Hail","Heat","Heavy Rain","Heavy Snow","High Surf","High Wind","Hurricane/Typhoon","Ice Storm","Lakeshore Flood","Lake-Effect Snow","Lightning","Marine Hail","Marine High Wind","Marine Strong Wind","Marine Thunderstorm Wind","Rip Current","Sleet","Storm Tide","Strong Wind","Thunderstorm Wind","Tornado","Tropical Depression","Tropical Storm","Tsunami","Volcanic Ash","Waterspout","Wildfire","Winter Storm","Winter Weather")
dictionary_hurricaneonly <- tolower(dictionary_hurricaneonly)
summed_property_events$EVTYPE <- tolower(summed_property_events$EVTYPE)
summed_property_events$EVTYPE_GROUPEDDIST <- dictionary_hurricaneonly[amatch(summed_property_events$EVTYPE,dictionary_hurricaneonly,method="lcs", maxDist=60)]
length(unique(summed_property_events$EVTYPE_GROUPEDDIST))
# Honestly seems pretty good
# and group again again:
summed_property_events %>% group_by(EVTYPE_GROUPEDDIST) %>%
summarise(TotalDamage = sum(TotalDamage)) %>%
arrange(desc(TotalDamage)) -> summed_property_events_refined
length(unique(summed_property_events_refined$EVTYPE_GROUPEDDIST))
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = market_segment, fill = customer_type)) +
facet_wrap(~hotel) +
theme(axis.text.x = element_text(angle = 45, size = 6, margin = margin(10))) +
scale_fill_manual(name = "Customer Type", values = wes_palette(4, name = "Zissou1", type = "continuous")) +
labs(title = "Customer Bookings by\n Hotel Type and Customer Type 2",
x = "Market Segment",
y = "Total Customer Bookings",
caption = paste0("Data from 2015 to 2017 / Legend Title Size 8, Text 6")) + # Let's center our title
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))
ggplot(data = summed_property_events_refined[1:10,]) +
geom_bar(mapping = aes(y = TotalDamage, x = EVTYPE_GROUPED)) +
theme(axis.text.x = element_text(angle = 45, size = 6, margin = margin(10))) +
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))
# I should just use col?
ggplot(data = neat10) +
geom_col(mapping = aes(y = TotalDamage, x = EVTYPE_GROUPEDDIST, fill = EVTYPE_GROUPEDDIST)) +
theme(axis.text.x = element_text(angle = 45, size = 6, margin = margin(10))) +
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))
neat10 <- arrange(summed_property_events_refined[1:10,], desc(TotalDamage))
library(forcats)
ggplot(data = neat10) +
geom_col(mapping = aes(x = fct_reorder(EVTYPE_GROUPEDDIST, TotalDamage, .desc = TRUE), y = TotalDamage, fill = EVTYPE_GROUPEDDIST)) +
theme(axis.text.x = element_text(angle = 45, size = 6, margin = margin(10))) +
theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0.5))
# I gotta fix this so hurricane/typhoon and hurricane aren't separate, it's bad
# Fixed it, I'm done, I'm the best
# Got my 3 figures too!