library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(knitr)
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
knitr::opts_chunk$set(echo = TRUE)
The data analyzed in this document compiles observations on multiple weather events and multiple features of these events including location, duration, etc. This analysis is centered on the events type, their impact on citizens life( injuries and deaths, hereby referred as “Health”) and the economic damage caused between 1990 and 2011. This document will recapitulate the data analysis and present the main results.
The following section describes in detail how the data was processed.
#download file
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2", "repdata_data_StormData.csv.bz2", mode = "wb")
# Unzip the file
unzip("repdata_data_StormData.csv.bz2")
#read the file
StormData <- read_csv("repdata_data_StormData.csv")
## 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.
The first manipulation of the dataset was the fusion of the variables related health (fatalities and injuries) into a single variable named “Health”, and the fusion of variables related to economic damages (property and crops) into a single variable named “Ec”. I also made sure to transform the date to POSIXlt format and make a column with only the year in it to simplify analysis.
#create new columns to add the variables related to human health and to economic impact
StormData$Health <- StormData$FATALITIES+StormData$INJURIES
StormData$Ec <- StormData$PROPDMG+StormData$CROPDMG
#create new columns to keep track of the dates
StormData$DATE <- as.POSIXlt(StormData$BGN_DATE, format = "%m/%d/%Y %H:%M:%S")
StormData$Year <- as.numeric(format(StormData$DATE, "%Y"))
After these manipulations, I summarized the data by year and event, keeping the total sum of damages caused to represent the damages to the population’s health and to the economy. Observations outside of the time-frame and observations without any kind of damage were excluded from the database to simplify analysis.
#Create new dataframe to summarise the information by year and event
df <- StormData %>%
group_by(Year, EVTYPE) %>%
summarise(
total_fatalities = sum(FATALITIES),
total_Injuries = sum(INJURIES),
Health = sum(Health),
total_Prop = sum(PROPDMG),
total_Crop = sum(CROPDMG),
Ec = sum(Ec))
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
#get rid of observations without any effect on health and the economy, limit observations to after 1990
df <- subset(df, df$Health>0 & df$Ec>0 & df$Year>1990)
Given that the original database contained almost a thousand unique event types, making analysis almost impossible, I created a second “event” variable to limit the number of categories. In this second variable, I grouped events with similar fundamental causes into a single category. For example, hail, rain and snow events were all encompassed by the category “precipitations”. In total, 10 categories were kept: Cold/Winter, Heat, Flood, Hurricane, Tornado, Precipitations, Storm, Wind, Wild Fires and Other, each created according to the criteria shown in the next chunk of code.
#Simplify the event types
df <- df %>% mutate(Event = case_when(
grepl("TORNADO", EVTYPE, ignore.case = TRUE) ~ "TORNADO",
grepl("HURRICANE", EVTYPE, ignore.case = TRUE) ~ "HURRICANE",
grepl("HEAT", EVTYPE, ignore.case = TRUE) ~ "HEAT",
grepl("WIND", EVTYPE, ignore.case = TRUE) ~ "WIND",
grepl("FLOOD", EVTYPE, ignore.case = TRUE) ~ "FLOOD",
str_detect(EVTYPE, regex("STORM|LIGHTNING", ignore_case = TRUE)) ~ "STORM",
str_detect(EVTYPE, regex("COLD|FREEZING|FREEZE|ICE|ICY|FROST|WINTER", ignore_case = TRUE)) ~ "COLD/WINTER",
str_detect(EVTYPE, regex("HAIL|RAIN|SNOW|THUNDERSNOW|BLIZZARD", ignore_case = TRUE)) ~ "PRECIPITATIONS",
str_detect(EVTYPE, regex("FIRE", ignore_case = TRUE)) ~ "WILD FIRES",
TRUE ~ "OTHER" # Default case if none of the conditions match
))
Further, I created another dataframe with the informations grouped by event in order to see the aggregated damages over the entire period.
Having finished the data processing, my next step was to visualize the data using ggplot to illustrate the damages caused throughout the years by event type. One visualization was made for health-related damage and another for economic damage.
# Create a ggplot for overall health damage
ggplot(df, aes(x = Year, y = Health, color = Event)) +
geom_line() +
labs(title = "Injuries and Deaths Over the Years",
x = "Year",
y = "Number of Injuries and Deaths") +
facet_wrap(~ Event, scales = "free_y", ncol = 2) +
theme_minimal()
# Create a ggplot for overall economic damage
ggplot(df, aes(x = Year, y = Ec, color = Event)) +
geom_line() +
labs(title = "Economic Impact Over the Years",
x = "Year",
y = "Economic Impact (in") +
facet_wrap(~ Event, scales = "free_y", ncol = 2) +
theme_minimal()
The analysis shows that punctual events such as floods and tornadoes have historically caused the greater damage to health with upwards of 6 thousands victims in a single year Economic damage is headed by the event type “wind”, followed by floods and tornadoes with maximums of 200 000 millions in costs in a single year
However, these extreme historical events do not seem to represent the overall damages caused in this period. While having caused little damage in a single year, storms caused more than 10 thousand victims in aggregate, a number close to the one produced by floods. Similarly, while tornadoes caused the most casualties in a single year, in total heat caused more deaths over this period. The following table summarizes the damages by event.
| Event | Fatalities | Annual_Fatalities | Injuries | Annual_Injuries | Total_Health | Annual_Health | Property_Damage | Annual_Property_Damage | Crop_Damage | Annual_Crop_Damage | Total_Economic_Damage | Annual_Economic_Damage |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TORNADO | 1727 | 82 | 25558 | 1217 | 27285 | 1299 | 1520700 | 72414 | 100021 | 4763 | 1620721 | 77177 |
| STORM | 1232 | 59 | 9429 | 449 | 10661 | 508 | 859680 | 40937 | 15616 | 744 | 875295 | 41681 |
| FLOOD | 1523 | 73 | 8601 | 410 | 10124 | 482 | 2393708 | 113986 | 361470 | 17213 | 2755178 | 131199 |
| WIND | 1065 | 51 | 8159 | 389 | 9224 | 439 | 3126404 | 148876 | 222922 | 10615 | 3349327 | 159492 |
| HEAT | 2244 | 107 | 5716 | 272 | 7960 | 379 | 3233 | 154 | 1473 | 70 | 4706 | 224 |
| PRECIPITATIONS | 345 | 16 | 3112 | 148 | 3457 | 165 | 885041 | 42145 | 594573 | 28313 | 1479614 | 70458 |
| OTHER | 694 | 33 | 2214 | 105 | 2908 | 138 | 73619 | 3506 | 12483 | 594 | 86103 | 4100 |
| WILD FIRES | 90 | 4 | 1606 | 76 | 1696 | 81 | 122912 | 5853 | 8549 | 407 | 131462 | 6260 |
| HURRICANE | 135 | 6 | 1326 | 63 | 1461 | 70 | 20936 | 997 | 9783 | 466 | 30719 | 1463 |
| COLD/WINTER | 252 | 12 | 882 | 42 | 1134 | 54 | 36720 | 1749 | 6206 | 296 | 42926 | 2044 |