The questions are answered in this analysis: 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? The analysis of the data from NOAA storm database shows that floods create the most economic damage in the US (in terms of crop and property damage), while tornadoes make the largest population health impact.
Unzipping and loading the data
if(!exists("storm.data")) {
storm.data <- read.csv(bzfile("repdata_data_StormData.csv.bz2"),header = TRUE)
}
Preliminary observations can be made by examining the structure
str(storm.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 ""," Christiansburg",..: 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 ""," CANTON"," TULIA",..: 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","%SD",..: 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 "","\t","\t\t",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
The useful variables were then extracted from the data. For the dependent variables on the question of population health, two variables are extracted: * FATALITIES * INJURIES
For the dependent variables on the question of economic loss, 4 variables are extracted: * PROPDMG * PROPDMGEXP * CROPDMG * CROPDMGEXP
For the independent variable of weather event, the variable “EVTYPE” is used.
var <- c("FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP","EVTYPE")
dat <- storm.data[, var]
Certain events can be grouped under a single category. The new variable for classifying events is “EVENT”
dat$EVENT <- "OTHER"
dat$EVENT[grep("HAIL", dat$EVTYPE, ignore.case = TRUE)] <- "HAIL"
dat$EVENT[grep("HEAT", dat$EVTYPE, ignore.case = TRUE)] <- "HEAT"
dat$EVENT[grep("FLOOD|FLD", dat$EVTYPE, ignore.case = TRUE)] <- "FLOOD"
dat$EVENT[grep("WIND", dat$EVTYPE, ignore.case = TRUE)] <- "WIND"
dat$EVENT[grep("STORM|HURRICANE|BLIZZARD", dat$EVTYPE, ignore.case = TRUE)] <- "STORM"
dat$EVENT[grep("SNOW|WINTER|FROST|COLD", dat$EVTYPE, ignore.case = TRUE)] <- "WINTER"
dat$EVENT[grep("TORNADO", dat$EVTYPE, ignore.case = TRUE)] <- "TORNADO"
dat$EVENT[grep("RAIN", dat$EVTYPE, ignore.case = TRUE)] <- "RAIN"
dat$EVENT[grep("FOG", dat$EVTYPE, ignore.case = TRUE)] <- "FOG"
dat$EVENT[grep("LIGHTNING", dat$EVTYPE, ignore.case = TRUE)] <- "LIGHTNING"
dat$EVENT[grep("DRY|DRI|DROUGHT", dat$EVTYPE, ignore.case = TRUE)] <- "DROUGHT"
dat$EVENT[grep("SLIDE|AVALANCHE", dat$EVTYPE, ignore.case = TRUE)] <- "LANDSLIDE"
Check for missing values. There are no missing values.
sum(is.na(dat$FATALITIES))
## [1] 0
sum(is.na(dat$INJURIES))
## [1] 0
sum(is.na(dat$PROPDMG))
## [1] 0
sum(is.na(dat$CROPDMG))
## [1] 0
sum(is.na(dat$PROPDMGEXP))
## [1] 0
sum(is.na(dat$CROPDMGEXP))
## [1] 0
The two pairs of crop damage and property damage variables require recoding into two separate variables expressed in dollars. For both PROPDMGEXP and CROPDMGEXP, “K” means thousand dollars, “M” means million dollars, and “B” means billion dollars.
dat$PROPDMGEXP <- as.character(dat$PROPDMGEXP)
dat$PROPDMGEXP[!grepl("K|M|B", dat$PROPDMGEXP, ignore.case = TRUE)] <- 0
dat$PROPDMGEXP[grep("K", dat$PROPDMGEXP, ignore.case = TRUE)] <- "3"
dat$PROPDMGEXP[grep("M", dat$PROPDMGEXP, ignore.case = TRUE)] <- "6"
dat$PROPDMGEXP[grep("B", dat$PROPDMGEXP, ignore.case = TRUE)] <- "9"
dat$PROPDMGEXP <- as.numeric(as.character(dat$PROPDMGEXP))
dat$prop <- dat$PROPDMG * 10^dat$PROPDMGEXP
dat$CROPDMGEXP <- as.character(dat$CROPDMGEXP)
dat$CROPDMGEXP[!grepl("K|M|B", dat$CROPDMGEXP, ignore.case = TRUE)] <- 0
dat$CROPDMGEXP[grep("K", dat$CROPDMGEXP, ignore.case = TRUE)] <- "3"
dat$CROPDMGEXP[grep("M", dat$CROPDMGEXP, ignore.case = TRUE)] <- "6"
dat$CROPDMGEXP[grep("B", dat$CROPDMGEXP, ignore.case = TRUE)] <- "9"
dat$CROPDMGEXP <- as.numeric(as.character(dat$CROPDMGEXP))
dat$crop <- dat$CROPDMG * 10^dat$CROPDMGEXP
A new dataframe is created to aggregate the effects of each type of weather event on economic variables of crop and property damage. A new variable has been created to add up effects of crop and property damage.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
dat$damage <- dat$crop + dat$prop
damagetot <- dat %>%
group_by(EVENT) %>%
summarise(damage_ev = sum(damage))%>%
arrange(-damage_ev)
damagetot
## # A tibble: 13 x 2
## EVENT damage_ev
## <chr> <dbl>
## 1 FLOOD 179835841779.
## 2 STORM 163721576541.
## 3 TORNADO 59010559549.
## 4 HAIL 18779880521.
## 5 DROUGHT 15025820380
## 6 WIND 12011967768.
## 7 WINTER 10854068003.
## 8 OTHER 10742338260
## 9 RAIN 4184512992
## 10 LIGHTNING 951105037.
## 11 HEAT 924539250
## 12 LANDSLIDE 355620900
## 13 FOG 25011500
New dataframes are created to aggregate the total fatalities and injuries caused by each type of weather events.
dead <- dat %>%
group_by(EVENT) %>%
summarise(dead_ev = sum(FATALITIES))%>%
arrange(-dead_ev)
hurt <- dat %>%
group_by(EVENT) %>%
summarise(hurt_ev = sum(INJURIES))%>%
arrange(-hurt_ev)
The top 10 events with the highest total economic damages (property and crop combined) are shown here.
library(ggplot2)
ggplot(damagetot[1:10,],
aes(x=reorder(EVENT, -damage_ev), y=damage_ev)) +
geom_bar(stat="identity") +
ggtitle("Top 10 Events with Highest Economic Impact") +
labs(x="Event Type", y="Total Economic Impact (USD)")
The top 10 events with the highest total fatalities are shown here.
ggplot(dead[1:10,],
aes(x=reorder(EVENT, -dead_ev), y=dead_ev)) +
geom_bar(stat="identity") +
ggtitle("Top 10 Events with Highest total fatalities") +
labs(x="Event Type", y="Total Fatalities")
The top 10 events with the highest total injuries are shown here.
ggplot(hurt[1:10,],
aes(x=reorder(EVENT, -hurt_ev), y=hurt_ev)) +
geom_bar(stat="identity") +
ggtitle("Top 10 Events with highest total injuries") +
labs(x="Event Type", y="Total Injuries")
Therefore, we can conclude that floods create the most economic damage in the US, while tornadoes make the largest population health impact.