RequiredPackages = c("rlang"
                     ,"plyr"
                     ,"dplyr"
                     ,"tidyr"
                     ,"ggplot2"
                     ,"readr"
                     ,"kableExtra"
                     ,"knitr",
                     "rsconnect")

#This function performs a number of initializations:
# Load useful packages
# Empty variables
# Define directories of interest
# Import local functions of interest

if (!"install.load"%in%installed.packages()) {
  install.packages("install.load", repos = "https://cloud.r-project.org")
}
library(install.load)
install_load(RequiredPackages)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

#Synopsys The goal of this analysis is to give direct keys to understand what type of meterological evenement create the most harmful consequences for human health and economy in USA. The results will be presetend by mean and by sum.

#Data Analysis

The data come from storm database of the U.S. National Oceanic and Atmospheric Administration (NOAA). This database tracks the characteristics of major storms and weather events in the U.S., including when and where they occur, as well as estimates of the number of deaths, injuries and property damage.

##Dowload data

# dowload the data from my repertory
dataset<-  read.csv("C:/Users/Louis Hognon/Downloads/repdata_data_StormData.csv")

##Human health analysis

The first analysis is on the most harmful evenements in USA for human health. We considered therefore the number of injuries and fatalities by evenement. We created a new factor which gather these 2 parameters : injuries / fatalites

#Create a new column "healthproblem" which equals to the value to injuries + fatalities
dataset$heathproblem <- dataset$INJURIES+dataset$FATALITIES

#Aggregate the mean of healthproblem by each type of evenement
health_problem_mean<- aggregate(heathproblem ~EVTYPE , data = dataset, FUN = mean)

#Finding the most harmful evenement for human health by mean
most_desastrous_evenement_health_mean <- health_problem_mean$EVTYPE[which.max
                                                   (health_problem_mean$heathproblem)]


#Aggregate the sum of healthproblem by each type of evenement
health_problem_sum<- aggregate(heathproblem ~EVTYPE , data = dataset, FUN = sum)


#Finding the most harmful evenement for human health in total
most_desastrous_evenement_health_sum <- health_problem_sum$EVTYPE[which.max
                                                   (health_problem_sum$heathproblem)]
#Figure for health problem by mean
# finding the top 10 of evenement in health_problem_mean
health_problem_top10 <- health_problem_mean %>%
      group_by(EVTYPE) %>%
      summarise(total_health_problems = sum(heathproblem, na.rm = TRUE)) %>%
      top_n(10, total_health_problems)

# Arrange the data
health_problem_top10 <- health_problem_top10 %>%
      arrange(desc(total_health_problems))

# Define a degrowth color 
color_palette <- scales::viridis_pal(direction = -1)(10)

# Create the figure
figure1 <- ggplot(health_problem_top10, aes(x = reorder(EVTYPE, -total_health_problems), y = total_health_problems, fill = total_health_problems)) +
      geom_bar(stat = "identity") +
      scale_fill_gradient(low = color_palette[1], high = color_palette[10], guide = "legend") +
      labs(title = "Top 10 evenement whith the most harmful impact for human health in mean",
           x = "Type of evenement",
           y = "Mean of health problem") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))



#Figure for health problem by sum
health_problem_top10 <- health_problem_sum %>%
      group_by(EVTYPE) %>%
      summarise(total_health_problems = sum(heathproblem, na.rm = TRUE)) %>%
      top_n(10, total_health_problems)

# Arrange the data
health_problem_top10 <- health_problem_top10 %>%
      arrange(desc(total_health_problems))

# Define a degrowth color 
color_palette <- scales::viridis_pal(direction = -1)(10)

# Create the figure
figure2 <- ggplot(health_problem_top10, aes(x = reorder(EVTYPE, -total_health_problems), y = total_health_problems, fill = total_health_problems)) +
      geom_bar(stat = "identity") +
      scale_fill_gradient(low = color_palette[1], high = color_palette[10], guide = "legend") +
      labs(title = "Top 10 evenement whith the most harmful impact for human health in total",
           x = "Type of evenement",
           y = "Sum of health problem") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

Economy analysis

In order to analyse properly the economy impact of the evenement, we calculated the right impact of each evenement. Therefore we multipliate each prop or crop amont of money by his right factor (k=1000\(, m=1000000\), b=1000 000 000$)

#create a new factor to calculate the right amount of money for prop
dataset <- dataset %>%
      mutate(
            propeco = case_when(
                  PROPDMGEXP == "K" ~ PROPDMG * 1000,
                  PROPDMGEXP == "M" ~ PROPDMG * 1000000,
                  PROPDMGEXP == "B" ~ PROPDMG * 1000000000,
                  TRUE ~ PROPDMG
            )
      )

#create a new factor to calculate the right amount of money for crop
dataset <- dataset %>%
      mutate(
            cropeco = case_when(
                  CROPDMGEXP == "K" ~ CROPDMG  * 1000,
                  CROPDMGEXP == "M" ~ CROPDMG  * 1000000,
                  CROPDMGEXP == "B" ~ CROPDMG  * 1000000000,
                  TRUE ~ CROPDMG 
            )
      )

#Creating new factor which additionate prop + crop 
dataset$ecotot <- dataset$propeco + dataset$cropeco


#Aggregate the mean of ecotot by each type of evenement
eco_problem_mean<- aggregate(ecotot ~EVTYPE , data = dataset, FUN = mean)

#Finding the most harmful evenement for economy by mean
most_desastrous_evenement_eco_mean <- eco_problem_mean$EVTYPE[which.max
                                                   (eco_problem_mean$ecotot)]


#Aggregate the sum of ecotot by each type of evenement
eco_problem_sum<- aggregate(ecotot ~EVTYPE , data = dataset, FUN = sum)


#Finding the most harmful evenement for economy in total
most_desastrous_evenement_eco_sum <- eco_problem_sum$EVTYPE[which.max
                                                   (eco_problem_sum$ecotot)]
#Figue for mean economy problem
eco_problem_top10 <- eco_problem_mean%>%
      group_by(EVTYPE) %>%
      summarise(total_eco_problems = sum(ecotot, na.rm = TRUE)) %>%
      top_n(10, total_eco_problems)

# Arrange the data
eco_problem_top10 <- eco_problem_top10 %>%
      arrange(desc(total_eco_problems))

# Define a degrowth color 
color_palette <- scales::viridis_pal(direction = -1)(10)

# Create the figure
figure3 <- ggplot(eco_problem_top10, aes(x = reorder(EVTYPE, -total_eco_problems), y = total_eco_problems, fill = total_eco_problems)) +
      geom_bar(stat = "identity") +
      scale_fill_gradient(low = color_palette[1], high = color_palette[10], guide = "legend") +
      labs(title = "Top 10 evenement whith the most harmful impact for human eco in mean",
           x = "Type of evenement",
           y = "Mean of eco problem") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))



#Figure for total economy problem 
eco_problem_top10 <- eco_problem_sum%>%
      group_by(EVTYPE) %>%
      summarise(total_eco_problems = sum(ecotot, na.rm = TRUE)) %>%
      top_n(10, total_eco_problems)

# Arrange the data
eco_problem_top10 <- eco_problem_top10 %>%
      arrange(desc(total_eco_problems))

# Define a degrowth color 
color_palette <- scales::viridis_pal(direction = -1)(10)

# Create the figure
figure4 <- ggplot(eco_problem_top10, aes(x = reorder(EVTYPE, -total_eco_problems), y = total_eco_problems, fill = total_eco_problems)) +
      geom_bar(stat = "identity") +
      scale_fill_gradient(low = color_palette[1], high = color_palette[10], guide = "legend") +
      labs(title = "Top 10 evenement whith the most harmful impact for human eco in total",
           x = "Type of evenement",
           y = "Sum of eco problem") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

#Results

##Results concerning the impact of the evenement for human health

cat("In mean, the most harmful evenement for human health in USA is", most_desastrous_evenement_health_mean,"with", max(health_problem_mean$heathproblem),"injuries or fatalities" )
## In mean, the most harmful evenement for human health in USA is Heat Wave with 70 injuries or fatalities
print(figure1)

cat("In total, the most harmful evenement for human health in USA is", most_desastrous_evenement_health_sum,"with", max(health_problem_sum$heathproblem),"injuries or fatalities" )
## In total, the most harmful evenement for human health in USA is TORNADO with 96979 injuries or fatalities
print(figure2)

##Results concerning the impact of the evenement for economy

cat("In mean, the most harmful evenement for economy in USA is", most_desastrous_evenement_eco_mean,"with", max(eco_problem_mean$ecotot),"dollar","\n")
## In mean, the most harmful evenement for economy in USA is TORNADOES, TSTM WIND, HAIL with 1602500000 dollar
print(figure3)

cat("In mean, the most harmful evenement for economy in USA is", most_desastrous_evenement_eco_sum,"with", max(eco_problem_sum$ecotot),"dollar","\n" )
## In mean, the most harmful evenement for economy in USA is FLOOD with 150319678257 dollar
print(figure4)