After analysing the dataset provided by the U.S. National Oceanic and Atmospheric Administration’s (NOAA), We can safely conclude Tornado are
library(dplyr)
library(ggplot2)
library(patchwork)
url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
file_path <- "StormData.csv.bz2" # Name for the downloaded file
download.file(url, destfile = file_path, mode = "wb")
# Read the CSV file
data <- read.csv(file_path)
head(data)
## 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
The file itself has 37 columns, we most likely will not need to exam all of these information so we will perform some EDA to select only relevant features ## 2.2. EDA
colnames(data)
## [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"
From the list of available features, we can identify “FATAILITIES” and “INJURIES” are directly related to public health while “PROPDMG” and “CROPDMG” are related to the economy. It seem damages can be so huge that they are also record in exponential term so we will also need “PROPDMGEXP” and “CROPDMGEXP” to fully measure the damages.
raw_data <- data[c("EVTYPE", "FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP")]
# Checking for the unique values of EVTYPE
unique_values <- unique(raw_data$EVTYPE)
num_unique_values <- length(unique_values)
We see that there are 985 different unique values for event type,
even though from the original document, there are only 48 types of
event. This variety most likely stemmed from typos and human error
during the recording process, so before continuing with the analysis, we
will need to clean the dataset and retain as much information as
possible.
## 2.3. Cleaning
First we just need to directly translate the letter on the 2 columns “CROPDMG”,“CROPDMGEXP” to the equivalent unit, namely: - K = 1,000 - M = 1,000,000 - B = 1,000,000,000
# Make a copy of data
ev_data <- raw_data
# Changing exponential values to numeric values
replace_chars <- function(x) {
x <- toupper(x) # Convert to uppercase for case-insensitive matching
ifelse(x == "K", 1000, ifelse(x == "M", 1000000, ifelse(x == "B", 1000000000, ifelse(x == "", 0, as.numeric(x)))))
}
# Replace characters in 'CROPDMGEXP' and 'PROPDMGEXP' columns
ev_data$CROPDMGEXP <- replace_chars(ev_data$CROPDMGEXP)
ev_data$PROPDMGEXP <- replace_chars(ev_data$PROPDMGEXP)
# Calculating damages
ev_data$ecom_dmg <- ev_data$PROPDMG*ev_data$PROPDMGEXP + ev_data$CROPDMG*ev_data$CROPDMGEXP
We will apply various approach in cleaning the text data in the EVTYPE, including trimming excess spaces in the string, turning all the text into lower case, manually group certain umbrella terms such as “Frost/Freeze” into the same group, following the instruction from the provided documents.
# List of accepted events
possible_values <- c("Astronomical Low Tide", "Avalanche", "Blizzard", "Coastal Flood",
"Cold Wind Chill", "Debris Flow", "Dense Fog", "Dense Smoke", "Drought",
"Dust Devil", "Dust Storm", "Excessive Heat", "Extreme Cold/Wind Chill",
"flood/flash flood", "Freezing Fog", "Frost/Freeze", "Funnel Cloud",
"Hail", "Heat", "Heavy Rain", "Heavy Snow", "High Surf",
"High Wind", "Hurricane/Typhoon", "Ice Storm",
"Lakeshore Flood", "Lake-Effect Snow", "Lightning", "Marine Hail",
"Marine High Wind", "Marine Strong Wind", "Marine Thunderstorm Wind",
"Rip Current", "Seiche", "Sleet", "Storm Tide", "Strong Wind",
"Thunderstorm Wind", "Tornado", "Tropical Depression",
"Tropical Storm", "Tsunami", "Volcanic Ash", "Waterspout",
"Wildfire", "Winter Storm", "Winter Weather")
# Turning all values into lower case
possible_values <- tolower(possible_values)
ev_data$EVTYPE <- tolower(ev_data$EVTYPE)
# Removing the space at start or trail of each text values
ev_data$EVTYPE <- trimws(ev_data$EVTYPE)
#Removing s trailing in all words
ev_data$EVTYPE <- sub("s$", "", ev_data$EVTYPE)
# For Marine Thunderstorm Wind the rows are denoted as TSTM so we need to changed them back
ev_data$EVTYPE[grepl("tstm", ev_data$EVTYPE, ignore.case = TRUE)] <- "marine thunderstorm wind"
# Since typhoon and hurricane are grouped into the same category so we will change these 2 into the same group
ev_data$EVTYPE[grepl("typhoon|hurricane", ev_data$EVTYPE, ignore.case = TRUE)] <- "hurricane/typhoon"
# Same logic apply to Extreme Cold/Wind Chill
ev_data$EVTYPE[grepl("extreme cold|wind chil", ev_data$EVTYPE, ignore.case = TRUE)] <- "extreme cold/wind chill"
# Grouping frost and freeze together but not including freezing fog
ev_data$EVTYPE[grepl("frost|freeze", ev_data$EVTYPE, ignore.case = TRUE)
& !grepl("freezing fog", ev_data$EVTYPE, ignore.case = TRUE)] <- "frost/freeze"
# Grouping flash flood and flood into the same category as just flood
ev_data$EVTYPE[grepl("flood|flash flood", ev_data$EVTYPE, ignore.case = TRUE)
& !grepl("lakeshore flood|coastal flood", ev_data$EVTYPE, ignore.case = TRUE)] <- "flood/flash flood"
#Removing spaces within all of the text string
ev_data$EVTYPE <- gsub("\\s", "", ev_data$EVTYPE)
possible_values <- gsub("\\s", "", possible_values)
# Function to check for matches
partial_match <- function(text) {
matches <- sapply(possible_values, function(pattern) grepl(pattern, text, ignore.case = TRUE))
matched_value <- ifelse(any(matches), possible_values[matches][1], NA)
return(matched_value)
}
# Applying the function to each row of EVTYPE column
ev_data$MATCHED_EVTYPE <- sapply(ev_data$EVTYPE, partial_match)
So now we should check if the data has been relatively clean and we are making use of most of the data By checking the remaining rows that have not been label on the MATCHED_EVTYPE column, we can see what are still left
# Filter out rows where 'MATCHED_EVTYPE' is NA
ev_data_filtered <- subset(ev_data, is.na(MATCHED_EVTYPE))
# Order the filtered data by 'ecom_dmg' column in descending order
ev_data_filtered <- ev_data_filtered[order(ev_data_filtered$ecom_dmg, decreasing = TRUE), ]
head(ev_data_filtered, 100)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG
## 577676 stormsurge 0 0 31.30 1e+09 0.0
## 581535 stormsurge 0 0 11.26 1e+09 0.0
## 739576 stormsurge/tide 11 0 4.00 1e+09 0.0
## 397334 wild/forestfire 0 0 1.50 1e+09 0.0
## 243445 severethunderstorm 0 0 1.20 1e+09 0.0
## 381995 wild/forestfire 0 0 547.00 1e+06 0.0
## 739574 stormsurge/tide 0 0 500.00 1e+06 0.0
## 577681 stormsurge 0 0 432.00 1e+06 0.0
## 314843 wild/forestfire 0 52 200.00 1e+06 0.0
## 314845 wild/forestfire 0 11 150.00 1e+06 0.0
## 199739 excessivewetnes 0 0 0.00 0e+00 142.0
## 345915 wild/forestfire 0 0 10.00 1e+06 80.0
## 711216 stormsurge/tide 0 0 75.00 1e+06 0.0
## 350780 wild/forestfire 0 0 66.90 1e+06 0.0
## 199738 coldandwetcondition 0 0 0.00 0e+00 66.0
## 567209 landslide 0 0 55.90 1e+06 0.0
## 195096 stormsurge 0 0 50.00 1e+06 0.0
## 196184 stormsurge 10 0 50.00 1e+06 0.0
## 209775 recordcold 0 0 50.00 1e+06 0.0
## 499600 stormsurge 0 0 50.00 1e+06 0.0
## 649615 landslide 0 0 48.00 1e+06 0.0
## 450901 wild/forestfire 0 0 45.70 1e+06 0.0
## 895597 stormsurge/tide 0 0 40.00 1e+06 0.0
## 251301 wild/forestfire 0 6 37.40 1e+06 0.0
## 606168 landslide 1 0 14.40 1e+06 20.0
## 314844 wild/forestfire 0 2 30.00 1e+06 0.0
## 566853 landslide 0 5 27.00 1e+06 0.0
## 283850 urban/smlstreamfld 0 0 25.00 1e+06 0.0
## 301162 unseasonablycold 0 0 0.00 0e+00 25.0
## 788703 landslide 0 0 25.00 1e+06 0.0
## 381970 wild/forestfire 0 0 22.70 1e+06 0.0
## 350833 wild/forestfire 0 4 22.20 1e+06 0.0
## 525598 stormsurge 0 0 19.90 1e+06 0.0
## 450661 wild/forestfire 0 19 16.00 1e+06 2.0
## 192397 severethunderstorm 0 0 500.00 1e+03 17.0
## 450941 wild/forestfire 0 14 15.30 1e+06 0.0
## 283184 wild/forestfire 0 0 15.00 1e+06 0.0
## 838987 landslide 0 0 15.00 1e+06 0.0
## 450883 wild/forestfire 0 0 14.30 1e+06 0.0
## 350817 wild/forestfire 0 4 13.30 1e+06 0.0
## 606190 landslide 0 0 13.00 1e+06 0.0
## 450919 wild/forestfire 0 14 12.70 1e+06 0.0
## 251297 wild/forestfire 0 0 12.30 1e+06 0.0
## 283108 wild/forestfire 0 0 2.00 1e+06 10.0
## 382438 wild/forestfire 0 0 12.00 1e+06 0.0
## 900549 landslide 0 0 12.00 1e+06 0.0
## 606180 landslide 0 0 11.50 1e+06 0.0
## 451422 wild/forestfire 0 0 11.00 1e+06 0.0
## 451488 wild/forestfire 0 0 11.00 1e+06 0.0
## 606209 landslide 0 0 11.00 1e+06 0.0
## 249579 wild/forestfire 0 0 10.00 1e+06 0.0
## 352051 wild/forestfire 0 0 10.00 1e+06 0.0
## 415954 wild/forestfire 0 2 10.00 1e+06 0.0
## 450900 wild/forestfire 0 38 10.00 1e+06 0.0
## 548513 landslide 0 0 10.00 1e+06 0.0
## 548564 landslide 0 0 10.00 1e+06 0.0
## 569089 stormsurge 0 0 10.00 1e+06 0.0
## 569084 stormsurge 0 0 9.50 1e+06 0.0
## 710340 stormsurge/tide 0 0 9.40 1e+06 0.0
## 529509 stormsurge 0 0 9.00 1e+06 0.0
## 489141 extremewindchill 0 0 0.00 0e+00 8.5
## 489146 extremewindchill 0 0 0.00 0e+00 8.5
## 569088 stormsurge 0 0 8.50 1e+06 0.0
## 747150 landslide 0 0 8.50 1e+06 0.0
## 451483 wild/forestfire 0 0 8.00 1e+06 0.0
## 529537 stormsurge 0 0 8.00 1e+06 0.0
## 569085 stormsurge 0 0 7.50 1e+06 0.0
## 569090 stormsurge 0 0 7.50 1e+06 0.0
## 606169 landslide 0 0 7.00 1e+06 0.0
## 416531 wild/forestfire 0 0 6.06 1e+06 0.0
## 363552 wild/forestfire 0 0 6.00 1e+06 0.0
## 451287 wild/forestfire 0 2 6.00 1e+06 0.0
## 451489 wild/forestfire 0 2 6.00 1e+06 0.0
## 312688 urban/smlstreamfld 0 0 0.00 0e+00 5.7
## 381885 wild/forestfire 0 0 0.00 0e+00 5.7
## 215085 forestfire 0 0 5.00 1e+06 500.0
## 251296 wild/forestfire 0 3 4.50 1e+06 1.0
## 473670 wild/forestfire 0 0 5.50 1e+06 0.0
## 256069 unseasonablecold 0 0 0.00 0e+00 5.1
## 194651 stormsurge 0 0 5.00 1e+06 0.0
## 194870 stormsurge 0 1 5.00 1e+06 0.0
## 195094 stormsurge 0 0 5.00 1e+06 0.0
## 195098 stormsurge 0 0 5.00 1e+06 0.0
## 195372 stormsurge 0 0 5.00 1e+06 0.0
## 195374 stormsurge 1 0 5.00 1e+06 0.0
## 197747 wind 0 1 5.00 1e+06 0.0
## 198147 snow 0 0 5.00 1e+06 0.0
## 198198 coolandwet 0 0 0.00 0e+00 5.0
## 208411 rain 0 0 5.00 1e+06 0.0
## 219161 freezingrain 0 0 5.00 1e+06 0.0
## 219163 recordcold 0 0 5.00 1e+06 0.0
## 219875 snow 1 1 5.00 1e+06 0.0
## 223355 iceandsnow 0 0 5.00 1e+06 0.0
## 237502 drymicroburst 0 0 5.00 1e+06 0.0
## 237631 severethunderstorm 0 0 5.00 1e+06 0.0
## 245866 ice 0 45 5.00 1e+06 0.0
## 283120 wild/forestfire 0 0 5.00 1e+06 0.0
## 312861 unseasonalrain 0 0 0.00 0e+00 5.0
## 350795 unseasonalrain 0 0 0.00 0e+00 5.0
## 363555 wild/forestfire 0 0 5.00 1e+06 0.0
## CROPDMGEXP ecom_dmg MATCHED_EVTYPE
## 577676 0e+00 3.130e+10 <NA>
## 581535 0e+00 1.126e+10 <NA>
## 739576 1e+03 4.000e+09 <NA>
## 397334 0e+00 1.500e+09 <NA>
## 243445 0e+00 1.200e+09 <NA>
## 381995 0e+00 5.470e+08 <NA>
## 739574 1e+03 5.000e+08 <NA>
## 577681 0e+00 4.320e+08 <NA>
## 314843 0e+00 2.000e+08 <NA>
## 314845 0e+00 1.500e+08 <NA>
## 199739 1e+06 1.420e+08 <NA>
## 345915 1e+06 9.000e+07 <NA>
## 711216 1e+03 7.500e+07 <NA>
## 350780 0e+00 6.690e+07 <NA>
## 199738 1e+06 6.600e+07 <NA>
## 567209 0e+00 5.590e+07 <NA>
## 195096 0e+00 5.000e+07 <NA>
## 196184 0e+00 5.000e+07 <NA>
## 209775 0e+00 5.000e+07 <NA>
## 499600 0e+00 5.000e+07 <NA>
## 649615 1e+03 4.800e+07 <NA>
## 450901 0e+00 4.570e+07 <NA>
## 895597 1e+03 4.000e+07 <NA>
## 251301 0e+00 3.740e+07 <NA>
## 606168 1e+06 3.440e+07 <NA>
## 314844 0e+00 3.000e+07 <NA>
## 566853 0e+00 2.700e+07 <NA>
## 283850 0e+00 2.500e+07 <NA>
## 301162 1e+06 2.500e+07 <NA>
## 788703 1e+03 2.500e+07 <NA>
## 381970 0e+00 2.270e+07 <NA>
## 350833 0e+00 2.220e+07 <NA>
## 525598 0e+00 1.990e+07 <NA>
## 450661 1e+06 1.800e+07 <NA>
## 192397 1e+06 1.750e+07 <NA>
## 450941 0e+00 1.530e+07 <NA>
## 283184 0e+00 1.500e+07 <NA>
## 838987 1e+03 1.500e+07 <NA>
## 450883 0e+00 1.430e+07 <NA>
## 350817 0e+00 1.330e+07 <NA>
## 606190 0e+00 1.300e+07 <NA>
## 450919 0e+00 1.270e+07 <NA>
## 251297 1e+03 1.230e+07 <NA>
## 283108 1e+06 1.200e+07 <NA>
## 382438 0e+00 1.200e+07 <NA>
## 900549 1e+03 1.200e+07 <NA>
## 606180 0e+00 1.150e+07 <NA>
## 451422 0e+00 1.100e+07 <NA>
## 451488 0e+00 1.100e+07 <NA>
## 606209 0e+00 1.100e+07 <NA>
## 249579 0e+00 1.000e+07 <NA>
## 352051 0e+00 1.000e+07 <NA>
## 415954 0e+00 1.000e+07 <NA>
## 450900 0e+00 1.000e+07 <NA>
## 548513 0e+00 1.000e+07 <NA>
## 548564 0e+00 1.000e+07 <NA>
## 569089 0e+00 1.000e+07 <NA>
## 569084 0e+00 9.500e+06 <NA>
## 710340 1e+03 9.400e+06 <NA>
## 529509 0e+00 9.000e+06 <NA>
## 489141 1e+06 8.500e+06 <NA>
## 489146 1e+06 8.500e+06 <NA>
## 569088 0e+00 8.500e+06 <NA>
## 747150 1e+03 8.500e+06 <NA>
## 451483 0e+00 8.000e+06 <NA>
## 529537 0e+00 8.000e+06 <NA>
## 569085 0e+00 7.500e+06 <NA>
## 569090 0e+00 7.500e+06 <NA>
## 606169 0e+00 7.000e+06 <NA>
## 416531 0e+00 6.060e+06 <NA>
## 363552 0e+00 6.000e+06 <NA>
## 451287 0e+00 6.000e+06 <NA>
## 451489 0e+00 6.000e+06 <NA>
## 312688 1e+06 5.700e+06 <NA>
## 381885 1e+06 5.700e+06 <NA>
## 215085 1e+03 5.500e+06 <NA>
## 251296 1e+06 5.500e+06 <NA>
## 473670 0e+00 5.500e+06 <NA>
## 256069 1e+06 5.100e+06 <NA>
## 194651 0e+00 5.000e+06 <NA>
## 194870 0e+00 5.000e+06 <NA>
## 195094 0e+00 5.000e+06 <NA>
## 195098 0e+00 5.000e+06 <NA>
## 195372 0e+00 5.000e+06 <NA>
## 195374 0e+00 5.000e+06 <NA>
## 197747 0e+00 5.000e+06 <NA>
## 198147 0e+00 5.000e+06 <NA>
## 198198 1e+06 5.000e+06 <NA>
## 208411 0e+00 5.000e+06 <NA>
## 219161 0e+00 5.000e+06 <NA>
## 219163 0e+00 5.000e+06 <NA>
## 219875 0e+00 5.000e+06 <NA>
## 223355 0e+00 5.000e+06 <NA>
## 237502 0e+00 5.000e+06 <NA>
## 237631 0e+00 5.000e+06 <NA>
## 245866 0e+00 5.000e+06 <NA>
## 283120 0e+00 5.000e+06 <NA>
## 312861 1e+06 5.000e+06 <NA>
## 350795 1e+06 5.000e+06 <NA>
## 363555 0e+00 5.000e+06 <NA>
# Display the filtered and ordered data frame
missed_row <- nrow(ev_data_filtered)
missed_row
## [1] 10876
Right away we see that there are 10876` items not accounted for yet, which is relatively small compare to the total of 902,297 rows, but some of the top items are values at billion and hundred millions, so we should look into them further to make use of as much information as possible. From visual evaluation, the top items seem to belong to a few groups, including forest fire, storm surge and landslide. By examining the provided documents, we know: (1) storm surge/tide are grouped in with “storm tide” (2) wild/forest fire are grouped in with “Wildfire” (3) landslide are grouped in with “debris flow”
# Calculate the percentage of missing data in term of fatalities, injuries and economical loss
sum_ev_data <- data.frame( FATALITIES= sum(ev_data$FATALITIES, na.rm = TRUE),
INJURIES = sum(ev_data$INJURIES, na.rm = TRUE),
ecom_dmg =sum(ev_data$ecom_dmg, na.rm = TRUE))
sum_ev_data_filtered <- data.frame(FATALITIES = sum(ev_data_filtered$FATALITIES, na.rm = TRUE),
INJURIES =sum(ev_data_filtered$INJURIES, na.rm = TRUE),
ecom_dmg =sum(ev_data_filtered$ecom_dmg, na.rm = TRUE))
# Create a comparison data frame
percentage_comparison <- data.frame(
FATALITIES = sum_ev_data_filtered["FATALITIES"] / sum_ev_data["FATALITIES"] * 100,
INJURIES = sum_ev_data_filtered["INJURIES"] / sum_ev_data["INJURIES"] * 100,
ecom_dmg = sum_ev_data_filtered["ecom_dmg"] / sum_ev_data["ecom_dmg"] * 100
)
# Calculating total missing records
missed_fatal <- percentage_comparison$FATALITIES
missed_injuries <- percentage_comparison$INJURIES
missed_ecom <- percentage_comparison$ecom_dmg
Just for a meaningful comparision, the left out items currently account for 3.02% of the fatalities, 1.74% of injuries and 11.16% of economical damages.
Now after we manually filling out some of the missing events
# Manually chaniging the top missing groups
ev_data$MATCHED_EVTYPE[grepl("stormsurge|stormtide", ev_data$EVTYPE, ignore.case = TRUE)] <- "stormtide"
# Since typhoon and hurricane are grouped into the same category so we will change these 2 into the same group
ev_data$MATCHED_EVTYPE[grepl("forestfire", ev_data$EVTYPE, ignore.case = TRUE)] <- "Wildfire"
# Grouping Landslide into the Debris Flow group
ev_data$MATCHED_EVTYPE[grepl("landslide", ev_data$EVTYPE, ignore.case = TRUE)] <- "debrisflow"
# Filter out rows where 'MATCHED_EVTYPE' is NA
ev_data_filtered <- subset(ev_data, is.na(MATCHED_EVTYPE))
# Calculating new fatalities/injuries/economical loss
sum_ev_data_filtered <- data.frame(FATALITIES = sum(ev_data_filtered$FATALITIES, na.rm = TRUE),
INJURIES =sum(ev_data_filtered$INJURIES, na.rm = TRUE),
ecom_dmg =sum(ev_data_filtered$ecom_dmg, na.rm = TRUE))
# Create a comparison data frame
percentage_comparison <- data.frame(
FATALITIES = sum_ev_data_filtered["FATALITIES"] / sum_ev_data["FATALITIES"] * 100,
INJURIES = sum_ev_data_filtered["INJURIES"] / sum_ev_data["INJURIES"] * 100,
ecom_dmg = sum_ev_data_filtered["ecom_dmg"] / sum_ev_data["ecom_dmg"] * 100
)
# Re-calculate
missed_fatal2 <- percentage_comparison$FATALITIES
missed_injuries2 <- percentage_comparison$INJURIES
missed_ecom2 <- percentage_comparison$ecom_dmg
Currently the missing values are at 2.52% of the fatalities, 1.28% of injuries and 0.36% of economical damages. Now that we have both cover the quality and quantity of the data, we caon proceed to perform analysis
# Copy the clean dataset and save to csv file
# Filter out rows where 'MATCHED_EVTYPE' is NA
clean_data <- ev_data[c("MATCHED_EVTYPE", "FATALITIES","INJURIES", "ecom_dmg")]
clean_data <- subset(clean_data, !is.na(MATCHED_EVTYPE))
write.csv(ev_data, "ev_data.csv", row.names = FALSE)
First we would like to examine the effect of natural disasters on human lives, i.e the number of injuries and fatalities caused by these events
# Aggregate FATALITIES and INJURIES by MATCHED_EVTYPE
aggregated_data <- clean_data %>%
group_by(MATCHED_EVTYPE) %>%
summarise(Total_Fatalities = sum(FATALITIES),
Total_Injuries = sum(INJURIES)) %>%
ungroup() %>%
mutate(Total_Affected = Total_Fatalities + Total_Injuries) %>%
arrange(desc(Total_Affected)) %>%
head(10)
# Create horizontal bar plot for FATALITIES (top 20)
plot_fatalities <- ggplot(aggregated_data[1:10, ], aes(x = Total_Fatalities, y = reorder(MATCHED_EVTYPE, Total_Fatalities))) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Top 20 FATALITIES",
x = "Total FATALITIES",
y = "Event Type") +
theme_minimal()
# Create horizontal bar plot for INJURIES (top 20)
plot_injuries <- ggplot(aggregated_data[1:10, ], aes(x = Total_Injuries, y = reorder(MATCHED_EVTYPE, Total_Injuries))) +
geom_bar(stat = "identity", fill = "salmon") +
labs(title = "Top 20 INJURIES",
x = "Total INJURIES",
y = "") +
theme_minimal()
# Arrange the plots side by side
combined_plots <- plot_fatalities + plot_injuries + plot_layout(ncol = 2)
# Display the combined plots
print(combined_plots)
We can see that Tornado overwhelmingly top both category and left the
runner-up by a landslide. But this can also because Tornado is the most
frequent event recorded, so we should also examine the average
casualties of these event for a more comprehensive understanding. Also
due to the overwhelming different between these events so we should also
opt for a log scale.
## FATALITIES
# Aggregating FATALITIES by natural event
aggregated_fatalities <- clean_data %>%
group_by(MATCHED_EVTYPE) %>%
summarise(Total_Fatalities = sum(FATALITIES),
Mean_Fatalities = mean(FATALITIES))
# Identifying the top EVTYPE in term of fatalities
top_total_fatality <- aggregated_fatalities$Total_Fatalities %in% tail(sort(aggregated_fatalities$Total_Fatalities), 4)
top_avg_fatality <- aggregated_fatalities$Mean_Fatalities %in% tail(sort(aggregated_fatalities$Mean_Fatalities), 4)
# Create a scatter plot
fatalities_scatter_plot <- ggplot(aggregated_fatalities, aes(x = Total_Fatalities, y = Mean_Fatalities)) +
geom_point(color = "blue") +
labs(title = "Total and Average Fatalities",
x = "Total Fatalities",
y = "Average Fatalities") +
theme_minimal() +
scale_x_log10() + # Set log scale for x-axis
scale_y_log10(limit = c(1e-03, 1e+2)) + # Set log scale for y-axis
geom_text(data = aggregated_fatalities[top_total_fatality | top_avg_fatality, ],
aes(label = MATCHED_EVTYPE), hjust = -0.1, vjust = -0.5)
## INJURIES
# Aggregating INJURIES by natural event
aggregated_injuries <- clean_data %>%
group_by(MATCHED_EVTYPE) %>%
summarise(Total_Injuries = sum(INJURIES),
Mean_Injuries = mean(INJURIES))
# Identifying the top EVTYPE in term of fatalities
top_total_injuries <- aggregated_injuries$Total_Injuries %in% tail(sort(aggregated_injuries$Total_Injuries), 4)
top_avg_injuries <- aggregated_injuries$Mean_Injuries %in% tail(sort(aggregated_injuries$Mean_Injuries), 4)
# Create a scatter plot
injuries_scatter_plot <- ggplot(aggregated_injuries, aes(x = Total_Injuries, y = Mean_Injuries)) +
geom_point(color = "blue") +
labs(title = "Total and Average Injuries",
x = "Total Injuries",
y = "Average Injuries") +
theme_minimal() +
scale_x_log10() + # Set log scale for x-axis
scale_y_log10(limit = c(1e-03, 1e+2)) + # Set log scale for y-axis
geom_text(data = aggregated_injuries[top_total_injuries | top_avg_injuries, ],
aes(label = MATCHED_EVTYPE), hjust = -0.1, vjust = -0.5)
# Arrange the plots side by side
combined_plots <- fatalities_scatter_plot + injuries_scatter_plot + plot_layout(nrow = 2)
# Display the combined plots
print(combined_plots)
So tornado is still in the leading position but we see some other
similar deady events such as tsunami, hurricane/typhoon, excessive
heat
# Aggregating total human and monetary loss
total_dmg <- clean_data %>%
group_by(MATCHED_EVTYPE) %>%
summarise(Total_Injuries = sum(INJURIES),
Total_Fatalities = sum(FATALITIES),
Total_Ecom = sum(ecom_dmg))
# Top ecom dmg
top_ecom <- total_dmg$Total_Ecom %in% tail(sort(total_dmg$Total_Ecom), 5)
scatter_plot <- ggplot(total_dmg, aes(x = Total_Fatalities, y = Total_Injuries, size = Total_Ecom)) +
geom_point(alpha = 0.7) + # Set transparency to 0.7 for better visibility of overlapping points
labs(title = "Total economical damages by EVTYPE",
x = "Total Fatalities",
y = "Total Injuries",
size = "Economic Damage") +
theme_minimal() +
scale_x_log10() + # Set log scale for x-axis
scale_y_log10() +
geom_text(data = total_dmg[top_ecom , ],
aes(label = MATCHED_EVTYPE), hjust = -0.1, vjust = -0.5)
# Display the scatter plot
print(scatter_plot)
Quite surprinsgly, tornado is not in the top 5 most economical destructive event, and we can visually see that hurricane/typhoon is number one, with storm surge/tide as closest second, with both distinctively more destructive then the rest