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. The goal is to present in the clearest possible manner the material and property damages, on one hand, and injuries and fatalities on the other hand.
Information on the Data: Documentation
library("knitr")
## Warning: package 'knitr' was built under R version 4.2.3
library("xtable")
## Warning: package 'xtable' was built under R version 4.2.3
library("reshape2")
## Warning: package 'reshape2' was built under R version 4.2.3
library("data.table")
## Warning: package 'data.table' was built under R version 4.2.3
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:reshape2':
##
## dcast, melt
library("dplyr")
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library("lubridate")
## Warning: package 'lubridate' was built under R version 4.2.3
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:data.table':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 4.2.3
library("DT")
## Warning: package 'DT' was built under R version 4.2.3
library("usmap")
## Warning: package 'usmap' was built under R version 4.2.3
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\\COURSE05\\ASSIGNMENT02\\repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl,
destfile=localfile)
stormDT <- read.csv(localfile)
colnames(stormDT)
## [1] "STATE__" "BGN_DATE" "BGN_TIME" "TIME_ZONE" "COUNTY"
## [6] "COUNTYNAME" "STATE" "EVTYPE" "BGN_RANGE" "BGN_AZI"
## [11] "BGN_LOCATI" "END_DATE" "END_TIME" "COUNTY_END" "COUNTYENDN"
## [16] "END_RANGE" "END_AZI" "END_LOCATI" "LENGTH" "WIDTH"
## [21] "F" "MAG" "FATALITIES" "INJURIES" "PROPDMG"
## [26] "PROPDMGEXP" "CROPDMG" "CROPDMGEXP" "WFO" "STATEOFFIC"
## [31] "ZONENAMES" "LATITUDE" "LONGITUDE" "LATITUDE_E" "LONGITUDE_"
## [36] "REMARKS" "REFNUM"
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)), ]
datatable(head(stormDT,10))
#flush memory
gc()
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)
gc()
#showing a small subset of transformed data
datatable(head(stormDT,10))
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)]
#totalCostDT <- totalCostDT[order(-Total_Cost), ]
#totalCostDT <- totalCostDT[1:10, ]
#head(totalCostDT, 5)
clust <- kmeans(totalDamageDT[,2:3], centers=3)
totalDamageDT$cluster <- clust["cluster"]
datatable(totalDamageDT[order(cluster),])
The occurrence of extreme outliers in both dimensions compromised the clustering into 3 groups, with groups 1 and 3, corresponding to the most extreme events, having 4 elements altogether, and the remaining events were clustered together. A more thorough analysis could very well exclude the outliers, but the objects of study are the most damaging events, so the outliers must be evidenced.
Listing the worst 10 kinds of events, in terms of population health impact
WORST_HEALTH <- head(totalDamageDT[order(-HUMAN_COST),c(1,3)],10)
datatable(WORST_HEALTH)
# Create chart
ggplot(WORST_HEALTH,
aes(y = reorder(EVTYPE,HUMAN_COST),
x = HUMAN_COST,
fill=HUMAN_COST,
label=HUMAN_COST)
) +
geom_bar(stat="identity") +
scale_fill_gradient(low="#CC99FF",high="#330066") +
labs(title = "Worst Populational Health Impacts",
subtitle = "Most harmful events in terms of populational health (injuries + deaths)",
x = "Human cost (injuries + deaths)",
y = "Event")
Listing the worst 10 kinds of events, in terms of population health impact
WORST_MATERIAL <- head(totalDamageDT[order(-MATERIAL_COST),c(1,2)],10)
datatable(WORST_MATERIAL)
# Create chart
ggplot(WORST_MATERIAL,
aes(y = reorder(EVTYPE,MATERIAL_COST),
x = MATERIAL_COST,
fill=MATERIAL_COST,
label=MATERIAL_COST)
) +
geom_bar(stat="identity") +
scale_fill_gradient(low="#00CCCC",high="#003333") +
labs(title = "Worst Material Impacts",
subtitle = "Most harmful events in terms of property + crop damages",
x = "Material cost, USD (property + crops)",
y = "Event")
The k-means clustering analysis revealed a small number of outliers with a disproportionally high population and property impact. The following plot shines light into these events and show an overall distribution of health versus material damage
Most_Extreme_Event <- totalDamageDT[cluster == 1]
Non_Extreme_Events <- totalDamageDT[cluster == 2]
Extreme_Events <- totalDamageDT[cluster == 3]
plot(x=1,
xlim=c(0,1.6*(10^11)),
ylim=c(0,1.2*(10^5)),
main="Most Extreme Events Across Material and Health Factors",
sub="Outliers from clustering process, meaning the most destructive events",
xlab="MATERIAL_COST",
ylab="HUMAN_COST",
type="n"
)
points(x = Non_Extreme_Events$MATERIAL_COST,
y = Non_Extreme_Events$HUMAN_COST,
pch = 16,
col = rgb(0,204,255,max=255))
points(x = Extreme_Events$MATERIAL_COST,
y = Extreme_Events$HUMAN_COST,
pch = 16,
col = rgb(255,0,153,max=255))
text(Extreme_Events$MATERIAL_COST,Extreme_Events$HUMAN_COST+3000,Extreme_Events$EVTYPE,cex=0.7,col="#FF0099")
points(x = Most_Extreme_Event$MATERIAL_COST,
y = Most_Extreme_Event$HUMAN_COST,
pch = 16,
col = rgb(0,0,102,max=255))
text(Most_Extreme_Event$MATERIAL_COST,Most_Extreme_Event$HUMAN_COST+3000,Most_Extreme_Event$EVTYPE,cex=0.7,col="#000066")
Summing up, while Tornados are the most devastating events in terms of human lives, Floods are the most destructive in material terms, with Hurricanes/Typhoons and Storm Surges being also overall destructive events.