This Report uses the 1950 to 2011 Storm Database of the U.S. National Oceanic and Atmospheric Administration (NOAA) to identify weather events that cause the highest economic and public health Harm(damages) to the US economy. Public health Harm are proxied using total fatalities(FATALITIES) and total injuries(INJURIES) per event type(EVTYPE) While economic Harm is proxied using property damage(PROPDMG) and crop damage(CROPDMG) by event type.
The most harmful event type for public health is
TORNADO. FLOOD creates the most property
damage, followed by HURRICANETYPHOON which also happens to
be the most harmful event regarding crop damage
##Data Processing
#Loading Relevant Libraries
library(readxl)
## Warning: le package 'readxl' a été compilé avec la version R 4.2.3
library(dplyr)
## Warning: le package 'dplyr' a été compilé avec la version R 4.2.3
##
## Attachement du package : 'dplyr'
## Les objets suivants sont masqués depuis 'package:stats':
##
## filter, lag
## Les objets suivants sont masqués depuis 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
## Warning: le package 'tidyr' a été compilé avec la version R 4.2.3
library(ggplot2)
## Warning: le package 'ggplot2' a été compilé avec la version R 4.2.3
#Loading and Exploring the NOAAdata
noaa <- read.csv("repdata_data_StormData.csv.bz2")
dim(noaa)
## [1] 902297 37
names(noaa)
## [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"
head(noaa)
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE EVTYPE
## 1 1 4/18/1950 0:00:00 0130 CST 97 MOBILE AL TORNADO
## 2 1 4/18/1950 0:00:00 0145 CST 3 BALDWIN AL TORNADO
## 3 1 2/20/1951 0:00:00 1600 CST 57 FAYETTE AL TORNADO
## 4 1 6/8/1951 0:00:00 0900 CST 89 MADISON AL TORNADO
## 5 1 11/15/1951 0:00:00 1500 CST 43 CULLMAN AL TORNADO
## 6 1 11/15/1951 0:00:00 2000 CST 77 LAUDERDALE AL TORNADO
## BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1 0 0 NA
## 2 0 0 NA
## 3 0 0 NA
## 4 0 0 NA
## 5 0 0 NA
## 6 0 0 NA
## END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG
## 1 0 14.0 100 3 0 0 15 25.0
## 2 0 2.0 150 2 0 0 0 2.5
## 3 0 0.1 123 2 0 0 2 25.0
## 4 0 0.0 100 2 0 0 2 2.5
## 5 0 0.0 150 2 0 0 2 2.5
## 6 0 1.5 177 2 0 0 6 2.5
## PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE
## 1 K 0 3040 8812
## 2 K 0 3042 8755
## 3 K 0 3340 8742
## 4 K 0 3458 8626
## 5 K 0 3412 8642
## 6 K 0 3450 8748
## LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1 3051 8806 1
## 2 0 0 2
## 3 0 0 3
## 4 0 0 4
## 5 0 0 5
## 6 0 0 6
str(noaa)
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : chr "4/18/1950 0:00:00" "4/18/1950 0:00:00" "2/20/1951 0:00:00" "6/8/1951 0:00:00" ...
## $ BGN_TIME : chr "0130" "0145" "1600" "0900" ...
## $ TIME_ZONE : chr "CST" "CST" "CST" "CST" ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: chr "MOBILE" "BALDWIN" "FAYETTE" "MADISON" ...
## $ STATE : chr "AL" "AL" "AL" "AL" ...
## $ EVTYPE : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : chr "" "" "" "" ...
## $ BGN_LOCATI: chr "" "" "" "" ...
## $ END_DATE : chr "" "" "" "" ...
## $ END_TIME : chr "" "" "" "" ...
## $ 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 : chr "" "" "" "" ...
## $ END_LOCATI: chr "" "" "" "" ...
## $ 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: chr "K" "K" "K" "K" ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr "" "" "" "" ...
## $ WFO : chr "" "" "" "" ...
## $ STATEOFFIC: chr "" "" "" "" ...
## $ ZONENAMES : chr "" "" "" "" ...
## $ 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 : chr "" "" "" "" ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
#The field "EVTYPE" has "r nrow(distinct(select(noaa, EVTYPE)))" distinct values. Going through them, it seems some are summaries. Others have labels that start with spaces. There are some inconsistencies with uppercase/lowercase - lets just move everything to uppercase. I am also going to check for typos.
##Processing of var(EVTYPE)
#For consistency, we will start by removing all white spaces on event types(EVTYPE) and then converting all into event type names into uppercase ("wind" to "WIND") using trimw() and toupper() functions:
noaa$EVTYPE2 <- trimws(noaa$EVTYPE)
noaa$EVTYPE2 <- toupper(noaa$EVTYPE2)
#We then evaluate the total number of unique event types using n_distinct() or nrow(distinct()) on EVTYPE
nrow(distinct(select(noaa, EVTYPE2)))
## [1] 890
Note that the new field EVTYPE2 has r nrow(distinct(select(noaa, EVTYPE2))), less than EVTYPE, so we are making progress.
Next, remove any summary rows, and while we are at it, any NA rows (those with value “?”): we apply !grepl() coupled with a filter() function to extract only data points which are neither “summary” or “?” and then we re-evaluate the number of unique event types using n_distinct() or nrow(distinct()) on EVTYPE in line 53
noaa2 <- filter(noaa, !grepl('summary', noaa$EVTYPE2, ignore.case=TRUE))
noaa2 <- filter(noaa2, EVTYPE2 != '?')
nrow(distinct(select(noaa2, EVTYPE2)))
## [1] 822
The new dataset noaa2 has only 822 distinct event
types.
#Arranging the resulting data: We sort the EVTYPE2 column especially given that sorting the field has the advantage of making the identification of typos in the data easier. (I.e points such as "AVALANCE" instead of "AVALANCHE")
# NOTE: the code below will generate a long list of event types
evtypes <- distinct(select(noaa2, EVTYPE2)) %>% arrange(EVTYPE2)
head(evtypes)
## EVTYPE2
## 1 ABNORMAL WARMTH
## 2 ABNORMALLY DRY
## 3 ABNORMALLY WET
## 4 ACCUMULATED SNOWFALL
## 5 AGRICULTURAL FREEZE
## 6 APACHE COUNTY
Next, we explore the resulting list swiftly using a generic function
adist to compare while assuming that a distance < 2 is a
typo. We update the original event type with the match that has the most
entries assuming that the typo is less common than the original value.
This does create a problem of THUNDERSTORM WIND 50 being
lumped together with THUNDERSTORM WIND 60. So, for this
analysis, We remove all numbers from event types as well as numbers that
start with a letter (e.g:F0, G50 etc.) and
parenthesis () too. We use the gsub() and sub() function to remove all
numbers from data points as highlighted below
# We use gsub() function to remove data points with numbers or potential leading letter (I.e: '[A-Z]?[0-9]+')from event types by replacing them with nothing and no white spaces
noaa2$EVTYPE3 <- gsub('[A-Z]?[0-9]+', '', noaa2$EVTYPE2)
# We use the sub() to remove ' MPH' by replacing them with nothing and no white spaces
noaa2$EVTYPE3 <- sub(' MPH', '', noaa2$EVTYPE3)
# We use the gsub()remove any non-letter character from event types by replacing them with nothing and no white spaces
noaa2$EVTYPE3 <- gsub('[^A-Z]', '', noaa2$EVTYPE3)
#Exploring the new data set
head(noaa2$EVTYPE3)
## [1] "TORNADO" "TORNADO" "TORNADO" "TORNADO" "TORNADO" "TORNADO"
Next we update similar event types, using the most common one as the probably correct one: (NOTE: I didn’t just create the whole matrix/vectorize, because R Studio kept crashing… therefore we loop!)
evtypes <- distinct(select(noaa2, EVTYPE3)) %>% arrange(EVTYPE3)
for (row in 1:(nrow(evtypes)-1)) {
left <- evtypes[row, 1]
right <- evtypes[row+1, 1]
distance <- adist(left, right)[1, 1]
if (distance < 2) {
nleft <- length(noaa2[noaa2$EVTYPE3 == left, 'EVTYPE3'])
nright <- length(noaa2[noaa2$EVTYPE3 == right, 'EVTYPE3'])
if (nleft > nright) {
noaa2$EVTYPE3[noaa2$EVTYPE3 == right] <- left
#print(c(right, length(noaa2[noaa2$EVTYPE3 == right, 'EVTYPE3'])))
}
else {
noaa2$EVTYPE3[noaa2$EVTYPE3 == left] <- right
#print(c(left, length(noaa2[noaa2$EVTYPE3 == left, 'EVTYPE3'])))
}
}
}
evtypes <- distinct(select(noaa2, EVTYPE3)) %>% arrange(EVTYPE3)
head(noaa2$EVTYPE3)
## [1] "TORNADO" "TORNADO" "TORNADO" "TORNADO" "TORNADO" "TORNADO"
It turns out that this approach is only a first stab - we’d have to actually define a smaller set of event types and a mapping to those types. At this stage the EVTYPE variable is ready for aanlysis. Time to look at the other variables that are necessary for our analysis
Let us look at the other fields we’d like to analyse:
#Adjusting the relevant columns to numeric using as.numeric() function for easy manipulation
noaa2$FATALITIES <- as.numeric(noaa2$FATALITIES)
noaa2$INJURIES <- as.numeric(noaa2$INJURIES)
noaa2$PROPDMG <- as.numeric(noaa2$PROPDMG)
noaa2$CROPDMG <- as.numeric(noaa2$CROPDMG)
noaa2$FATALITIES <- as.numeric(noaa2$FATALITIES)
noaa2$INJURIES <- as.numeric(noaa2$INJURIES)
Going through the documentation, it seems as if the
PROPDMGEXP and CROPDMGEXP fields contain an
exponent to multiply the PROPDMG and CROPDMG
values with. I need to clean this up since a couple of conventions are
used:
# We begin by adjusting PROPDMGEXP to character and then convert data points to uppercase and then delete all white spaces for consistency using as.character(), toupper() and the trimws() function.
noaa2$PROPDMGEXP <- as.character(noaa2$PROPDMGEXP)
noaa2$PROPDMGEXP <- toupper(noaa2$PROPDMGEXP)
noaa2$PROPDMGEXP <- trimws(noaa2$PROPDMGEXP)
# For consistency, we replace "?" and "-" and "+" and " " with "0". This step is very essential as it enable us to deal with invalid characters and NAs
noaa2$PROPDMGEXP[noaa2$PROPDMGEXP %in% c("?", "-", "", "+")] <- "0"
# We equally replace K, M, B, H with 3, 6, 9, 2 respectively. Thus making the column easier to manipulate mathematically
noaa2$PROPDMGEXP[noaa2$PROPDMGEXP == "K"] <- "3"
noaa2$PROPDMGEXP[noaa2$PROPDMGEXP == "M"] <- "6"
noaa2$PROPDMGEXP[noaa2$PROPDMGEXP == "B"] <- "9"
noaa2$PROPDMGEXP[noaa2$PROPDMGEXP == "H"] <- "2"
noaa2$PROPDMGEXP <- as.numeric(noaa2$PROPDMGEXP)
# Now we create a new field using mutate() function. This column multiplies the values for PROPDMG by (10 ^ PROPDMGEXP). The resulting variable (property.damage) highlights the value of property damages.
noaa2 <- mutate(noaa2, property.damage = PROPDMG * (10 ^ PROPDMGEXP))
# repeat for crop damage
# first all uppercase
noaa2$CROPDMGEXP <- as.character(noaa2$PROPDMGEXP)
noaa2$CROPDMGEXP <- toupper(noaa2$PROPDMGEXP)
noaa2$CROPDMGEXP <- trimws(noaa2$PROPDMGEXP)
# replace "?" and "-" and "+" and " " with "0"
noaa2$CROPDMGEXP[noaa2$CROPDMGEXP %in% c("?", "-", "", "+")] <- "0"
# replace K, M, B, H with 3, 6, 9, 2 respectively
noaa2$CROPDMGEXP[noaa2$CROPDMGEXP == "K"] <- "3"
noaa2$CROPDMGEXP[noaa2$CROPDMGEXP == "M"] <- "6"
noaa2$CROPDMGEXP[noaa2$CROPDMGEXP == "B"] <- "9"
noaa2$CROPDMGEXP[noaa2$CROPDMGEXP == "H"] <- "2"
noaa2$CROPDMGEXP <- as.numeric(noaa2$CROPDMGEXP)
# create new field with multiplied values
noaa2 <- mutate(noaa2, crop.damage = CROPDMG * (10 ^ CROPDMGEXP))
# next, let's just drop the fields we're not interested in:
noaa3 <- select(noaa2, EVTYPE3, INJURIES, FATALITIES, property.damage, crop.damage)
We are trying to answer tow questions:
If population health is impaired by fatalities and injuries, which events have the highest count of fatalities?
#Grouping, summarizing and arranging fatalities and assigning them to a variable called fatal. We then highlight the top 10 fatalities and assign them to a variable called top.fatal.
fatal <- noaa3 %>% group_by(EVTYPE3) %>% summarize(total.fatalities=sum(FATALITIES)) %>%
arrange(desc(total.fatalities))
top.fatal <- head(fatal, n=10)
top.fatal
## # A tibble: 10 × 2
## EVTYPE3 total.fatalities
## <chr> <dbl>
## 1 TORNADO 5633
## 2 EXCESSIVEHEAT 1903
## 3 FLASHFLOOD 978
## 4 HEAT 937
## 5 LIGHTNING 817
## 6 RIPCURRENT 572
## 7 TSTMWIND 505
## 8 FLOOD 470
## 9 HIGHWIND 248
## 10 AVALANCHE 225
Viewing the TSTMWIND event type, we can see shortcomings
of the data cleaning step - future research could try to do a better job
here, but a quick glance at the numbers suggests that the top three
(TORNADO, EXCESSIVEHEAT and FLASHFLOOD) will not be altered.
What events therefore cause the most injuries? Let’s use can use the same technique here:
#Grouping, summarizing and arranging injuries and assigning them to a variable called injuries. We then highlight the top 10 injuries and assign them to a variable called top.injuries.
injuries <- noaa3 %>% group_by(EVTYPE3) %>% summarize(total.injuries=sum(INJURIES)) %>%
arrange(desc(total.injuries))
top.injuries <- head(injuries, n=10)
top.injuries
## # A tibble: 10 × 2
## EVTYPE3 total.injuries
## <chr> <dbl>
## 1 TORNADO 91364
## 2 TSTMWIND 6961
## 3 FLOOD 6789
## 4 EXCESSIVEHEAT 6525
## 5 LIGHTNING 5230
## 6 HEAT 2100
## 7 ICESTORM 1975
## 8 FLASHFLOOD 1777
## 9 THUNDERSTORMWIND 1488
## 10 HAIL 1361
Here, again, shortcomings of my data cleaning step show up
(“TSTMWIND”). It is still clear, though, that TORNADO is
the big bad event!
Let’s show this graphically:
#Using ggplot() function to graph this results as two panels within the same figure, we set par(mfrow=c(1, 2), mar = c(12, 4, 2, 2.5), cex=0.7).
par(mfrow=c(1, 2), mar = c(12, 4, 2, 2.5), cex=0.7)
barplot(top.fatal$total.fatalities, names.arg=top.fatal$EVTYPE3, ylab= "Fatalities", las=3, main= "Fatalities by Event type")
barplot(top.injuries$total.injuries, names.arg=top.injuries$EVTYPE3, ylab="Injuries", las=3,
main= "Injuries by Event type")
png("replot1.png")
par(mfrow=c(1, 2), mar = c(12, 4, 2, 2.5), cex=0.7)
barplot(top.fatal$total.fatalities, names.arg=top.fatal$EVTYPE3, ylab= "Fatalities", las=3, main= "Fatalities by Event type")
barplot(top.injuries$total.injuries, names.arg=top.injuries$EVTYPE3, ylab="Injuries", las=3,
main= "Injuries by Event type")
dev.off()
## png
## 2
Next, we look at the cost of these events to the economy. We look at both property damage and damage to crops:
#Grouping, summarizing and arranging property and assigning them to a variable called property. We then highlight the top 10 property damaged and assign them to a variable called top.property.
property <- noaa3 %>% group_by(EVTYPE3) %>% summarize(total.property.damage=sum(property.damage)) %>%
arrange(desc(total.property.damage))
top.property <- head(property, n=10)
top.property
## # A tibble: 10 × 2
## EVTYPE3 total.property.damage
## <chr> <dbl>
## 1 FLOOD 144657709807
## 2 HURRICANETYPHOON 69305840000
## 3 TORNADO 56952151876.
## 4 STORMSURGE 43323536000
## 5 FLASHFLOOD 16823223978.
## 6 HAIL 15735819513.
## 7 HURRICANE 11868319010
## 8 TROPICALSTORM 7703890550
## 9 WINTERSTORM 6688497251
## 10 WILDFIRE 5489714000
#Grouping, summarizing and arranging crops and assigning them to a variable called crop. We then highlight the top 10 crop damaged and assign them to a variable called top.crop.
crop <- noaa3 %>% group_by(EVTYPE3) %>% summarize(total.crop.damage=sum(crop.damage)) %>%
arrange(desc(total.crop.damage))
top.crop <- head(crop, n=10)
top.crop
## # A tibble: 10 × 2
## EVTYPE3 total.crop.damage
## <chr> <dbl>
## 1 HURRICANE 802881916053
## 2 HURRICANETYPHOON 732768451330
## 3 FLOOD 87251978368.
## 4 FLASHFLOOD 38865139889.
## 5 TORNADO 28269885712.
## 6 HAIL 15316833978.
## 7 HURRICANEOPALHIGHWINDS 10000000000
## 8 TSTMWIND 7684657862.
## 9 WILDFIRE 7174309478.
## 10 HIGHWIND 7174066263
#Using ggplot() function to graph this results as two panels within the same figure, we set par(mfrow=c(1, 2), mar = c(12, 4, 2, 2.5), cex=0.7).
par(mfrow=c(1, 2), mar = c(12, 4, 2, 2.5), cex=0.7)
barplot(top.property$total.property.damage / 1000, names.arg=top.property$EVTYPE3, ylab='Property Damage (1000$)', las=3, main='Property Damage by Event type')
barplot(top.crop$total.crop.damage / 1000, names.arg=top.crop$EVTYPE3, ylab='Crop Damage (1000$)', las=3, main='Crop Damage by Event type')
png("replot2.png")
par(mfrow=c(1, 2), mar = c(12, 4, 2, 2.5), cex=0.7)
barplot(top.property$total.property.damage / 1000, names.arg=top.property$EVTYPE3, ylab='Property Damage (1000$)', las=3, main='Property Damage by Event type')
barplot(top.crop$total.crop.damage / 1000, names.arg=top.crop$EVTYPE3, ylab='Crop Damage (1000$)', las=3, main='Crop Damage by Event type')
dev.off()
## png
## 2
We can see that HURRICANE and
HURRICANETYPHOON are both the most harmful event types for
crops. These probably could even be lumped into a single event type
HURRICANE (see data cleaning section).
We can also see that FLOOD causes the most property
damage, with HURRICANETYPHOON and TORNADO
among the top 3 too!
I would be interested in seeing how much these numbers change if I
had time to reassign each event type to a smaller list (say, 20-30
types) by doing a more rigorous data cleaning.
#1 might not change, though.
```