Introduction

Storms and various severe weather occurrences pose significant challenges to public health and the economy in both communities and municipalities. These events often lead to loss of lives, bodily harm, and destruction of property, making their prevention and mitigation a crucial focus.

The aim of this project is to investigate the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This comprehensive database records the details of significant storms and meteorological events across the United States, encompassing their timing, locations, and any associated human casualties, injuries, or property damages.

The fundamental objective of this task is to delve into the NOAA Storm Database and address several key inquiries regarding severe weather incidents. We will utilize this database as the basis for answering the following questions, and you need to present the entire code used in your analysis.

Questions to be answered:

  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?

Setting Global Options & Setup

First, we set the global options, then load the necessary libraries and set the working directory.

library(dplyr)
library(data.table)
library(ggplot2)
#set the working directory
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

Loading and preprocessing the data

We download the storm data if it’s not already available and read it into R.

filename <- 'StormData.csv.bz2'
if (!file.exists(filename)) {
    download.file('https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2', filename)
}

Storm_Data <- if (!exists("Storm_Data")) read.csv(filename) else Storm_Data

Data Preparation

The data is converted to a data.table for efficient processing. We then subset the data and convert certain columns into a usable format.

# Convert data.frame to data.table
storm_data <- setDT(Storm_Data)

# Data Subsetting
cols_interest <- c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")
storm_data <- storm_data[, ..cols_interest, with = FALSE]
storm_data <- storm_data[EVTYPE != "?" & (INJURIES > 0 | FATALITIES > 0 | PROPDMG > 0 | CROPDMG > 0)]

Data Transformation

The exponent columns are transformed into numeric values to calculate the economic cost effectively.

# Converting Exponent Columns into Actual Exponents
conv_exp <- function(key, col) {
    key[as.character(storm_data[[col]])]
}

# Define keys for mapping
prop_dmg_key <- setNames(c(1, 1, 1, 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 100, 1e3, 1e6, 1e9), 
                       c("\"\"", "-", "+", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "H", "K", "M", "B"))

crop_dmg_key <- setNames(c(1, 1, 1, 1e3, 1e6, 1e9), c("\"\"", "?", "0", "K", "M", "B"))

# Apply conversion
storm_data[, c("PROPDMGEXP", "CROPDMGEXP") := lapply(.SD, toupper), 
        .SDcols = c("PROPDMGEXP", "CROPDMGEXP")]
storm_data[, PROPDMGEXP := conv_exp(prop_dmg_key, "PROPDMGEXP")]
storm_data[, CROPDMGEXP := conv_exp(crop_dmg_key, "CROPDMGEXP")]

# Ensure that the damage and exponent columns are numeric
storm_data[, c("PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP") := lapply(.SD, as.numeric), 
        .SDcols = c("PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")]

# Handle NA Values in Exponent Columns
storm_data[, c("PROPDMGEXP", "CROPDMGEXP") := lapply(.SD, 
          function(x) ifelse(is.na(x), 1, x)), .SDcols = c("PROPDMGEXP", "CROPDMGEXP")]

Economic Cost and Casualties Analysis

Calculating the total economic cost and the number of fatalities and injuries by event type.

# Calculating Economic Cost and Total Fatalities/Injuries
storm_data <- storm_data[, .(prop_cost = sum(PROPDMG * PROPDMGEXP), 
                             crop_cost = sum(CROPDMG * CROPDMGEXP), 
                             tot_cost = sum(PROPDMG * PROPDMGEXP) + sum(CROPDMG * CROPDMGEXP),
                             tot_fatal = sum(FATALITIES), 
                             tot_inj = sum(INJURIES)), 
                         by = .(EVTYPE)]

Visualizations for Health Effects

We first prepare the data for plots for the fatalities/injuries

# Selecting top 10 event types based on fatalities for plotting
top_fatalities <- storm_data[order(-tot_fatal)][1:10]

Casualties by Event Type

We first plot for total fatalities by event type in the left and then we plot for total injuries by event type in the right.

# Reshape the data for combined plotting of fatalities and injuries
long_casualty_summ <- reshape2::melt(top_fatalities, id.vars = "EVTYPE", 
                                     measure.vars = c("tot_fatal", "tot_inj"), 
                                     variable.name = "CasualtyType", 
                                     value.name = "Count")

# Creating a panel plot for separate visualization of fatalities and injuries
ggplot(long_casualty_summ, aes(x = reorder(EVTYPE, -Count), y = Count, fill = CasualtyType)) +
    geom_bar(stat="identity") +
    facet_wrap(~ CasualtyType, scales = "free_y") +
    theme_minimal() +
    labs(title="Total Fatalities and Injuries by Event Type", 
         x="Event Type", 
         y="Count") +
    scale_fill_manual(values = c("tot_fatal" = "red", "tot_inj" = "orange")) +
    theme(axis.text.x=element_text(angle=45, hjust=1))

These plots will display the top 10 event types that have the highest total fatalities and injuries, respectively.

Visualizations for Economic Cost

Creating visualizations to better understand the data.

We first preparing data for the plots

# Selecting top 10 event types based on property and crop costs for plotting
top_prop_cost <- storm_data[order(-prop_cost)][1:10]
top_crop_cost <- storm_data[order(-crop_cost)][1:10]

Property Cost by Event Type

Now, we create the plots: the first is the plot for total property cost by event type. Here we plot total property cost and total crop cost versus event type in two separate plots, because the leading event type is different in each category, and drought only causes crop cost.

# Plot for Total Property Cost
ggplot(top_prop_cost, aes(x=reorder(EVTYPE, -prop_cost), y=prop_cost)) +
    geom_bar(stat="identity", fill="blue") +
    theme_minimal() +
    labs(title="Top 10 Event Types by Total Property Cost", 
         x="Event Type", 
         y="Total Property Cost") +
    theme(axis.text.x=element_text(angle=45, hjust=1))

Crop Cost by Event Type

Then we plot for total crop cost by event type:

# Plot for Total Crop Cost
ggplot(top_crop_cost, aes(x=reorder(EVTYPE, -crop_cost), y=crop_cost)) +
    geom_bar(stat="identity", fill="green") +
    theme_minimal() +
    labs(title="Top 10 Event Types by Total Crop Cost", 
         x="Event Type", 
         y="Total Crop Cost") +
    theme(axis.text.x=element_text(angle=45, hjust=1))

Conclusions

This report provided insights into the human consequences and economic impacts of various storm events.

These plots show that

  • the tornadoes are responsible for most of the medical cases (casualties and injuries) among weather events,
  • the floods have the most devastating effects on the economy with highest property cost,
  • the drought is the most influential factor for the crop damage.