This document is made for the course project of Reproducible Research.In this project I will explore the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage. This poject will focus on the harm that different weather event done and try to answer two question. 1. Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health? 2. Across the United States, which types of events have the greatest economic consequences?
I will do some setup before I start
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(readr)
library(patchwork)
Read the data first
storm <- read_csv("repdata_data_StormData.csv.bz2")
## Rows: 902297 Columns: 37
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (18): BGN_DATE, BGN_TIME, TIME_ZONE, COUNTYNAME, STATE, EVTYPE, BGN_AZI,...
## dbl (18): STATE__, COUNTY, BGN_RANGE, COUNTY_END, END_RANGE, LENGTH, WIDTH, ...
## lgl (1): COUNTYENDN
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Take a look at the structure of the data first
str(storm)
## spec_tbl_df [902,297 × 37] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ STATE__ : num [1:902297] 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : chr [1:902297] "4/18/1950 0:00:00" "4/18/1950 0:00:00" "2/20/1951 0:00:00" "6/8/1951 0:00:00" ...
## $ BGN_TIME : chr [1:902297] "0130" "0145" "1600" "0900" ...
## $ TIME_ZONE : chr [1:902297] "CST" "CST" "CST" "CST" ...
## $ COUNTY : num [1:902297] 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: chr [1:902297] "MOBILE" "BALDWIN" "FAYETTE" "MADISON" ...
## $ STATE : chr [1:902297] "AL" "AL" "AL" "AL" ...
## $ EVTYPE : chr [1:902297] "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ BGN_RANGE : num [1:902297] 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : chr [1:902297] NA NA NA NA ...
## $ BGN_LOCATI: chr [1:902297] NA NA NA NA ...
## $ END_DATE : chr [1:902297] NA NA NA NA ...
## $ END_TIME : chr [1:902297] NA NA NA NA ...
## $ COUNTY_END: num [1:902297] 0 0 0 0 0 0 0 0 0 0 ...
## $ COUNTYENDN: logi [1:902297] NA NA NA NA NA NA ...
## $ END_RANGE : num [1:902297] 0 0 0 0 0 0 0 0 0 0 ...
## $ END_AZI : chr [1:902297] NA NA NA NA ...
## $ END_LOCATI: chr [1:902297] NA NA NA NA ...
## $ LENGTH : num [1:902297] 14 2 0.1 0 0 1.5 1.5 0 3.3 2.3 ...
## $ WIDTH : num [1:902297] 100 150 123 100 150 177 33 33 100 100 ...
## $ F : num [1:902297] 3 2 2 2 2 2 2 1 3 3 ...
## $ MAG : num [1:902297] 0 0 0 0 0 0 0 0 0 0 ...
## $ FATALITIES: num [1:902297] 0 0 0 0 0 0 0 0 1 0 ...
## $ INJURIES : num [1:902297] 15 0 2 2 2 6 1 0 14 0 ...
## $ PROPDMG : num [1:902297] 25 2.5 25 2.5 2.5 2.5 2.5 2.5 25 25 ...
## $ PROPDMGEXP: chr [1:902297] "K" "K" "K" "K" ...
## $ CROPDMG : num [1:902297] 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr [1:902297] NA NA NA NA ...
## $ WFO : chr [1:902297] NA NA NA NA ...
## $ STATEOFFIC: chr [1:902297] NA NA NA NA ...
## $ ZONENAMES : chr [1:902297] NA NA NA NA ...
## $ LATITUDE : num [1:902297] 3040 3042 3340 3458 3412 ...
## $ LONGITUDE : num [1:902297] 8812 8755 8742 8626 8642 ...
## $ LATITUDE_E: num [1:902297] 3051 0 0 0 0 ...
## $ LONGITUDE_: num [1:902297] 8806 0 0 0 0 ...
## $ REMARKS : chr [1:902297] NA NA NA NA ...
## $ REFNUM : num [1:902297] 1 2 3 4 5 6 7 8 9 10 ...
## - attr(*, "spec")=
## .. cols(
## .. STATE__ = col_double(),
## .. BGN_DATE = col_character(),
## .. BGN_TIME = col_character(),
## .. TIME_ZONE = col_character(),
## .. COUNTY = col_double(),
## .. COUNTYNAME = col_character(),
## .. STATE = col_character(),
## .. EVTYPE = col_character(),
## .. BGN_RANGE = col_double(),
## .. BGN_AZI = col_character(),
## .. BGN_LOCATI = col_character(),
## .. END_DATE = col_character(),
## .. END_TIME = col_character(),
## .. COUNTY_END = col_double(),
## .. COUNTYENDN = col_logical(),
## .. END_RANGE = col_double(),
## .. END_AZI = col_character(),
## .. END_LOCATI = col_character(),
## .. LENGTH = col_double(),
## .. WIDTH = col_double(),
## .. F = col_double(),
## .. MAG = col_double(),
## .. FATALITIES = col_double(),
## .. INJURIES = col_double(),
## .. PROPDMG = col_double(),
## .. PROPDMGEXP = col_character(),
## .. CROPDMG = col_double(),
## .. CROPDMGEXP = col_character(),
## .. WFO = col_character(),
## .. STATEOFFIC = col_character(),
## .. ZONENAMES = col_character(),
## .. LATITUDE = col_double(),
## .. LONGITUDE = col_double(),
## .. LATITUDE_E = col_double(),
## .. LONGITUDE_ = col_double(),
## .. REMARKS = col_character(),
## .. REFNUM = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
Subset the data we interested
storm1 <- select(storm, EVTYPE, FATALITIES, INJURIES, PROPDMG,
PROPDMGEXP, CROPDMG, CROPDMGEXP)
Turn PROPDMGEXP and CROPDMGEXP into
numbers
- B:1000000000
- M:1000000
- K:1000
- H:100
- ?: NA
- Numbers remain numbers
- others view it as dollars
storm1$PROPDMGEXP[storm1$PROPDMGEXP == "?"] <- NA
storm1$PROPDMGEXP <- str_replace(storm1$PROPDMGEXP, "K", "3") |>
str_replace(regex("M", ignore_case = TRUE), "6") |>
str_replace("B", "9") |>
str_replace(regex("H", ignore_case = TRUE), "2") |>
str_replace_all(c("\\+" = "1", "\\-" = "1", "0" = "1")) |>
as.numeric()
storm1$property_dmg <- storm1$PROPDMG * 10 ^ storm1$PROPDMGEXP
storm1$CROPDMGEXP[storm1$CROPDMGEXP == "?"] <- NA
storm1$CROPDMGEXP <- str_replace(storm1$CROPDMGEXP, "K", "3") |>
str_replace(regex("M", ignore_case = TRUE), "6") |>
str_replace("B", "9") |>
str_replace(regex("H", ignore_case = TRUE), "2") |>
str_replace_all(c("\\+" = "1", "\\-" = "1", "0" = "1")) |>
as.numeric()
## Warning: 強制變更過程中產生了 NA
storm1$crop_dmg <- storm1$CROPDMG * 10 ^ storm1$CROPDMGEXP
See what are the types of different event
head(sort(table(storm1$EVTYPE), decreasing = TRUE), n = 20)
##
## HAIL TSTM WIND THUNDERSTORM WIND
## 288661 219944 82563
## TORNADO FLASH FLOOD FLOOD
## 60652 54278 25326
## THUNDERSTORM WINDS HIGH WIND LIGHTNING
## 20843 20212 15755
## HEAVY SNOW HEAVY RAIN WINTER STORM
## 15708 11723 11433
## WINTER WEATHER FUNNEL CLOUD MARINE TSTM WIND
## 7026 6839 6175
## MARINE THUNDERSTORM WIND WATERSPOUT STRONG WIND
## 5812 3797 3566
## URBAN/SML STREAM FLD WILDFIRE
## 3392 2761
from the table we can see it is classified too detailed
so I will regrouped them with their common pattern
tornado <- filter(storm1, str_detect(storm1$EVTYPE, "TORNADO"))
wind <- filter(storm1, str_detect(storm1$EVTYPE, "WIND"))
rain <- filter(storm1, str_detect(storm1$EVTYPE, "RAIN"))
hail <- filter(storm1, str_detect(storm1$EVTYPE, "HAIL"))
snow <- filter(storm1, str_detect(storm1$EVTYPE, "SNOW"))
ice <- filter(storm1, str_detect(storm1$EVTYPE, "ICE"))
flood <- filter(storm1, str_detect(storm1$EVTYPE, "FLOOD"))
heat <- filter(storm1, str_detect(storm1$EVTYPE, "HEAT"))
tsunami <- filter(storm1, str_detect(storm1$EVTYPE, "TSUNAMI"))
typhoon <- filter(storm1, str_detect(storm1$EVTYPE, "TYPHOON"))
Than put them back together
storm_regroup <-
data.frame(
"tornado" = apply(tornado[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"wind" = apply(wind[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"rain" = apply(rain[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"hail" = apply(hail[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"snow" = apply(snow[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"ice" = apply(ice[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"flood" = apply(flood[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"heat" = apply(heat[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"tsunami" = apply(tsunami[, c(2:3, 8:9)], 2, sum, na.rm = TRUE),
"typhoon" = apply(typhoon[, c(2:3, 8:9)], 2, sum, na.rm = TRUE)) |>
t() |>
as.data.frame()
storm_regroup$event_type <- row.names(storm_regroup)
names(storm_regroup) <- str_to_lower(names(storm_regroup))
Now we can start analyzing with storm_regroup
Before we continue I’ll like to see how many observations are dropped
mystring <- c("TORNADO", "WIND", "RAIN", "HAIL", "SNOW", "ICE",
"FLOOD", "HEAT", "TSUNAMI", "TYPHOON")
sum(!str_detect(storm1$EVTYPE, paste(mystring, collapse = "|"))) /
nrow(storm1)
## [1] 0.07787236
8% is an acceptable number for me
Now let’s take a look at each value of different event
plot_theme1 <- ggplot(storm_regroup, aes(fill = event_type)) +
scale_x_discrete(guide = guide_axis(angle = 45)) +
xlab("event types") +
theme(legend.position = "none",
plot.title = element_text(size = 8))
p1 <- plot_theme1 +
geom_col(aes(x = reorder(event_type, fatalities),
y = fatalities)) +
labs(title = "Total number of fatalities")
p2 <- plot_theme1 +
geom_col(aes(x = reorder(event_type, injuries),
y = injuries)) +
labs(title = "Total number of injuries")
p3 <- plot_theme1 +
geom_col(aes(x = reorder(event_type, property_dmg),
y = property_dmg)) +
labs(title = "Total properties damage in dollars")
p4 <- plot_theme1 +
geom_col(aes(x = reorder(event_type, crop_dmg),
y = crop_dmg)) +
labs(title = "Total crops damage in dollars")
(p1 | p2) / (p3 | p4)
From the plot we can see tornado and flood cause much more harm than other events respectively on population health and economic , however we can’t tell whether these event cause more damage or they happened more often, so I decided to make another df with mean value
storm_mean <-
data.frame(
"tornado" = apply(tornado[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"wind" = apply(wind[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"rain" = apply(rain[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"hail" = apply(hail[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"snow" = apply(snow[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"ice" = apply(ice[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"flood" = apply(flood[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"heat" = apply(heat[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"tsunami" = apply(tsunami[, c(2:3, 8:9)], 2, mean, na.rm = TRUE),
"typhoon" = apply(typhoon[, c(2:3, 8:9)], 2,
mean, na.rm = TRUE)) |>
t() |>
as.data.frame()
storm_mean$event_type <- row.names(storm_mean)
names(storm_mean) <- str_to_lower(names(storm_mean))
I make the plot again with both total value and mean value
plot_theme2 <- ggplot(storm_mean, aes(fill = event_type)) +
scale_x_discrete(guide = guide_axis(angle = 45)) +
xlab("event types") +
theme(legend.position = "none",
plot.title = element_text(size = 8))
p5 <- plot_theme2 +
geom_col(aes(x = reorder(event_type, fatalities),
y = fatalities)) +
labs(title = "Average number of fatalities")
p6 <- plot_theme2 +
geom_col(aes(x = reorder(event_type, injuries),
y = injuries)) +
labs(title = "Average number of injuries")
p7 <- plot_theme2 +
geom_col(aes(x = reorder(event_type, property_dmg),
y = property_dmg)) +
labs(title = "Average properties damage in dollars")
p8 <- plot_theme2 +
geom_col(aes(x = reorder(event_type, crop_dmg),
y = crop_dmg)) +
labs(title = "Average crops damage in dollars")
(p1 | p2) / (p3 | p4)
(p5 | p6) / (p7 | p8)
Also make a table of the rate of observation of each event
data.frame("tornado" = nrow(tornado),
"wind" = nrow(wind),
"rain" = nrow(rain),
"hail" = nrow(hail),
"snow" = nrow(snow),
"ice" = nrow(ice),
"flood" = nrow(flood),
"heat" = nrow(heat),
"tsunami" = nrow(tsunami),
"typhoon" = nrow(typhoon)) |>
sort()
## Warning in xtfrm.data.frame(x): cannot xtfrm data frames
## tsunami typhoon ice heat rain snow tornado flood hail wind
## 1 20 99 2185 2645 12202 17617 60700 82665 290394 364847
Now we can answer the questions
Ans1. Tornado is the most harmful event to population health because it happened quite often and it also cause severe damage every time it happened. Tsunami and typhoon are also dangerous but they occur way fewer than tornado.
Ans2. Flood have the greatest economic consequences. It does severe damage to both crops and properties.It is noteworthy that though typhoon make less total damage than flood, it makes huge impact on economic consequences every time it happened.