This is a project of course Reproducible Research is a part of a specialization Data Science and this is an analysis about the economic impact and population healthy caused by whether events, the data used in this work was obtain from NOAA data base.
The analysis will answer two questions required by the course:
Across the United States, which types of events are most harmful with respect to population health?
Across the United States, which types of events have the greatest economic consequences?
This section will describe how the data was processed to obtain the answer to the questions
First load libraries and obtain data from the csv file.
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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.4
## ✔ ggplot2 3.4.2 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.3.3
#read data
df <-read.csv("repdata_data_StormData.csv.bz2", header = TRUE, sep = ",")
head(df)
## 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
dim(df)
## [1] 902297 37
str(df)
## '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 ...
Then select the data to use in the analysis
harm <- select(df, EVTYPE, FATALITIES, INJURIES)
str(harm)
## 'data.frame': 902297 obs. of 3 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 ...
head(harm)
## EVTYPE FATALITIES INJURIES
## 1 TORNADO 0 15
## 2 TORNADO 0 0
## 3 TORNADO 0 2
## 4 TORNADO 0 2
## 5 TORNADO 0 2
## 6 TORNADO 0 6
dim(harm)
## [1] 902297 3
Summaries the data to obtain the total of injuries and fatalities by each event and reduce the numbers of data to have only the more significant.
df1 <- harm %>%
group_by(EVTYPE) %>%
summarise(Fatal = sum(FATALITIES), Injuries =sum(INJURIES), na.rm =TRUE)
df2 <- df1[order(-df1$Fatal),]
head(df2)
## # A tibble: 6 × 4
## EVTYPE Fatal Injuries na.rm
## <chr> <dbl> <dbl> <lgl>
## 1 TORNADO 5633 91346 TRUE
## 2 EXCESSIVE HEAT 1903 6525 TRUE
## 3 FLASH FLOOD 978 1777 TRUE
## 4 HEAT 937 2100 TRUE
## 5 LIGHTNING 816 5230 TRUE
## 6 TSTM WIND 504 6957 TRUE
df3 <- df2[1:10,]
Obtain data to economic analysis
eco <- select(df, EVTYPE, PROPDMG,PROPDMGEXP,CROPDMG,CROPDMGEXP)
str(eco)
## 'data.frame': 902297 obs. of 5 variables:
## $ EVTYPE : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ 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 "" "" "" "" ...
head(eco)
## EVTYPE PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 25.0 K 0
## 2 TORNADO 2.5 K 0
## 3 TORNADO 25.0 K 0
## 4 TORNADO 2.5 K 0
## 5 TORNADO 2.5 K 0
## 6 TORNADO 2.5 K 0
Assign a value of multiples K, M , B, and some variables in minus to the multiple per
eco$PROPDMGEXP <- str_replace(eco$PROPDMGEXP, 'K', '1000')
eco$PROPDMGEXP <- str_replace(eco$PROPDMGEXP, 'M', '1000000')
eco$PROPDMGEXP <- str_replace(eco$PROPDMGEXP, 'm', '1000000')
eco$PROPDMGEXP <- str_replace(eco$PROPDMGEXP, 'B', '1000000000')
eco$CROPDMGEXP <- str_replace(eco$CROPDMGEXP, 'K', '1000')
eco$CROPDMGEXP <- str_replace(eco$CROPDMGEXP, 'k', '1000')
eco$CROPDMGEXP <- str_replace(eco$CROPDMGEXP, 'M', '1000000')
eco$CROPDMGEXP <- str_replace(eco$CROPDMGEXP, 'm', '1000000')
eco$CROPDMGEXP <- str_replace(eco$CROPDMGEXP, 'B', '1000000000')
eco$PROPDMG <- eco$PROPDMG * as.numeric(eco$PROPDMGEXP)
## Warning: NAs introducidos por coerción
eco$CROPDMG <- eco$CROPDMG * as.numeric(eco$CROPDMGEXP)
## Warning: NAs introducidos por coerción
head(eco)
## EVTYPE PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 25000 1000 NA
## 2 TORNADO 2500 1000 NA
## 3 TORNADO 25000 1000 NA
## 4 TORNADO 2500 1000 NA
## 5 TORNADO 2500 1000 NA
## 6 TORNADO 2500 1000 NA
Summaries the data to obtain the total damage by each event and reduce the numbers of data to have only the more significant.
eco_2 <- select(eco, EVTYPE, PROPDMG, CROPDMG)
eco_3 <- eco_2 %>%
group_by(EVTYPE) %>%
summarise(Pro_dmg = sum(PROPDMG), Crop_dmg =sum(CROPDMG), na.rm =TRUE)
Order damage by Crop and Property
#Property damage
eco3 <- eco_3[order(-eco_3$Pro_dmg),]
head(eco3)
## # A tibble: 6 × 4
## EVTYPE Pro_dmg Crop_dmg na.rm
## <chr> <dbl> <dbl> <lgl>
## 1 TORNADOES, TSTM WIND, HAIL 1600000000 2500000 TRUE
## 2 WILD FIRES 624100000 NA TRUE
## 3 HAILSTORM 241000000 NA TRUE
## 4 HIGH WINDS/COLD 110500000 7000000 TRUE
## 5 River Flooding 106155000 NA TRUE
## 6 MAJOR FLOOD 105000000 NA TRUE
eco3 <- eco3[1:10,]
#Crop damage
eco4 <- eco_3[order(-eco_3$Crop_dmg),]
head(eco4)
## # A tibble: 6 × 4
## EVTYPE Pro_dmg Crop_dmg na.rm
## <chr> <dbl> <dbl> <lgl>
## 1 EXCESSIVE WETNESS NA 142000000 TRUE
## 2 COLD AND WET CONDITIONS NA 66000000 TRUE
## 3 Early Frost NA 42000000 TRUE
## 4 Damaging Freeze NA 34130000 TRUE
## 5 Freeze NA 10500000 TRUE
## 6 HURRICANE OPAL/HIGH WINDS 100000000 10000000 TRUE
eco4 <- eco4[1:10,]
view(eco3)
Inj <- ggplot(data = df3, aes( x = reorder(EVTYPE, -Injuries), y =Injuries, fill = reorder(EVTYPE, -Fatal)))+
geom_bar(position = "stack", stat = "identity")+
theme(axis.text = element_text(angle = 45, hjust = 1))+
labs(x= "Event")+
labs(title = "Injuries by Event")+
labs(fill = "Event")
fatal<- ggplot(data = df3, aes( x = reorder(EVTYPE, -Fatal), y =Fatal, fill = reorder(EVTYPE, -Fatal)))+
geom_bar(position = "stack", stat = "identity")+
theme(axis.text = element_text(angle = 45, hjust = 1))+
labs(x= "Event")+
labs(title = "Fatalities by Event")+
labs(fill = "Event")
ggarrange(Inj, fatal, ncol =1, nrow = 2, align = 'h')
With the two bar plot we can how _TORNADO__ is the event that cause more fatalities and more injuries but the others events don’t follow the same order, for example EXCESSIVE HEAT is in second place in fatalities but fourth place in injuries
plot_prop <- ggplot(data = eco3, aes(x = EVTYPE, y= Pro_dmg, fill = EVTYPE))+
geom_bar(position = "stack", stat = "identity")+
theme(axis.text = element_text(angle = 45, hjust = 1))+
labs(x= "Event")+
labs(title = "Property damage")+
labs(fill = "Event")
plot_crop <- ggplot(data = eco4, aes(x = EVTYPE, y= Crop_dmg, fill = EVTYPE))+
geom_bar(position = "stack", stat = "identity")+
theme(axis.text = element_text(angle = 45, hjust = 1))+
labs(x= "Event")+
labs(title = "Crop damage")+
labs(fill = "Event")
ggarrange(plot_crop, plot_prop, ncol =1, nrow = 2, align = 'h')
Here we can see a difference between crop and property damage caused by a weather event, more variation than healthy impact (injuries an fatalities) property damage has more economic impact and by the same event that cause more healthy impact, in other case crop impact the damage is less and by other events.