library(rlang)
library(stargazer)
library(texreg)
library(lubridate)
library(tidyverse)
library(writexl)
#Steps: #Explore #Clean #Manipulate #Describe and summarise #Visualise #Analyse
data <- read.csv("/Users/marjo/Desktop/RStudio/repdata_data_StormData.csv")
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
names(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"
storm <- data %>%
select(c("EVTYPE","FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP")) %>%
rename_all(tolower)
str(storm)
## 'data.frame': 902297 obs. of 7 variables:
## $ evtype : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ 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 "" "" "" "" ...
#evtype : storm event type
#fatalities: amount of fatalities per event
#injuries : amount of injuries per event
#propdmg : property damage amount
#propdmgexp: property damage in exponents
#cropdmg : crop damage amount
#cropdmgexp: crop damage in exponents
storm$propdmgexp <- as.character(storm$propdmgexp)
storm$cropdmgexp <- as.character(storm$cropdmgexp)
first_analysis <-
storm %>% select(evtype, fatalities, injuries) %>%
group_by(evtype) %>%
summarize(fatalities = sum(fatalities), injuries = sum(injuries), .groups='drop') %>%
arrange(desc(fatalities), desc(injuries)) %>%
slice(1:10) %>%
gather(key = type, value = value, fatalities, injuries)
ggplot(data=first_analysis, aes(reorder(evtype, -value), value, fill=type)) +
geom_bar(position = "dodge", stat="identity", width=0.8) +
labs(x="Event Type", y="Count") +
theme_minimal() + # Clean and modern look
theme(axis.text.x = element_text(angle = 20, vjust=0.7, size=10, color="black"),
axis.text.y = element_text(size=10, color="black"),
plot.title = element_text(hjust = 0.5, size=14, color="black", face="bold"),
legend.position = "top",
legend.title = element_text(size=10, color="black"),
legend.text = element_text(size=10, color="black")) +
ggtitle("Total Number of Fatalities and Injuries") +
scale_fill_manual(values=c("steelblue", "seagreen")) # Custom color palette
#Function to calculate cost
cost <- function(x) {
if (x == "H")
1E-4
else if (x == "K")
1E-3
else if (x == "M")
1
else if (x == "B")
1E3
else
1-6
}
second_analysis <-
storm %>% select("evtype", "propdmg", "propdmgexp", "cropdmg", "cropdmgexp") %>%
mutate(prop_dmg = propdmg*sapply(propdmgexp, FUN = cost), crop_dmg = cropdmg*sapply(cropdmgexp, FUN = cost), .keep="unused") %>%
group_by(evtype) %>%
summarize(property = sum(prop_dmg), crop = sum(crop_dmg), .groups='drop') %>%
arrange(desc(property), desc(crop)) %>%
slice(1:10) %>%
gather(key = type, value = value, property, crop)
ggplot(data=second_analysis, aes(reorder(evtype, -value), value, fill=type)) +
geom_bar(position = "dodge", stat="identity", width=0.7) +
labs(x="Event Type", y="Count (millions)",
title="Total Cost of Property and Crop Damage") +
theme_minimal() + # Clean and modern look
theme(axis.text.x = element_text(angle=25, vjust=0.5, hjust=0.7, size=10, color="black"),
axis.text.y = element_text(size=10, color="black"),
plot.title = element_text(hjust=0.5, size=14, color="black", face="bold"),
legend.position = "top",
legend.title = element_text(size=10, color="black"),
legend.text = element_text(size=10, color="black")) +
scale_fill_manual(values=c("steelblue", "slategray")) # Custom color palette
#In light of the analysis, efforts should focus on enhancing safety and public health by investing in improved infrastructure and early warning systems to address tornado risks. Additionally, funding should be allocated to support innovation and the development of advanced systems and infrastructure for managing hurricanes and typhoons, with the aim of safeguarding properties and crops to minimize potential damage.