This is the 2nd course project for the Data Science: Foundations using R Specialization by Coursera for the Reproducible Research course.
This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage.
Descriptive and exploratory data analysis showed that tornados, floods, and TSTM winds are the most harmful with respect to population health and have the greatest economic consequences. There is a positive correlation between the number of injuries and the amount of property damage.
First, we need to download the data.
# download the data
data_source <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
file_path <- "./data/repdata_data_StormData.csv.bz2"
# create the data directory if it does not exist
if (!file.exists("./data")) {
dir.create("./data")
}
# download the data if it does not exist
if (!file.exists(file_path)) {
download.file(data_source, file_path)
}
# read the data
storm_data <- read.csv(bzfile(file_path))
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : EOF within quoted string
str(storm_data)
## 'data.frame': 481124 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 ...
Our goal is to explore the data and answer the following questions:
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?
To answer the first question, we need to calculate the total number of fatalities and injuries for each event type. We will then rank the event types by the total number of fatalities and injuries.
# calculate the total number of fatalities and injuries for each event type
fatalities_injuries <- aggregate(cbind(FATALITIES, INJURIES) ~ EVTYPE, storm_data, sum)
# rank the event types by the total number of fatalities and injuries
fatalities_injuries <- fatalities_injuries[
order(fatalities_injuries$FATALITIES + fatalities_injuries$INJURIES, decreasing = TRUE),
]
# show the top 10 event types
head(fatalities_injuries, 10)
## EVTYPE FATALITIES INJURIES
## 821 TORNADO 4576 78696
## 167 FLOOD 233 6491
## 843 TSTM WIND 440 6061
## 127 EXCESSIVE HEAT 1376 4106
## 457 LIGHTNING 495 3219
## 421 ICE STORM 70 1918
## 150 FLASH FLOOD 467 1331
## 272 HEAT 708 878
## 957 WINTER STORM 146 1176
## 241 HAIL 12 1030
We can see that the top three are Tornados, Floods, and TSTM Wind.
Here is a visualization of the top 10 events by fatalities and injuries.
# bar plot of the top 10 events by fatalities and injuries
top_10_fatalities_injuries <- head(fatalities_injuries, 10)
barplot(
top_10_fatalities_injuries$FATALITIES + top_10_fatalities_injuries$INJURIES,
names.arg = top_10_fatalities_injuries$EVTYPE,
xlab = "Event Type",
ylab = "Total Fatalities and Injuries",
main = "Top 10 Events by Total Fatalities and Injuries",
col = "lightblue",
las = 2
)
To answer the second question, we need to calculate the total property damage for each event type. We will then rank the event types by the total property damage.
# calculate the total property damage for each event type
property_damage <- aggregate(PROPDMG ~ EVTYPE, storm_data, sum)
# rank the event types by the total property damage
property_damage <- property_damage[
order(property_damage$PROPDMG, decreasing = TRUE),
]
# show the top 10 event types
head(property_damage, 10)
## EVTYPE PROPDMG
## 821 TORNADO 2469344.16
## 843 TSTM WIND 872908.48
## 150 FLASH FLOOD 588408.45
## 773 THUNDERSTORM WINDS 446293.18
## 241 HAIL 335335.55
## 167 FLOOD 303297.30
## 457 LIGHTNING 294550.85
## 354 HIGH WIND 132317.48
## 307 HEAVY SNOW 79974.71
## 371 HIGH WINDS 55625.00
The weather that causes the most deaths, Tornado, is also responsible for the msot property damage. This is followed by TSTM Winds and Flash Floods.
Here is a visualization of the top 10 events by property damage.
# bar plot of the top 10 events by property damage
top_10_property_damage <- head(property_damage, 10)
barplot(
top_10_property_damage$PROPDMG / 1e9,
names.arg = top_10_property_damage$EVTYPE,
xlab = "Event Type",
ylab = "Total Property Damage (in billions)",
main = "Top 10 Events by Total Property Damage",
col = "lightblue",
las = 2
)
Another interesting question is to find the correlation between the number of injuries and the amount of property damage.
# calculate the correlation between the number of injuries and the amount of property damage
correlation <- cor(fatalities_injuries$INJURIES, property_damage$PROPDMG)
# graph the correlation
plot(
fatalities_injuries$INJURIES,
property_damage$PROPDMG,
xlab = "Total Injuries",
ylab = "Total Property Damage",
main = "Correlation between Total Injuries and Total Property Damage",
col = "lightblue"
)
# add the correlation to the graph
text(
x = 1e4,
y = 1e11,
labels = paste("Correlation:", round(correlation, 2)),
col = "red"
)
As we can see, there is a positive correlation between the number of injuries and the amount of property damage.
The top three events that are most harmful with respect to population health are Tornados, Floods, and TSTM Wind. These events are also the top three events with the greatest economic consequences. There is a positive correlation between the number of injuries and the amount of property damage.