Intro

This is the submission of peer-reviewed project 2 for Reproducible research via Coursera.

Setting golbal options:

knitr::opts_chunk$set(echo = TRUE)
Sys.setlocale("LC_TIME","en_US")
## [1] "en_US"
library(plyr)
## Warning: package 'plyr' was built under R version 4.4.1

Assignment Description

The basic goal of this assignment is to explore the NOAA Storm Database and answer some basic questions about severe weather events. You must use the database to answer the questions below and show the code for your entire analysis. Your analysis can consist of tables, figures, or other summaries. You may use any R package you want to support your analysis.

Questions

  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?

Notice: Consider writing your report as if it were to be read by a government or municipal manager who might be responsible for preparing for severe weather events and will need to prioritize resources for different types of events. However, there is no need to make any specific recommendations in your report.

Data Processing

Loads dataset

stormData <- read.csv(bzfile("repdata_data_StormData.csv.bz2"), 
                      header = TRUE,
                      strip.white = TRUE,
                      stringsAsFactors = FALSE)
data <- stormData[, c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")]

Q1:Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?

The most harmful events with respect to population health comes from data fields FATALITIES and INJURIES.

library(dplyr) #for arrange function
## Warning: package 'dplyr' was built under R version 4.4.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Aggregates fatalities ad injuries by Event Type
fatalities <- aggregate(FATALITIES ~ EVTYPE, data = data, sum)
injuries <- aggregate(INJURIES ~ EVTYPE, data = data, sum)

# Arranges in descending order by Event Type by number of fatalities or injuries
fatalities <- arrange(fatalities, desc(FATALITIES), EVTYPE)[1:10, ]
injuries <- arrange(injuries, desc(INJURIES), EVTYPE)[1:10, ]

# Converts event type variable to factor for analysis
fatalities$EVTYPE <- factor(fatalities$EVTYPE, levels = fatalities$EVTYPE)
injuries$EVTYPE <- factor(injuries$EVTYPE, levels = injuries$EVTYPE)

Q2:Across the United States, which types of events have the greatest economic consequences?

The most harmful events with respect to damage comes from data fields Crop (CROPDMG) and Property (PROPDMG) damage. Magnitude (exponent) of the cost value for Property damage is stored in PROPDMGEXP and Crop damage in CROPDMGEXP.

# Normalize Event Damage Amount to Integer Format
tmpPROPDMG <- mapvalues(data$PROPDMGEXP,
                        c("K","M","", "B","m","+","0","5","6","?","4","2","3","h","7","H","-","1","8"), 
                        c(1e3,1e6, 1, 1e9,1e6, 1, 1, 1e5, 1e6, 1, 1e4, 1e2, 1e3, 1, 1e7, 1e2, 1, 10, 1e8))

tmpCROPDMG <- mapvalues(data$CROPDMGEXP,
                        c("","M","K","m","B","?","0","k","2"),
                        c(1, 1e6, 1e3, 1e6, 1e9, 1, 1, 1e3, 1e2))

#Calc Property, Crop and Total Damage costs in Billions $
# Property damage in Billions $
data$TOTAL_PROPDMG <- as.numeric(tmpPROPDMG) * data$PROPDMG / (10^9)

# Crop damage in Billions $
data$TOTAL_CROPDMG <- as.numeric(tmpCROPDMG) * data$CROPDMG / (10^9)

# Create a Total Damage Amount which is the Total of Property and Crop Damage Amounts
data$TOTALDMG <- data$TOTAL_PROPDMG + data$TOTAL_CROPDMG

# Sum total damages for property and crop by Weather Event Type (EVTYPE):
propDamage <- aggregate(TOTAL_PROPDMG ~ EVTYPE, data = data, sum)
cropDamage <- aggregate(TOTAL_CROPDMG ~ EVTYPE, data = data, sum)

# Sum total damages (property + crop) by Weather Event Type (EVTYPE):
totalDamage <- aggregate(TOTALDMG ~ EVTYPE, data = data, sum)

# Arrange descending damages for property and crop by Weather Event Type (EVTYPE) (Top 10 Events):
cropDamage <- arrange(cropDamage, desc(cropDamage$TOTAL_CROPDMG), EVTYPE)[1:10, ]
propDamage <- arrange(propDamage, desc(propDamage$TOTAL_PROPDMG), EVTYPE)[1:10, ]
totalDamage <- arrange(totalDamage, desc(totalDamage$TOTALDMG), EVTYPE)[1:10, ]

# Set Weather Event Type (EVTYPE) as a Factor Variable:
propDamage$EVTYPE <- factor(propDamage$EVTYPE, levels = propDamage$EVTYPE)
cropDamage$EVTYPE <- factor(cropDamage$EVTYPE, levels = cropDamage$EVTYPE)
totalDamage$EVTYPE <- factor(totalDamage$EVTYPE, levels = totalDamage$EVTYPE)

Result

This is the results for Question 1:Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health ?

# Shows results ordered from most harmfull to least
fatalities
##            EVTYPE FATALITIES
## 1         TORNADO       5633
## 2  EXCESSIVE HEAT       1903
## 3     FLASH FLOOD        978
## 4            HEAT        937
## 5       LIGHTNING        816
## 6       TSTM WIND        504
## 7           FLOOD        470
## 8     RIP CURRENT        368
## 9       HIGH WIND        248
## 10      AVALANCHE        224

The corresponding data visualization for Q1 is as follows:

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.1
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.4.1
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(grid)

# Plots Fatalities and Injuries by Event Type
fatalitiesByEvent <- ggplot(fatalities, aes(x = EVTYPE, y = FATALITIES)) + 
    geom_bar(stat = "identity", fill = "blue", width = NULL) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    xlab("Event Type") + ylab("Fatalities")

# Plots the injuries by Event Type
injuriesByEvent <- ggplot(injuries, aes(x = EVTYPE, y = INJURIES)) + 
    geom_bar(stat = "identity", fill = "blue", width = NULL) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    xlab("Event Type") + ylab("Injuries") 


grid.arrange(fatalitiesByEvent, injuriesByEvent, ncol = 2, nrow = 1,
             top = textGrob("Public Health Impact - Fatalities & Injuries from top 10 Weather Events", gp = gpar(fontsize = 14, font = 3)))

This is the results for Question 2:

##PROPERTY DAMAGE by Event Type (Descending in value of damage):
# Property damage in Billions $
propDamage
##               EVTYPE TOTAL_PROPDMG
## 1              FLOOD    144.657710
## 2  HURRICANE/TYPHOON     69.305840
## 3            TORNADO     56.947381
## 4        STORM SURGE     43.323536
## 5        FLASH FLOOD     16.822674
## 6               HAIL     15.735268
## 7          HURRICANE     11.868319
## 8     TROPICAL STORM      7.703891
## 9       WINTER STORM      6.688497
## 10         HIGH WIND      5.270046
# Crop damage in Billions $
cropDamage
##               EVTYPE TOTAL_CROPDMG
## 1            DROUGHT     13.972566
## 2              FLOOD      5.661968
## 3        RIVER FLOOD      5.029459
## 4          ICE STORM      5.022113
## 5               HAIL      3.025954
## 6          HURRICANE      2.741910
## 7  HURRICANE/TYPHOON      2.607873
## 8        FLASH FLOOD      1.421317
## 9       EXTREME COLD      1.292973
## 10      FROST/FREEZE      1.094086

The corresponding data visualization for Q2 is as follows:

# Plots the PROPERTY DAMAGE by Event Type
propPlotDamage <- ggplot(propDamage, aes(x = EVTYPE, y = TOTAL_PROPDMG)) + 
    geom_bar(stat = "identity", fill = "blue") + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Event Type") + ylab("Property Damages (Billions $)") 

#Plots the CROP DAMAGE by Event Type
cropPlotDamage <- ggplot(cropDamage, aes(x = EVTYPE, y = TOTAL_CROPDMG)) + 
    geom_bar(stat = "identity", fill = "blue") + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Event Type") + ylab("Crop Damages (Billions $)") 

## Total damage
totalDamage
##               EVTYPE   TOTALDMG
## 1              FLOOD 150.319678
## 2  HURRICANE/TYPHOON  71.913713
## 3            TORNADO  57.362334
## 4        STORM SURGE  43.323541
## 5               HAIL  18.761222
## 6        FLASH FLOOD  18.243991
## 7            DROUGHT  15.018672
## 8          HURRICANE  14.610229
## 9        RIVER FLOOD  10.148404
## 10         ICE STORM   8.967041
#Plots the TOTAL DAMAGE by Event Type
totPlotDamage <- ggplot(totalDamage, aes(x = EVTYPE, y = TOTALDMG)) + 
    geom_bar(stat = "identity", fill = "blue") + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Event Type") + ylab("Total Prop & Crop Damages (Billions $)") 

grid.arrange(propPlotDamage, cropPlotDamage, totPlotDamage, ncol = 3, nrow = 1,
             top = textGrob("Damage Impact - Property, Crop, & Overall from top 10 Weather Events ", gp = gpar(fontsize = 14, font = 3)))

Conclusions

Tornadoes are responsible for the most deaths and injuries, followed by excessive heat for deaths and windstorms for injuries.