This publication shows exploratory analysis of data from the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database as the second Peer Assessement course project of Reproducible Research, a part of the Datascience Specialization offered by Johns Hopkins University on Coursera.
The data analysis in this publication seeks to address the following questions:
Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?
Across the United States, which types of events have the greatest economic consequences?
Set working directory and download the dataset if it has not been previously downloaded
# Set Working Directory
setwd("C:/Users/user/datasciencecoursera/Reproducible Research/Peer_Assessment2")
# Set file URL
fileURL <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
# Check if file exists. Download file if it does not exist
if(!file.exists("StormData.csv.bz2")) {
download.file(fileURL, destfile="StormData.csv.bz2")}
Read file into R and view the structure of the dataset
filePath <- "./StormData.csv.bz2" # Set file path with compression type
data <- read.csv(filePath, header=T) # Read compressed csv file automatically
str(data) # View the structure of the data
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : Factor w/ 16335 levels "1/1/1966 0:00:00",..: 6523 6523 4242 11116 2224 2224 2260 383 3980 3980 ...
## $ BGN_TIME : Factor w/ 3608 levels "00:00:00 AM",..: 272 287 2705 1683 2584 3186 242 1683 3186 3186 ...
## $ TIME_ZONE : Factor w/ 22 levels "ADT","AKS","AST",..: 7 7 7 7 7 7 7 7 7 7 ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: Factor w/ 29601 levels "","5NM E OF MACKINAC BRIDGE TO PRESQUE ISLE LT MI",..: 13513 1873 4598 10592 4372 10094 1973 23873 24418 4598 ...
## $ STATE : Factor w/ 72 levels "AK","AL","AM",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ EVTYPE : Factor w/ 985 levels " HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : Factor w/ 35 levels ""," N"," NW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_LOCATI: Factor w/ 54429 levels "","- 1 N Albion",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_DATE : Factor w/ 6663 levels "","1/1/1993 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_TIME : Factor w/ 3647 levels ""," 0900CST",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ COUNTY_END: num 0 0 0 0 0 0 0 0 0 0 ...
## $ COUNTYENDN: logi NA NA NA NA NA NA ...
## $ END_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ END_AZI : Factor w/ 24 levels "","E","ENE","ESE",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_LOCATI: Factor w/ 34506 levels "","- .5 NNW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ LENGTH : num 14 2 0.1 0 0 1.5 1.5 0 3.3 2.3 ...
## $ WIDTH : num 100 150 123 100 150 177 33 33 100 100 ...
## $ F : int 3 2 2 2 2 2 2 1 3 3 ...
## $ MAG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ FATALITIES: num 0 0 0 0 0 0 0 0 1 0 ...
## $ INJURIES : num 15 0 2 2 2 6 1 0 14 0 ...
## $ PROPDMG : num 25 2.5 25 2.5 2.5 2.5 2.5 2.5 25 25 ...
## $ PROPDMGEXP: Factor w/ 19 levels "","-","?","+",..: 17 17 17 17 17 17 17 17 17 17 ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: Factor w/ 9 levels "","?","0","2",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ WFO : Factor w/ 542 levels ""," CI","$AC",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ STATEOFFIC: Factor w/ 250 levels "","ALABAMA, Central",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ ZONENAMES : Factor w/ 25112 levels ""," "| __truncated__,..: 1 1 1 1 1 1 1 1 1 1 ...
## $ LATITUDE : num 3040 3042 3340 3458 3412 ...
## $ LONGITUDE : num 8812 8755 8742 8626 8642 ...
## $ LATITUDE_E: num 3051 0 0 0 0 ...
## $ LONGITUDE_: num 8806 0 0 0 0 ...
## $ REMARKS : Factor w/ 436781 levels "","-2 at Deer Park\n",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
Subset and transform the necessary variables required to determine the most harmful event types with respect to population health and with the greatest economic consequencies
library(dplyr)
data <- tbl_df(data)
# Subset data with necessary columns to determine answers to questions raised
sdata <- data %>%
select(EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP) %>%
mutate(PROPDMGEXP=as.character(PROPDMGEXP), CROPDMGEXP=as.character(CROPDMGEXP))
# Clean respective exponential columns for crop and property damage
sdata$PROPDMGEXP=gsub("\\+|\\?|\\-", "0", sdata$PROPDMGEXP)
sdata$PROPDMGEXP=gsub("H|h", "2", sdata$PROPDMGEXP)
sdata$PROPDMGEXP=gsub("K|k", "3", sdata$PROPDMGEXP)
sdata$PROPDMGEXP=gsub("M|m", "6", sdata$PROPDMGEXP)
sdata$PROPDMGEXP=gsub("B|b", "9", sdata$PROPDMGEXP)
sdata$PROPDMGEXP=as.numeric(sdata$PROPDMGEXP)
sdata$PROPDMGEXP[is.na(sdata$PROPDMGEXP)] = 0
sdata$CROPDMGEXP=gsub("\\+|\\?|\\-", 0, sdata$CROPDMGEXP)
sdata$CROPDMGEXP=gsub("H|h", "2", sdata$CROPDMGEXP)
sdata$CROPDMGEXP=gsub("K|k", "3", sdata$CROPDMGEXP)
sdata$CROPDMGEXP=gsub("M|m", "6", sdata$CROPDMGEXP)
sdata$CROPDMGEXP=gsub("B|b", "9", sdata$CROPDMGEXP)
sdata$CROPDMGEXP=as.numeric(sdata$CROPDMGEXP)
sdata$CROPDMGEXP[is.na(sdata$CROPDMGEXP)] = 0
View the structure of the transformed data subset
str(sdata) # View structure of the clean dataset
## Classes 'tbl_df', 'tbl' and 'data.frame': 902297 obs. of 7 variables:
## $ EVTYPE : Factor w/ 985 levels " HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
## $ FATALITIES: num 0 0 0 0 0 0 0 0 1 0 ...
## $ INJURIES : num 15 0 2 2 2 6 1 0 14 0 ...
## $ PROPDMG : num 25 2.5 25 2.5 2.5 2.5 2.5 2.5 25 25 ...
## $ PROPDMGEXP: num 3 3 3 3 3 3 3 3 3 3 ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: num 0 0 0 0 0 0 0 0 0 0 ...
Which types of events are most harmful with respect to population health?
This will be determined by highlighting the events with the highest fatalities and injuries across the United States within the period under review.
dataFATAL <- sdata %>%
group_by(EVTYPE) %>%
summarise(FATALVALUES=sum(FATALITIES)) %>%
arrange(desc(FATALVALUES)) %>%
top_n(10) # Top 10 fatal severe weather events
dataINJURY <- sdata %>%
group_by(EVTYPE) %>%
summarise(INJURYVALUES=sum(INJURIES)) %>%
arrange(desc(INJURYVALUES)) %>%
top_n(10) # Top 10 injury causing severe weather events
Plotting severe weather events causing fatalities and injuries
library(ggplot2)
plot1 <- ggplot(aes(EVTYPE, FATALVALUES), data=dataFATAL) +
geom_bar(aes(fill=EVTYPE), stat="identity") +
labs(title="Top 10 Severe Weather Events \n Causing Fatalities in the United States",
x="Severe Weather Events",
y="Total Fatalities") +
theme(legend.title=element_text(face="bold"),
axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_discrete(name="Event Types")
plot2 <- ggplot(aes(EVTYPE, INJURYVALUES), data=dataINJURY) +
geom_bar(aes(fill=EVTYPE), stat="identity")+
labs(title="Top 10 Severe Weather Events \n Causing Injuries in the United States",
x="Severe Weather Events",
y="Total Injuries") +
theme(legend.title=element_text(face="bold"),
axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_discrete(name="Event Types")
The top 10 severe weather events causing fatalities and injuries are shown in the plots below:
library(gridExtra)
# Plot1 shows severe weather events causing fatalities
# Plot2 shows severe weather events causing injuries
grid.arrange(plot1, plot2, ncol=2)
From the Plots shown above, TORNADO is the most harmful severe weather event causing fatalities and injuries across the United States.
Which types of events have the greatest economic consequences?
This will be determined by highlighting the events with the highest property and crop damages across the United States with the period under review.
options(scipen=999) # Set to display values without exponetials
# Summarise severe weather events with computed total property and crop damage values
dataDMG <- sdata %>%
group_by(EVTYPE) %>%
summarise(PROPDMGVALUES=sum(PROPDMG*10^PROPDMGEXP),
CROPDMGVALUES=sum(CROPDMG*10^CROPDMGEXP))
dataDMGprop <- dataDMG %>%
select(EVTYPE, PROPDMGVALUES) %>%
arrange(desc(PROPDMGVALUES)) %>%
top_n(10) # Top 10 severe weather events with greatest property damage
names(dataDMGprop) <- c("Event Type", "Damage in US Dollars")
dataDMGcrop <- dataDMG %>%
select(EVTYPE, CROPDMGVALUES) %>%
arrange(desc(CROPDMGVALUES)) %>%
top_n(10) # Top 10 severe weather events with greatest crop damage
names(dataDMGcrop) <- c("Event Type", "Damage in US Dollars")
dataDMGtotal <- dataDMG %>%
mutate(DMGTOTAL=PROPDMGVALUES+CROPDMGVALUES) %>%
arrange(desc(DMGTOTAL)) %>%
top_n(10) # Top 10 severe weather events with greatest total crop and property damage
View Property and Crop Damage datasets
print(dataDMGprop) # View top 10 events with the greatest property damage
## Source: local data frame [10 x 2]
##
## Event Type Damage in US Dollars
## 1 FLOOD 144657709807
## 2 HURRICANE/TYPHOON 69305840000
## 3 TORNADO 56947380677
## 4 STORM SURGE 43323536000
## 5 FLASH FLOOD 16822673979
## 6 HAIL 15735267513
## 7 HURRICANE 11868319010
## 8 TROPICAL STORM 7703890550
## 9 WINTER STORM 6688497251
## 10 HIGH WIND 5270046295
print(dataDMGcrop) # View top 10 events with the greatest crop damage
## Source: local data frame [10 x 2]
##
## Event Type Damage in US Dollars
## 1 DROUGHT 13972566000
## 2 FLOOD 5661968450
## 3 RIVER FLOOD 5029459000
## 4 ICE STORM 5022113500
## 5 HAIL 3025954473
## 6 HURRICANE 2741910000
## 7 HURRICANE/TYPHOON 2607872800
## 8 FLASH FLOOD 1421317100
## 9 EXTREME COLD 1292973000
## 10 FROST/FREEZE 1094086000
Plotting severe weather events with greatest economic consequencies
library(tidyr)
# Transform dataset for ease of plotting
plotDMGtotal <- dataDMGtotal %>%
select(EVTYPE, PROPDMGVALUES, CROPDMGVALUES) %>%
gather(TYPE, DMGVALUE, PROPDMGVALUES:CROPDMGVALUES) %>%
mutate(TYPE=ifelse(TYPE=="PROPDMGVALUES", "Property Damages", "Crop Damages"))
plot3 <- ggplot(aes(EVTYPE, DMGVALUE/1000000000, fill=TYPE), data=plotDMGtotal) +
geom_bar(stat="identity") +
labs(title="Top 10 Severe Weather Events with \n Economic Consequencies in the United States",
x="Severe Weather Events",
y="Total Damages in Billions of Dollars") +
theme(legend.title=element_text(face="bold"),
axis.text.x = element_text(angle = 90, hjust = 1))
The top 10 severe weather events with the greatest economic consequencies are shown in the plot below:
print(plot3) # Plot showing top 10 severe weather events with greatest economic consequencies.
Although the data and plot above shows that DROUGHTS account for the greatest crop damages while FLOODS account for the greatest property damages, it also shows that FLOOD is the severe weather event causing the greatest combined economic losses.