Synopsis

The goal of the present analysis is to assess the adverse impacts of weather events, particularly storms, as recorded in the NOAA Storm Database between the years 1950 and 2011.

Information on the Data: Documentation

Data Processing

Environment setup and library calls

invisible(lapply(c("knitr","xtable","reshape2","data.table","dplyr","lubridate","ggplot2","DT","usmap","plotly", "DT"), 
                 library, character.only = TRUE))

2.2: Data Loading

Fileset to datatable pipeline, from downloading the database to

fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
localfile <- "C:\\Users\\bruno\\OneDrive\\Desktop\\COURSERA\\DS_JOHNSHOPKINS\\COURSE09\\repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl, 
              destfile=localfile)
stormDT <- read.csv(localfile)

Data Subsetting

Consider only the columns we are going to use in the analysis, then flush unused items to free up memory

# Instead of listing 29 superfluous columns, consider all columns except the useful as superfluous
allcols <- colnames(stormDT)
necessarycols <- c("STATE","EVTYPE","FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP")
cols_superfluous <- setdiff(allcols, necessarycols)
# Removing superfluous columns
stormDT <- subset(stormDT, select = necessarycols)
# Filter out all occurrences with no death or injuries or damages
stormDT <- stormDT[("EVTYPE" != "?" & 
             ("INJURIES" > 0 | "FATALITIES" > 0 | "PROPDMG" > 0 | "CROPDMG" > 0)), ]

Converting Exponent Columns into Actual Exponents instead of (-,+, H, K, etc)

Making the PROPDMGEXP and CROPDMGEXP columns cleaner so they can be used to calculate property and crop cost.

# Mapping the exponent keys to actual exponents.
propDmgKey <-  c("\"\"" = 10^0,
                 "-" = 10^0, 
                 "+" = 10^0,
                 "H" = 10^2,
                 "K" = 10^3,
                 "M" = 10^6,
                 "B" = 10^9,                 
                 "h" = 10^2,
                 "k" = 10^3,
                 "m" = 10^6,
                 "b" = 10^9,                           
                 "0" = 10^0,
                 "1" = 10^1,
                 "2" = 10^2,
                 "3" = 10^3,
                 "4" = 10^4,
                 "5" = 10^5,
                 "6" = 10^6,
                 "7" = 10^7,
                 "8" = 10^8,
                 "9" = 10^9)

# Map crop damage alphanumeric exponents to numeric values
cropDmgKey <-  c("\"\"" = 10^0,
                 "K" = 10^3,
                 "M" = 10^6,
                 "B" = 10^9,                 
                 "?" = 10^0, 
                 "0" = 10^0)

#Coercing the dataframe to data table format
stormDT <- setDT(stormDT)

#Replacing keys with actual numeric values
stormDT2 <- stormDT[, PROPDMGEXP := propDmgKey[as.character(stormDT[,PROPDMGEXP])]]
stormDT2 <- stormDT2[, CROPDMGEXP := cropDmgKey[as.character(stormDT[,CROPDMGEXP])]]

#Assigning magnitude to 1 when NA
stormDT2 <- replace(stormDT2, is.na(stormDT2), 1)


#Flushing unused memory and variables
stormDT <- stormDT2
remove(stormDT2)
#rearranging variables
stormDT <- stormDT[, .(EVTYPE, FATALITIES, INJURIES, PROPERTYDAMAGE = PROPDMG * PROPDMGEXP, CROPDAMAGE = CROPDMG * CROPDMGEXP)]
totalDamageDT <- stormDT[, .(MATERIAL_COST = sum(PROPERTYDAMAGE) + sum(CROPDAMAGE),
                            HUMAN_COST = sum(FATALITIES) + sum(INJURIES)),
                                  by = .(EVTYPE)]

Using K-means clustering to determine the most damaging events across both categories, and tidying up the final dataset

clust <- kmeans(totalDamageDT[,2:3], centers=5)
totalDamageDT$cluster <- clust["cluster"]
#removing cluster group 1 for being an outlier
totalDamage <- totalDamageDT[cluster > 1]
ClusterKey <-  c("2" = "MORE LETHAL, MID DAMAGE",
                 "3" = "MORE LETHAL, MORE DAMAGING", 
                 "4" = "LESS LETHAL, LESS DAMAGING",
                 "5" = "LESS LETHAL, NOT DAMAGING")
totalDamage <- totalDamage[, cluster := ClusterKey[as.character(totalDamage[,cluster])]]

3: Results

pal <- c("#AA0033", "#000033", "#F6600C", "#6699CC")
fig <- plot_ly(data = totalDamage, x = ~MATERIAL_COST, y = ~HUMAN_COST, color = ~cluster, colors = pal)
fig