Storms and other severe weather events can cause both public health and economic problems for communities and municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.
This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database and analyse the impact in health and economics. 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.
First, we download the data from the NOAA.
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2", destfile = "./StormData.csv.bz2")
Read and see the data
storm_data <- read.csv("StormData.csv.bz2")
str(storm_data)
## '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 ...
head(storm_data[,1:5])
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY
## 1 1 4/18/1950 0:00:00 0130 CST 97
## 2 1 4/18/1950 0:00:00 0145 CST 3
## 3 1 2/20/1951 0:00:00 1600 CST 57
## 4 1 6/8/1951 0:00:00 0900 CST 89
## 5 1 11/15/1951 0:00:00 1500 CST 43
## 6 1 11/15/1951 0:00:00 2000 CST 77
Documentation of the data is available here
According to the documentation, the EVTYPE columns give us the type of event that ocurred.
# Check for missing values in EVTYPE
sum(is.na(storm_data$EVTYPE))
## [1] 0
# Create a table of event types
EVTYPE <- as.data.frame(table(storm_data$EVTYPE))
# Rename columns
colnames(EVTYPE) <- c("Event", "Count")
#View at the data:
head(EVTYPE[order(-EVTYPE$Count, decreasing = FALSE), ],20)
## Event Count
## 244 HAIL 288661
## 856 TSTM WIND 219940
## 760 THUNDERSTORM WIND 82563
## 834 TORNADO 60652
## 153 FLASH FLOOD 54277
## 170 FLOOD 25326
## 786 THUNDERSTORM WINDS 20843
## 359 HIGH WIND 20212
## 464 LIGHTNING 15754
## 310 HEAVY SNOW 15708
## 290 HEAVY RAIN 11723
## 972 WINTER STORM 11433
## 978 WINTER WEATHER 7026
## 216 FUNNEL CLOUD 6839
## 490 MARINE TSTM WIND 6175
## 489 MARINE THUNDERSTORM WIND 5812
## 936 WATERSPOUT 3796
## 676 STRONG WIND 3566
## 919 URBAN/SML STREAM FLD 3392
## 957 WILDFIRE 2761
summary(EVTYPE$Count)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1 1 2 916 5 288661
Number of types if events:
nrow(EVTYPE)
## [1] 985
Quantile 99:
quantile(EVTYPE$Count, probs = 0.99)
## 99%
## 12360.6
Quantile 95:
quantile(EVTYPE$Count, probs = 0.99)
## 99%
## 12360.6
Also, in the documentation we can see that there is property damage, which is the estimation as actual dollar amounts. This estimates are rounded to 3 significant digits, followed by an alphabetical character: B for Billion, K for thousands and M for millions. PROPDMG and PROPDMGEXP columns give us information about property damage, while CROPDMG and CROPDMGEXP give us information about economic crop damage.
#Look at uniques values from PROPDMGEXP column
unique(storm_data$PROPDMGEXP)
## [1] "K" "M" "" "B" "m" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "H" "-" "1" "8"
#We see it is not consistent.
#The same with CROPDMGEXP
unique(storm_data$CROPDMGEXP)
## [1] "" "M" "K" "m" "B" "?" "0" "k" "2"
#Here we the there may be just "2" value inconsistent
Let’s look how much of the inconsistent data is for each value:
prop_test <- as.data.frame(table(storm_data$PROPDMGEXP))
names(prop_test) <- c("value", "count")
prop_test[order(prop_test$count, decreasing=TRUE),]
## value count
## 1 465934
## 17 K 424665
## 19 M 11330
## 5 0 216
## 14 B 40
## 10 5 28
## 6 1 25
## 7 2 13
## 3 ? 8
## 18 m 7
## 16 H 6
## 4 + 5
## 12 7 5
## 8 3 4
## 9 4 4
## 11 6 4
## 2 - 1
## 13 8 1
## 15 h 1
#We see that inconsistent data it's not too much
Let’s do the same for crop data:
crop_test <- as.data.frame(table(storm_data$CROPDMGEXP))
names(crop_test) <- c("value", "count")
crop_test[order(crop_test$count, decreasing=TRUE),]
## value count
## 1 618413
## 7 K 281832
## 9 M 1994
## 6 k 21
## 3 0 19
## 5 B 9
## 2 ? 7
## 4 2 1
## 8 m 1
#We see that inconsistent data it's not too much
So, let’s clear the data in order to make the calculations of the damage impact for each type of event.
storm_data$PROPDMGEXP <- ifelse(storm_data$PROPDMGEXP %in% c("K", "M", "B"),
storm_data$PROPDMGEXP,
"")
unique(storm_data$PROPDMGEXP)
## [1] "K" "M" "" "B"
Let’s do the same for CROPMGEXP;
storm_data$CROPDMGEXP <- ifelse(storm_data$CROPDMGEXP %in% c("K", "M", "B"),
storm_data$CROPDMGEXP,
"")
unique(storm_data$CROPDMGEXP)
## [1] "" "M" "K" "B"
Now, we are going to create a new column in order to make the calculation:
storm_data$propMagnitude <- ifelse(storm_data$PROPDMGEXP == "K", 1000, ifelse(storm_data$PROPDMGEXP == "M", 1000000,ifelse(storm_data$PROPDMGEXP == "B", 1000000000,0)))
the same for crop:
storm_data$cropMagnitude <- ifelse(storm_data$CROPDMGEXP == "K", 1000, ifelse(storm_data$CROPDMGEXP == "M", 1000000,ifelse(storm_data$CROPDMGEXP == "B", 1000000000,0)))
Now, we create a column where the get the total amount of damage for each event:
storm_data$total_DMG <- with(storm_data, PROPDMG*propMagnitude + CROPDMG*cropMagnitude)
Now, lest convert the BGN_DATE column to a class of date:
storm_data$BGN_DATE<- as.Date(as.character(storm_data$BGN_DATE), format = "%m/%d/%Y")
We create now a column for the year of the date:
# Assuming your date column is named "DateColumn"
storm_data$year <- as.integer(format(storm_data$BGN_DATE, "%Y"))
In the documentation we can read that there are injuries and fatalities because of the events. This will lead us to get the most harmful events respect to population health, which are “FATALITIES” and “INJURED” columns in the Storm data.
As there are too much types of events, let’s see who are the most caused monetary damage in general:
economic <- tapply(storm_data$total_DMG, storm_data$EVTYPE, sum)
economic <- data.frame(EVTYPE = names(economic), Total_damage = as.numeric(economic))
economic<-economic[order(economic$Total_damage, decreasing = TRUE),]
#Let's take the first 15 of them, since there are a lot:
economic_top15 <- economic[1:15,]
print(economic_top15)
## EVTYPE Total_damage
## 170 FLOOD 150319678250
## 411 HURRICANE/TYPHOON 71913712800
## 834 TORNADO 57340613590
## 670 STORM SURGE 43323541000
## 244 HAIL 18752904170
## 153 FLASH FLOOD 17562128610
## 95 DROUGHT 15018672000
## 402 HURRICANE 14610229010
## 590 RIVER FLOOD 10148404500
## 427 ICE STORM 8967041310
## 848 TROPICAL STORM 8382236550
## 972 WINTER STORM 6715441250
## 359 HIGH WIND 5908617560
## 957 WILDFIRE 5060586800
## 856 TSTM WIND 5038935790
Now, let’s do it for the last 4 years (2011,2010,2009,2008)
economic_data <- aggregate(total_DMG ~ year + EVTYPE, data = storm_data, sum)
years <- c(2008:2011)
economic_data <- subset(economic_data, economic_data$year %in%years)
economic_data <- economic_data[order(economic_data$year, economic_data$total_DMG, decreasing = TRUE), ]
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
##
## 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
# Create a table with the top 10 total_DMG for each year
top10_economic <- economic_data %>%
group_by(year) %>%
top_n(10, wt = total_DMG)
# View the resulting top table
head(top10_economic,15)
## # A tibble: 15 × 3
## # Groups: year [2]
## year EVTYPE total_DMG
## <int> <chr> <dbl>
## 1 2011 TORNADO 9850961700
## 2 2011 FLOOD 7872549450
## 3 2011 FLASH FLOOD 1472491700
## 4 2011 WILDFIRE 658115400
## 5 2011 HAIL 533663550
## 6 2011 THUNDERSTORM WIND 521723410
## 7 2011 TROPICAL STORM 163243200
## 8 2011 HIGH WIND 86244000
## 9 2011 TSUNAMI 53554000
## 10 2011 LIGHTNING 47090920
## 11 2010 FLOOD 4198737540
## 12 2010 HAIL 3506089950
## 13 2010 TORNADO 1164565450
## 14 2010 FLASH FLOOD 991720900
## 15 2010 FROST/FREEZE 357415000
Let’s graph
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
library(tidytext) #reorder_within function
## Warning: package 'tidytext' was built under R version 4.3.2
library(scales) #comma_format function
## Warning: package 'scales' was built under R version 4.3.2
top10_economic$year <- factor(top10_economic$year, levels = unique(top10_economic$year))
ggplot(top10_economic, aes(reorder_within(EVTYPE, -total_DMG, year), total_DMG, fill = as.factor(year))) +
geom_col() +
geom_text(aes(y=0,label = EVTYPE), vjust = 0, hjust = 0, size = 3, angle = 90) +
facet_wrap(~ year, scales = "free_x") +
#guides(fill=FALSE)+
#theme(legend.position="bottom")+
labs(title = "Top 10 Total Damage by Event Type for Each Year",
x = NULL,
y = "Total Damage in millions USD", fill = "Year") +
scale_y_continuous(labels = scales::comma_format(scale = 1e-6)) + # Format y-axis labels
theme_minimal() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank())
Now, let’s do the same for health consequences.
# Calculate the sum of FATALITIES and INJURIES for each EVTYPE
health <- aggregate(cbind(FATALITIES, INJURIES) ~ EVTYPE, data = storm_data, sum)
# Create a new column Total_damage as the sum of FATALITIES and INJURIES
health$Total_damage <- health$FATALITIES + health$INJURIES
# Order the data frame by Total_damage in descending order
health <- health[order(health$Total_damage, decreasing = TRUE),]
# Take the first 15 rows
head(health, 15)
## EVTYPE FATALITIES INJURIES Total_damage
## 834 TORNADO 5633 91346 96979
## 130 EXCESSIVE HEAT 1903 6525 8428
## 856 TSTM WIND 504 6957 7461
## 170 FLOOD 470 6789 7259
## 464 LIGHTNING 816 5230 6046
## 275 HEAT 937 2100 3037
## 153 FLASH FLOOD 978 1777 2755
## 427 ICE STORM 89 1975 2064
## 760 THUNDERSTORM WIND 133 1488 1621
## 972 WINTER STORM 206 1321 1527
## 359 HIGH WIND 248 1137 1385
## 244 HAIL 15 1361 1376
## 411 HURRICANE/TYPHOON 64 1275 1339
## 310 HEAVY SNOW 127 1021 1148
## 957 WILDFIRE 75 911 986
Now, let’s do it for the last four years of data:
health_data <- aggregate(cbind(FATALITIES, INJURIES) ~ year + EVTYPE, data = storm_data, sum)
# Filter data for the years 2008 to 2011
years <- c(2008:2011)
health_data <- subset(health_data, health_data$year %in% years)
# Order the data frame by year and the sum of both FATALITIES and INJURIES in descending order
health_data <- health_data[order(health_data$year,
health_data$FATALITIES + health_data$INJURIES,
decreasing = TRUE), ]
health_data$total <- health_data$FATALITIES+health_data$INJURIES
# Create a table with the top 10 total for each year
top10_health <- health_data %>%
group_by(year) %>%
top_n(10, wt = total)
# View the resulting top 10 table
head(top10_health,15)
## # A tibble: 15 × 5
## # Groups: year [2]
## year EVTYPE FATALITIES INJURIES total
## <int> <chr> <dbl> <dbl> <dbl>
## 1 2011 TORNADO 587 6163 6750
## 2 2011 HEAT 63 611 674
## 3 2011 THUNDERSTORM WIND 54 373 427
## 4 2011 LIGHTNING 26 194 220
## 5 2011 EXCESSIVE HEAT 36 138 174
## 6 2011 WILDFIRE 6 116 122
## 7 2011 FLASH FLOOD 68 30 98
## 8 2011 FLOOD 58 10 68
## 9 2011 RIP CURRENT 29 27 56
## 10 2011 STRONG WIND 10 33 43
## 11 2010 TORNADO 45 699 744
## 12 2010 THUNDERSTORM WIND 15 323 338
## 13 2010 FLASH FLOOD 67 188 255
## 14 2010 LIGHTNING 29 182 211
## 15 2010 FLOOD 41 127 168
Let’s pivot the data:
library(tidyr)
health_data_long <- pivot_longer(top10_health, cols = c("FATALITIES", "INJURIES"),
names_to = "type", values_to = "value")
health_data_long <- health_data_long %>%
select(year, EVTYPE, type, value, total)
health_data_long <- health_data_long[order(-health_data_long$year, -health_data_long$total),]
head(health_data_long,15)
## # A tibble: 15 × 5
## # Groups: year [1]
## year EVTYPE type value total
## <int> <chr> <chr> <dbl> <dbl>
## 1 2011 TORNADO FATALITIES 587 6750
## 2 2011 TORNADO INJURIES 6163 6750
## 3 2011 HEAT FATALITIES 63 674
## 4 2011 HEAT INJURIES 611 674
## 5 2011 THUNDERSTORM WIND FATALITIES 54 427
## 6 2011 THUNDERSTORM WIND INJURIES 373 427
## 7 2011 LIGHTNING FATALITIES 26 220
## 8 2011 LIGHTNING INJURIES 194 220
## 9 2011 EXCESSIVE HEAT FATALITIES 36 174
## 10 2011 EXCESSIVE HEAT INJURIES 138 174
## 11 2011 WILDFIRE FATALITIES 6 122
## 12 2011 WILDFIRE INJURIES 116 122
## 13 2011 FLASH FLOOD FATALITIES 68 98
## 14 2011 FLASH FLOOD INJURIES 30 98
## 15 2011 FLOOD FATALITIES 58 68
Let’s plot the data:
health_data_long$year <- factor(health_data_long$year, levels = unique(health_data_long$year))
ggplot(health_data_long, aes(x = reorder_within(EVTYPE, -value, year),
y = value, fill = factor(type))) +
geom_col(position = "stack", width = 0.8) +
geom_text(aes(y = 0, label = EVTYPE), vjust = 0, hjust = 0, size = 3, angle = 90) +
facet_wrap(~ year, scales = "free_x") +
labs(title = "Top 10 Total Fatalities and Injuries by Event Type for Each Year",
x = NULL,
y = "Total Fatalities and Injuries",
fill = "type") +
theme_minimal() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank())