-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.Rmd
277 lines (209 loc) · 7.01 KB
/
report.Rmd
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
---
title: "Weather Damage Report"
author: "Victor Wildner"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
```
> The basic goal of this project is to explore the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database, seeking to answer some questions about severe weather events. The main focus of this study is to enable full-research reproducibility, using literate programming for every step.
## Data Processing
### Setup
#### Loading libraries
```{r libs, echo=FALSE, results=FALSE}
library(tidyverse)
library(cowplot)
```
#### Setting seed for reproducibility
```{r seed}
set.seed(144)
```
#### Showing current environment
```{r sessioninfo}
sessionInfo()
```
#### Downloading the dataset
```{r download, cache = TRUE}
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2", "stormdata.csv.bz2")
```
#### Read dataset into variable removing blank spaces and transform into tidyverse compatible
```{r load_data, cache = TRUE}
storm_data <- as_tibble(read.csv(file = "stormdata.csv.bz2", na.strings = ""))
```
#### Get a glimpse of the data
```{r glimpse}
glimpse(storm_data)
```
#### Count NAs
```{r countnas}
sum_na <- storm_data %>%
summarize_all(funs(sum(is.na(.))))
glimpse(sum_na)
```
#### Select only needed data from the processed dataset
```{r filterdata}
filter_data <- storm_data %>%
select(STATE, EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP)
glimpse(filter_data)
```
#### Impute 0 to NAs
```{r tidyone}
tidy_storm <- filter_data %>%
replace(is.na(.), "0"
) %>%
mutate(PROPDMGEXP = as.character(PROPDMGEXP),
CROPDMGEXP = as.character(CROPDMGEXP))
glimpse(tidy_storm)
```
#### Find unique values in prop and crop
```{r findunique}
unique_prop <- unique(tidy_storm$PROPDMGEXP)
unique_crop <- unique(tidy_storm$CROPDMGEXP)
unique_prop
unique_crop
```
#### Create dictionary for the unique values
```{r dictionary}
rep_prop <- c(1e3, 1e6, 0, 1e9, 1e6, 0, 1e5, 1e6, 0, 1e4, 1e2, 1e3, 1e2, 1e7, 1e2, 0, 1e1, 1e8)
rep_crop <- c(0, 1e6, 1e3, 1e6, 1e9, 0, 1e3, 1e2)
prop_dictionary <- cbind(unique_prop, rep_prop)
crop_dictionary <- cbind(unique_crop, rep_crop)
prop_dictionary
crop_dictionary
```
#### Remap values back into the tidy data set using plyr
```{r tidy}
tidy_storm$PROPDMGEXP <- plyr::mapvalues(tidy_storm$PROPDMGEXP, from = unique_prop, to = rep_prop)
tidy_storm$CROPDMGEXP <- plyr::mapvalues(tidy_storm$CROPDMGEXP, from = unique_crop, to = rep_crop)
tidy_storm <- tidy_storm %>%
mutate(PROPDMGEXP = as.double(PROPDMGEXP),
CROPDMGEXP = as.double(CROPDMGEXP))
glimpse(tidy_storm)
```
## Questions
### 1. Across the United States, which types of events (as indicated in the EVTYPE variable are most harmful with respect to population health?
#### Rank fatalities and injuries according to EVTYPE
```{r nowquestion}
sum_events_global <- tidy_storm %>%
select(-STATE) %>%
group_by(EVTYPE) %>%
summarize_all(
list(sum)
)
sum_fatalities_global <- sum_events_global %>%
select(EVTYPE, FATALITIES) %>%
arrange(desc(FATALITIES))
sum_fatalities_top10 <- slice(sum_fatalities_global, 1:10)
sum_injuries_global <- sum_events_global %>%
select(EVTYPE, INJURIES) %>%
arrange(desc(INJURIES))
sum_injuries_top10 <- slice(sum_injuries_global, 1:10)
```
#### Plot results
```{r plot1, warning=FALSE}
plot1 <- ggplot(sum_fatalities_top10, aes(reorder(EVTYPE, -FATALITIES), FATALITIES, group = EVTYPE, alpha = EVTYPE)) +
geom_bar(stat = "identity") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90),
legend.position = "none"
) +
labs(
title = "Major Causes of Fatality",
x = "",
y = "Fatalities"
)
plot2 <- ggplot(sum_injuries_top10, aes(reorder(EVTYPE, -INJURIES), INJURIES, group = EVTYPE, alpha = EVTYPE, fill = "red")) +
geom_bar(stat = "identity") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90),
legend.position = "none"
) +
labs(
title = "Major Causes of Injuries",
x = "",
y = "Injuries"
)
plot_grid(plot1, plot2)
```
### 2. Across the United States, which types of events have the greatest economic consequences?
### Prepare vars for measured and expected damage
```{r measurednexpected}
# Measured damage
propdmg_global <- sum_events_global %>%
select(EVTYPE, PROPDMG) %>%
arrange(desc(PROPDMG))
propdmg_top10 <- slice(propdmg_global, 1:10)
cropdmg_global <- sum_events_global %>%
select(EVTYPE, CROPDMG) %>%
arrange(desc(CROPDMG))
cropdmg_top10 <- slice(cropdmg_global, 1:10)
# Expected damage
exp_propdmg_global <- sum_events_global %>%
select(EVTYPE, PROPDMGEXP) %>%
arrange(desc(PROPDMGEXP))
exp_propdmg_top10 <- slice(exp_propdmg_global, 1:10)
exp_cropdmg_global <- sum_events_global %>%
select(EVTYPE, CROPDMGEXP) %>%
arrange(desc(CROPDMGEXP))
exp_cropdmg_top10 <- slice(exp_cropdmg_global, 1:10)
```
#### Plot the results
```{r plot2, warning=FALSE}
plot3 <- ggplot(propdmg_top10, aes(reorder(EVTYPE, -PROPDMG), PROPDMG, group = EVTYPE, alpha = EVTYPE)) +
geom_bar(stat = "identity", fill = "rosybrown") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90),
legend.position = "none"
) +
labs(
title = "Major Causes of Prop Damage",
x = "",
y = "Gross cost ($)"
)
plot4 <- ggplot(cropdmg_top10, aes(reorder(EVTYPE, -CROPDMG), CROPDMG, group = EVTYPE, alpha = EVTYPE)) +
geom_bar(stat = "identity", fill = "navyblue") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90),
legend.position = "none"
) +
labs(
title = "Major Causes of Crop Damage",
x = "",
y = "Gross cost ($)"
)
plot_grid(plot3, plot4)
```
```{r plot3, warning=FALSE}
plot5 <- ggplot(exp_propdmg_top10, aes(reorder(EVTYPE, -PROPDMGEXP), PROPDMGEXP, group = EVTYPE, alpha = EVTYPE)) +
geom_bar(stat = "identity", fill = "firebrick") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90),
legend.position = "none"
) +
labs(
title = "Expected Causes of Prop Damage",
x = "",
y = "Gross cost ($)"
)
plot6 <- ggplot(exp_cropdmg_top10, aes(reorder(EVTYPE, -CROPDMGEXP), CROPDMGEXP, group = EVTYPE, alpha = EVTYPE)) +
geom_bar(stat = "identity", fill = "slateblue3") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90),
legend.position = "none"
) +
labs(
title = "Expected Causes of Crop Damage",
x = "",
y = "Gross cost ($)"
)
plot_grid(plot5, plot6, align = "hv")
```
## Results
The extreme events that took most lives and caused most injuries in the United States are tornado, heat, and flash flood with tornado being about 2/3 of the total reported cases.
The events that had the most economic damage in the US are hurricanes, tornadoes, flood and drought.