In the following analysis we want to find out which event type caused the the most harm to population health or had the greatest economic consequences in the US since 1950. First we will look at the code I used for my analysis and then display the results. We will see that tornadoes cause the most harm to population health with 91.346 caused injuries and 5.633 fatalities. Tornadoes also caused the most economical damage with approx. $3.2 Million in property damage.
We will use the following packages in the analysis:
library(dplyr)
library(ggplot2)
library(gridExtra)
Now we load in the data that we want to analyse to get an answer to our problem.
# specify url to csv containing the data
url_data <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
# download the data and save it as "storm_data.csv"
download.file( url_data, "Data/storm_data.csv")
# load "storm_data.csv"
storm_data <- read.csv("Data/storm_data.csv")
The data consists of 37 variables and 902297 rows. We will try to answer the problem of our analysis in 2 steps. First we will determine which event types caused the most harm to population health and then we want to find out which event types had the greatest economical consequences. For the first only 3 columns in our data “storm_data” are relevant. Those are “EVTYPE”, “FATALITIES” and “INJURIES”. We calculate the sums of “FATALITIES” and “INJURIES” by “EVTYPE”.
# create new table that contains the sums of fatalities and injuries caused by
# the respective event type over the recorded years
sums_by_evtype <- storm_data %>%
select(EVTYPE, FATALITIES, INJURIES) %>%
group_by(EVTYPE) %>%
summarise(
sum_Fatalities = sum(FATALITIES, na.rm = TRUE),
sum_Injuries = sum(INJURIES, na.rm = TRUE)
)
head(sums_by_evtype)
## # A tibble: 6 x 3
## EVTYPE sum_Fatalities sum_Injuries
## <chr> <dbl> <dbl>
## 1 " HIGH SURF ADVISORY" 0 0
## 2 " COASTAL FLOOD" 0 0
## 3 " FLASH FLOOD" 0 0
## 4 " LIGHTNING" 0 0
## 5 " TSTM WIND" 0 0
## 6 " TSTM WIND (G45)" 0 0
You can clearly see that there are event types that didn’t cause any recorded harm to population health. As these type of events aren’t of any interest for our analysis, we will get rid of them.
# delete event types that caused no fatalities or injuries
sums_by_evtype <- sums_by_evtype[sums_by_evtype[,2] + sums_by_evtype[,3] != 0, ]
head(sums_by_evtype)
## # A tibble: 6 x 3
## EVTYPE sum_Fatalities sum_Injuries
## <chr> <dbl> <dbl>
## 1 AVALANCE 1 0
## 2 AVALANCHE 224 170
## 3 BLACK ICE 1 24
## 4 BLIZZARD 101 805
## 5 blowing snow 1 1
## 6 BLOWING SNOW 1 13
Now there are are only 220 event types left. To determine which of these event types caused the most harm, we will create a new column “total_impact” that contains the sums of the fatalities and injuries caused by the respective event type. We will then sort the data by “total_impact” in descending Order to get the highest impact values at the top of our dataframe. For this analysis we will only look at the top 5 most harmful event types for population health.
# create new column that displays the sum of fatalities and injuries
sums_by_evtype$total_impact <- (sums_by_evtype[,2] + sums_by_evtype[,3])
# sort events by total_impact in descending order to get highest impact values
# at the top
sums_by_evtype <- arrange(sums_by_evtype, desc(total_impact))
# extract the top 5 most harmful events to population health
top5_most_harmful <- sums_by_evtype[c(1:5),]
head(top5_most_harmful)
## # A tibble: 5 x 4
## EVTYPE sum_Fatalities sum_Injuries total_impact$sum_Fatalities
## <chr> <dbl> <dbl> <dbl>
## 1 TORNADO 5633 91346 96979
## 2 EXCESSIVE HEAT 1903 6525 8428
## 3 TSTM WIND 504 6957 7461
## 4 FLOOD 470 6789 7259
## 5 LIGHTNING 816 5230 6046
So these are the top 5 most harmful event types for population health since 1950. Now we will try to find out which event types had the biggest economical consequences. For this step we will again only need 3 columns of the original dataset “storm_data”. These are “EVTYPE”, “PROPDMG” and “PROPDMGEXP”. We get rid of all events that didn’t cause any damage.
# delete events that caused no property damages and only keep the columns
# "EVTYPE", "PROPDMG" and "PROPDMGEXP"
propdmg <- storm_data[storm_data$PROPDMG != 0, c(8, 25, 26)]
head(propdmg)
## EVTYPE PROPDMG PROPDMGEXP
## 1 TORNADO 25.0 K
## 2 TORNADO 2.5 K
## 3 TORNADO 25.0 K
## 4 TORNADO 2.5 K
## 5 TORNADO 2.5 K
## 6 TORNADO 2.5 K
The column “PROPDMGEXP” has values like K, M, B etc. that should classify the magnitude of the value in “PROPDMG”. We get rid of any events where “PROPDMGEXP” isn’t classified by K, M and B, as we only know the meaning of these variables.
# delete events where propdmgexp isn't characterized by K, M or B as we only
# know the meanings of these variables
propdmg <- propdmg[propdmg$PROPDMGEXP %in% c("K", "M", "B"),]
Now we will replace the variables in “PROPDMGEXP” with the according numerical value, with K = 1.000, M = 1.000.000 and B = 1.000.000.000.
# replace magnitudes in propdmgexp with K = 1.000, M = 1.000.000 and
# B = 1.000.000.000
propdmg <- propdmg %>%
mutate(PROPDMGEXP = case_when(
PROPDMGEXP == "K" ~ 1000,
PROPDMGEXP == "M" ~ 1000000,
PROPDMGEXP == "B" ~ 1000000000
))
We multiply the values in “PROPDMG” with the respective magnitude in “PROPDMGEXP” to get the correct damage caused by the event.
# multiply propdmg by magnitude in propdmgexp
propdmg$PROPDMG <- propdmg$PROPDMG * propdmg$PROPDMGEXP
head(propdmg)
## EVTYPE PROPDMG PROPDMGEXP
## 1 TORNADO 25000 1000
## 2 TORNADO 2500 1000
## 3 TORNADO 25000 1000
## 4 TORNADO 2500 1000
## 5 TORNADO 2500 1000
## 6 TORNADO 2500 1000
Everything is set to now calculate the sums of damages by event types.
# create new table that contains the sum of property damage caused by
# the respective event type over the recorded years
propdmg_by_evtype <- storm_data %>%
select(EVTYPE, PROPDMG) %>%
group_by(EVTYPE) %>%
summarise(
sum_propdmg = sum(PROPDMG, na.rm = TRUE)
)
head(propdmg_by_evtype)
## # A tibble: 6 x 2
## EVTYPE sum_propdmg
## <chr> <dbl>
## 1 " HIGH SURF ADVISORY" 200
## 2 " COASTAL FLOOD" 0
## 3 " FLASH FLOOD" 50
## 4 " LIGHTNING" 0
## 5 " TSTM WIND" 108
## 6 " TSTM WIND (G45)" 8
We will now determine the top 5 event types that caused the most economical damage.
# sort events by propdmg in descending order to get highest impact values
# at the top
propdmg_by_evtype <- arrange(propdmg_by_evtype, desc(sum_propdmg))
top5_most_damage <- propdmg_by_evtype[c(1:5), ]
head(top5_most_damage)
## # A tibble: 5 x 2
## EVTYPE sum_propdmg
## <chr> <dbl>
## 1 TORNADO 3212258.
## 2 FLASH FLOOD 1420125.
## 3 TSTM WIND 1335966.
## 4 FLOOD 899938.
## 5 THUNDERSTORM WIND 876844.
So these are the top 5 event types that caused the most economical damage in the US since 1950.
Here is a short summary of our analysis. The top 5 most harmful event types in the US since 1950 are showcased in the following plot:
# create bar graph displaying sum of injuries caused by event type
plot_injuries <- ggplot(top5_most_harmful, aes(x = EVTYPE, y = sum_Injuries)) +
geom_bar(stat = "identity", fill = "steelblue") +
geom_text(aes(label = sum_Injuries), vjust = -0.3, color = "black") +
labs(x = "Event Type", y = "Sum of Injuries") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none"
) +
ggtitle("Sum of injuries caused by top 5
most harmful event types since 1950")
# create bar graph displaying sum of fatalities caused by event type
plot_fatalities <- ggplot(top5_most_harmful, aes(x = EVTYPE, y = sum_Fatalities)) +
geom_bar(stat = "identity", fill = "steelblue") +
geom_text(aes(label = sum_Fatalities), vjust = -0.3, color = "black") +
labs(x = "Event Type", y = "Sum of Fatalities") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none"
) +
ggtitle("Sum of fatalities caused by top 5
most harmful event types since 1950")
# display both bar graphs on one plot
grid.arrange(plot_injuries, plot_fatalities, ncol = 2)
The top 5 event types that caused the most economical damage in the US since 1950 are showcased in the following plot:
ggplot(top5_most_damage, aes(x = EVTYPE, y = sum_propdmg)) +
geom_bar(stat = "identity", fill = "steelblue") +
geom_text(aes(label = paste (sum_propdmg, "$")), vjust = -0.3, color = "black") +
labs(x = "Event Type", y = "Property Damage (in $)") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none"
) +
ggtitle("Property damage caused by top 5 event types
that caused the most economical damage since 1950")
We can cleary see that tornadoes by far caused the most harm to population health with 91.346 caused injuries and 5.633 fatalities. Tornadoes also by far caused the most economical damage with approx. $3.2 Million in property damage.