In the U.S., certain weather events have pronounced effects on both population health and the economy. Preliminary data analysis reveals varying impacts of these events. To safeguard public health, it’s crucial to identify which events cause the most harm. Similarly, understanding economic implications helps in planning and resource allocation. This report sheds light on the events with the most significant health and economic consequences. The insights aim to guide government and municipal managers in prioritizing resources for severe weather preparedness, although no direct recommendations are provided.
Let’s start by downloading the data and taking a look at the structure.
stormdata <- read.csv("StormData.csv")
summary(stormdata)
## STATE__ BGN_DATE BGN_TIME TIME_ZONE
## Min. : 1.0 Length:902297 Length:902297 Length:902297
## 1st Qu.:19.0 Class :character Class :character Class :character
## Median :30.0 Mode :character Mode :character Mode :character
## Mean :31.2
## 3rd Qu.:45.0
## Max. :95.0
##
## COUNTY COUNTYNAME STATE EVTYPE
## Min. : 0.0 Length:902297 Length:902297 Length:902297
## 1st Qu.: 31.0 Class :character Class :character Class :character
## Median : 75.0 Mode :character Mode :character Mode :character
## Mean :100.6
## 3rd Qu.:131.0
## Max. :873.0
##
## BGN_RANGE BGN_AZI BGN_LOCATI END_DATE
## Min. : 0.000 Length:902297 Length:902297 Length:902297
## 1st Qu.: 0.000 Class :character Class :character Class :character
## Median : 0.000 Mode :character Mode :character Mode :character
## Mean : 1.484
## 3rd Qu.: 1.000
## Max. :3749.000
##
## END_TIME COUNTY_END COUNTYENDN END_RANGE
## Length:902297 Min. :0 Mode:logical Min. : 0.0000
## Class :character 1st Qu.:0 NA's:902297 1st Qu.: 0.0000
## Mode :character Median :0 Median : 0.0000
## Mean :0 Mean : 0.9862
## 3rd Qu.:0 3rd Qu.: 0.0000
## Max. :0 Max. :925.0000
##
## END_AZI END_LOCATI LENGTH WIDTH
## Length:902297 Length:902297 Min. : 0.0000 Min. : 0.000
## Class :character Class :character 1st Qu.: 0.0000 1st Qu.: 0.000
## Mode :character Mode :character Median : 0.0000 Median : 0.000
## Mean : 0.2301 Mean : 7.503
## 3rd Qu.: 0.0000 3rd Qu.: 0.000
## Max. :2315.0000 Max. :4400.000
##
## F MAG FATALITIES INJURIES
## Min. :0.0 Min. : 0.0 Min. : 0.0000 Min. : 0.0000
## 1st Qu.:0.0 1st Qu.: 0.0 1st Qu.: 0.0000 1st Qu.: 0.0000
## Median :1.0 Median : 50.0 Median : 0.0000 Median : 0.0000
## Mean :0.9 Mean : 46.9 Mean : 0.0168 Mean : 0.1557
## 3rd Qu.:1.0 3rd Qu.: 75.0 3rd Qu.: 0.0000 3rd Qu.: 0.0000
## Max. :5.0 Max. :22000.0 Max. :583.0000 Max. :1700.0000
## NA's :843563
## PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## Min. : 0.00 Length:902297 Min. : 0.000 Length:902297
## 1st Qu.: 0.00 Class :character 1st Qu.: 0.000 Class :character
## Median : 0.00 Mode :character Median : 0.000 Mode :character
## Mean : 12.06 Mean : 1.527
## 3rd Qu.: 0.50 3rd Qu.: 0.000
## Max. :5000.00 Max. :990.000
##
## WFO STATEOFFIC ZONENAMES LATITUDE
## Length:902297 Length:902297 Length:902297 Min. : 0
## Class :character Class :character Class :character 1st Qu.:2802
## Mode :character Mode :character Mode :character Median :3540
## Mean :2875
## 3rd Qu.:4019
## Max. :9706
## NA's :47
## LONGITUDE LATITUDE_E LONGITUDE_ REMARKS
## Min. :-14451 Min. : 0 Min. :-14455 Length:902297
## 1st Qu.: 7247 1st Qu.: 0 1st Qu.: 0 Class :character
## Median : 8707 Median : 0 Median : 0 Mode :character
## Mean : 6940 Mean :1452 Mean : 3509
## 3rd Qu.: 9605 3rd Qu.:3549 3rd Qu.: 8735
## Max. : 17124 Max. :9706 Max. :106220
## NA's :40
## REFNUM
## Min. : 1
## 1st Qu.:225575
## Median :451149
## Mean :451149
## 3rd Qu.:676723
## Max. :902297
##
From the summary, we can see a number of issues:
Many columns have impossible to understand abbreviations and it took ages going through the supplementary document trying to figure out, for example, what columns “F” and “END_AZI” represent. All columns will be renamed in camelcase font rather than all upoercase.
Several columns such as BGN_DATE, BGN_TIME, TIME_ZONE, and others are of type character. BGN_DATE will need to be converted to appropriate date and time formats, but times can be ignored for the purposes of this analysis.
COUNTY_END has a constant value of 0 throughout the dataset. The same goes for COUNTYENDN, which contains only NA values. These columns can therefore be ignored or deleted.
Many columns have missing values. Fro example F has many missing values (843,563 NA’s), but this makes sense if F is storm force and not all the weather events are storms or storms that register on this scale. LATITUDE and LONGITUDE also have some missing values, but these might not be that valuable if the analysis does not go deep eneough to include exact starting and ending locations. State and county should be sufficient.
Columns like LENGTH, WIDTH, FATALITIES, INJURIES, PROPDMG, and CROPDMG have maximum values that are significantly higher than their respective 3rd quartile values, indicating potential outliers, but given the context of the data, this might make sense.
Columns like BGN_AZI and END_AZI are character columns, but these should represent geographical coordinates. However, these are not relevant to the analysis. In addition, PROPDMGEXP and CROPDMGEXP are also character columns but these represent damage estimates rounded to three significant digits, followed by an alphabetical character signifying the magnitude of the number, i.e., 1.55B for $1,550,000,000. Alphabetical characters used to signify magnitude include “K” for thousands, “M” for millions, and “B” for billions. These columns (PropertyDamage/PropertyDamageExp and CropDamage/CropDamageExp) are essential in determing the economic impact of different events and will need to be combined in order to return a numeric $ value.
There are longitude and latitude values that are zero, which may be placeholders or erroneous entries. However, these are perhaps not so important for you analysis and can be ignored since we already have the state and county information.
EVTYPE is a character column containing all the different climate events that occurred, but a unique() call on this column reveals that there are almost a thousand uniques entries, many of which have values that are similar in meaning but represented differently, such as tornado, wind, tstm wind, hurricane erin, high winds, etc.
From here, we can start to tidy the data to make it easier to work with, first of all by converting column names from uppercase to camelcase.
new_column_names <- c(
"StateCode", # STATE__
"StartDate", # BGN_DATE
"StartTime", # BGN_TIME
"TimeZone", # TIME_ZONE
"CountyCode", # COUNTY
"CountyName", # COUNTYNAME
"StateName", # STATE
"EventType", # EVTYPE
"BeginRange", # BGN_RANGE
"BeginAzimuth", # BGN_AZI
"BeginLocation", # BGN_LOCATI
"EndDate", # END_DATE
"EndTime", # END_TIME
"CountyEnd", # COUNTY_END (though this column seems redundant given every value is 0)
"CountyEndN", # COUNTYENDN
"EndRange", # END_RANGE
"EndAzimuth", # END_AZI
"EndLocation", # END_LOCATI
"Length", # LENGTH
"Width", # WIDTH
"FujitaScale", # F (Tornado Fujita Scale)
"Magnitude", # MAG
"Fatalities", # FATALITIES
"Injuries", # INJURIES
"PropertyDamage", # PROPDMG
"PropertyDamageExp", # PROPDMGEXP
"CropDamage", # CROPDMG
"CropDamageExp", # CROPDMGEXP
"WeatherForecastOffice", # WFO
"StateOffice", # STATEOFFIC
"ZoneNames", # ZONENAMES
"Latitude", # LATITUDE
"Longitude", # LONGITUDE
"LatitudeEnd", # LATITUDE_E
"LongitudeEnd", # LONGITUDE_
"Remarks", # REMARKS
"RefNum" # REFNUM
)
colnames(stormdata) <- new_column_names
Next, the dates in the data frame are in the MM-DD-YYY format, which is strange, so I will convert them to the more standard YYYY-MM-DD. The dates and specific locations of different events aren’t necessary to answer the two main quetions of the assignment but it might be interesting to see whether extreme events happen at certain times of year.
# Convert the 'bgn_date' and 'end_date' columns to Date class
stormdata$StartDate <- as.Date(stormdata$StartDate, format="%m/%d/%Y %H:%M:%S")
stormdata$EndDate <- as.Date(stormdata$EndDate, format="%m/%d/%Y %H:%M:%S")
Next, we can combine PropertyDamage/PropertyDamageExp and CropDamage/CropDamageExp to return numeric $ values.
# Define function to convert values with K, M, B exponents to numeric
convert_to_numeric <- function(value, exp) {
switch(exp,
"K" = value * 1e3,
"M" = value * 1e6,
"B" = value * 1e9,
value # if no exponent, just return the original value
)
}
# Apply the function to PropertyDamage and CropDamage columns
stormdata$PropertyDamageNumeric <- mapply(convert_to_numeric, stormdata$PropertyDamage, stormdata$PropertyDamageExp)
stormdata$CropDamageNumeric <- mapply(convert_to_numeric, stormdata$CropDamage, stormdata$CropDamageExp)
Finally, we can try to group the event types into categories. This process was extremely time consuming because there are so many different unique event types that can be grouped based on similarities in their descriptions.
First of all, we convert all event types to lowercase and determine how many unique event types we have.
# Convert 'EventType' column to lowercase
stormdata$EventType <- tolower(stormdata$EventType)
# Determine unique event types
unique_event_types <- unique(stormdata$EventType)
This coding results in 898 unique events, which can then be categorised MANUALLY into groups with similar characterisitcs.
categories <- list(
'Wind related' = c('tstm wind', 'thunderstorm winds', 'thunderstorm wind', 'high winds', 'wind', 'gusty winds', 'strong winds', 'winds', 'strong wind', 'high wind damage', 'microburst', 'downburst', 'gustnado and', 'downburst winds', 'microburst winds', 'tornado', 'tornados', 'waterspout/tornado', 'funnel', 'funnel cloud', 'wall cloud', 'hurricane', 'tstm wind 40', 'tstm wind (41)', 'tstm wnd', ' tstm wind', 'typhoon', 'tstm wind (g45)', 'tstm wind (g35)', 'tstm', 'tstm wind g45'),
'Thunderstorm related' = c('thunderstorm', 'severe thunderstorm', 'severe thunderstorms', 'thunderstorms', 'tropical storm', 'torndao'),
'Snow related' = c('snow', 'snow/ice', 'winter storm', 'blizzard', 'heavy snow', 'avalanche', 'snow and wind', 'winter storms', 'heavy snowpack', 'blowing snow', 'wintry mix', 'winter weather', 'sleet/rain/snow', 'snow/rain/sleet'),
'Cold related' = c('record cold', 'extreme cold', 'wind chill', 'freeze', 'record cold and high wind', 'low temperature record', 'record low', 'extreme/record cold', 'unseasonably cold', 'first snow', 'cold', 'unusually cold', 'cold and frost', 'freezing fog', 'record cold', 'extreme cold/wind chill'),
'Rain & flood related' = c('freezing rain', 'heavy rain', 'flash flood', 'flash flooding', 'flooding', 'heavy rains', 'flooding/heavy rain', 'urban flooding', 'rainstorm', 'flood/flash flood', 'flood/rain/winds', 'flash flood winds', 'stream flooding', 'urban and small', 'heavy rain/flooding', 'glaze', 'coastal flooding', 'rain/snow', 'snow/rain/sleet', 'flood', 'river flood', 'heavy precipatation', 'local flood', 'prolonged rain', 'dam failure', 'high water', 'record/excessive rainfall', 'flash floooding', 'excessive rainfall', 'torrential rainfall', 'rain (heavy)', 'prolonged rain'),
'High-temperature related' = c('heat', 'record high temperature', 'record high', 'extreme heat', 'record heat', 'heat wave', 'high temperature record', 'unusually warm', 'warm weather', 'record warmth', 'unseasonably warm', 'record warm temps.', 'excessive heat/drought'),
'Fire related' = c('wild fires', 'drought', 'wildfire', 'wild/forest fire', 'brush fires'),
'Lightning related' = c('lightning', 'lighting', 'lightning and heavy rain', 'thunderstorm winds lightning', 'lightning/heavy rain', 'lightning thunderstorm windss', 'ligntning'),
'Ocean related' = c('rip current', 'waterspout', 'high tides', 'coastal flood', 'heavy surf coastal flooding', 'heavy surf', 'high surf', 'marine mishap', 'waterspout/tornado', 'rip currents heavy surf', 'rough seas', 'high swells', 'heavy seas', 'heavy swells', 'rough surf', 'hazardous surf', 'storm surge/tide'),
'Hail related' = c('hail', 'hail 1.75)', 'hail storm', 'hail 75', 'ice pellets', 'ice storm', 'sleet/ice storm'),
'Unclassified' = character()
)
# Categorize the events
event_categories <- character(length(unique_event_types))
for (event in unique_event_types) {
assigned <- FALSE
for (cat_name in names(categories)) {
if (any(grepl(paste(categories[[cat_name]], collapse="|"), tolower(event)))) {
event_categories[unique_event_types == event] <- cat_name
assigned <- TRUE
break
}
}
if (!assigned) {
event_categories[unique_event_types == event] <- 'Unclassified'
}
}
The first iteration of this process left 343 events as “Unclassified”. However, when I print them, I can see that many can be included in the previous categories, so I built on the previous coding to include more events (often to include typographical errors) within each category. The second iteration resulted in 265 “Unclassified” unique_event_types.
I also noticed that many of the unique_event_types are actually just a reference to a summary of some kind, so i removed these as well, resulting in 240 “Unlclassified” unique_event_types.
# Identify indices of unique_event_types that start with "Summary"
summary_indices <- grep("^summary", unique_event_types)
# Remove those indices from both unique_event_types and event_categories
unique_event_types <- unique_event_types[-summary_indices]
I performed one more iteration of the categorisation process by adding the most severe “Unclassified” unique_event_types to the appropriate categories, resulting in a final total of 171 less severe nad/or hard to categorise weather-related events.
Now it is time to do exploratory data analysis to determine, first of all, the impact of each category in the unique_event_types variable on health (comprising fatalities and injuries).
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
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
# Associate each EventType with a category from unique_event_types
stormdata$category <- sapply(stormdata$EventType, function(event) {
category <- "Unclassified"
for (key in names(categories)) {
if (event %in% categories[[key]]) {
category <- key
break
}
}
return(category)
})
# Aggregate data based on category
impact_data <- stormdata %>%
group_by(category) %>%
summarise(Fatalities = sum(Fatalities, na.rm = TRUE),
Injuries = sum(Injuries, na.rm = TRUE)) %>%
mutate(TotalImpact = Fatalities + Injuries) %>%
arrange(-TotalImpact)
# Plotting the top N categories by TotalImpact
N <- 10
top_categories <- impact_data[1:N,]
ggplot(top_categories, aes(x=reorder(category, TotalImpact), y=TotalImpact, fill=TotalImpact)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title="Top Weather-Related Events by Impact on Health",
x="Weather-Related Event Category",
y="Total Impact (Fatalities + Injuries)") +
scale_fill_gradient(low="green", high="red") +
theme_minimal() +
theme(legend.position="none")
Clearly, results suggest that wind-related events are by far the most serious when it comes to health impacts.
Next, we will look at which unique_event_types by category have the greatest economic consequences as determined by the PropertyDamageNumeric and CropDamageNumeric variables?
# Aggregate data based on category for economic impact
economic_impact_data <- stormdata %>%
group_by(category) %>%
summarise(PropertyDamage = sum(PropertyDamageNumeric, na.rm = TRUE),
CropDamage = sum(CropDamageNumeric, na.rm = TRUE)) %>%
mutate(TotalEconomicImpact = PropertyDamage + CropDamage) %>%
arrange(-TotalEconomicImpact)
# Plotting the top N categories by TotalEconomicImpact
N <- 10
top_economic_categories <- economic_impact_data[1:N,]
ggplot(top_economic_categories, aes(x=reorder(category, TotalEconomicImpact), y=TotalEconomicImpact, fill=TotalEconomicImpact)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title="Top N Storm Event Categories by Economic Impact",
x="Storm Event Category",
y="Total Economic Impact (Property + Crop Damage)") +
scale_fill_gradient(low="green", high="red") +
theme_minimal() +
theme(legend.position="none")
Results for economic impact of weather-related events are less clear cut than those for health. In this case, rain and flooding causes the most severe economic impact, followed by unclassified events (which, given more time, could be further pareled out into different categories). Wind, in terms of economic impact is the third, followed by hail and fire as approximately equally impactful.