The following r libraries were used to perform the analysis:
“dplyr”
“tidyr”
“lubridate”
“ggplot2”
“plotly”
“grid”
“gridExtra”
“lemon”
Here is the location of the data source needed (previously provided by the assignment instruction page)
“/Users/lubravo19/Documents/Labinfo/Cousera trainings/Reproducible Research/Assignment week4”
The comma separated was extracted from the provided “zip” file
repdata_data_StormData.csv.bz2
The extraction focused on the columns : “BGN_DATE”,“EVTYPE”,“FATALITIES”,“INJURIES”,“PROPDMG”,“PROPDMGEXP”,“CROPDMG”,“CROPDMGEXP” so only the data relevant for the analysis was loaded.
The data content is uploaded into the dataframe named “noaaevs” and ready to be processed
Here is the code used:
noaaevs<-read.csv(“repdata_data_StormData.csv.bz2”)[,c(“BGN_DATE”,“EVTYPE”,“FATALITIES”,“INJURIES”,“PROPDMG”,“PROPDMGEXP”,“CROPDMG”,“CROPDMGEXP”)] An additional column was added “Date2” to convert date string values to r date types
noaaevs$Date2<-as.Date(noaaevs$BGN_DATE,“%m/%d/%Y %H:%M:%S”)
The first part of the analysis was meant to show how the population’s health is affected by weather events. The population’s health is defined by the number of fatalities and injuries inflicted across the United States territory
harmbyev<-aggregate(FATALITIES + INJURIES ~ EVTYPE, data = noaaevs, FUN = sum)
Here are the first rows of the first data aggregation:
| EVTYPE | FATALITIES + INJURIES | |
|---|---|---|
| 18 | AVALANCE | 1 |
| 19 | AVALANCHE | 394 |
| 29 | BLACK ICE | 25 |
| 30 | BLIZZARD | 906 |
| 42 | blowing snow | 2 |
| 44 | BLOWING SNOW | 14 |
r code : head(subset(harmbyev,
harmbyev$FATALITIES + INJURIES > 0)) In order to find
the most harmful events, I only used the entries with the values above
the fatalities and injuries average count.
| EVTYPE | FATALITIES + INJURIES | |
|---|---|---|
| 19 | AVALANCHE | 394 |
| 30 | BLIZZARD | 906 |
| 89 | DENSE FOG | 360 |
| 117 | DUST STORM | 462 |
| 130 | EXCESSIVE HEAT | 8428 |
| 140 | EXTREME COLD | 391 |
r code:
avgFTINJ<-mean(harmbyev$FATALITIES + INJURIES)
harmbyevnz<-subset(harmbyev, FATALITIES + INJURIES >
avgFTINJ)
And finally I used the “upper fence” statistical value to show the outlier events that have impacted the US population
| EVTYPE | FATALITIES + INJURIES | |
|---|---|---|
| 130 | EXCESSIVE HEAT | 8428 |
| 170 | FLOOD | 7259 |
| 464 | LIGHTNING | 6046 |
| 834 | TORNADO | 96979 |
| 856 | TSTM WIND | 7461 |
r code:
qs<-quantile(harmbyevnz$FATALITIES + INJURIES, probs=
seq(0,1,1/4)) ## Obtain 3rd Quantile q3<-qs[4] q1<-qs[2]
uppfn<-q3 + (1.5*(q3 - q1)) ## subset only upper outliers
harmbyevnz<-subset(harmbyevnz,FATALITIES + INJURIES
>= uppfn)
Here are the plot analysis by events and through the data collection period
r code:
evvas<-harmbyevnz$EVTYPE noaevharms<-subset(noaaevs,EVTYPE %in%
evvas) harmplot<-ggplot(data=noaevharms,aes(y =(FATALITIES +
INJURIES),x = Date2)) + geom_point() + aes(color = EVTYPE, group =1) +
labs(y=“Number of Fatalities and Injuries”,x=“Period : Jan 3 1950 - Nov
30 2011”) outliers1<-ggplot(data=harmbyevnz,aes(y =
FATALITIES + INJURIES,x = EVTYPE)) + geom_point() +
labs(y=“Number of Fatalities and Injuries”,x=“Weather Events”)
blankpan<- grid.rect(gp=gpar(col=“white”))
evalplot<-grid.arrange(outliers1,blankpan,harmplot, nrow = 1, widths = c(3,1/3,4))
The second part of the analysis shows the greatest economic consequences inflicted by the weather events. The economic consequences is defined by the impact on property and crop damage across the United States Territory.
r code:
damgan<-noaaevs%>%select(“Date2”,“PROPDMG”,“PROPDMGEXP”,“CROPDMG”,“CROPDMGEXP”,“EVTYPE”) damgan<-subset(subset(damgan,PROPDMGEXP %in% c(“K”,“M”,“B”)),CROPDMGEXP %in% c(“M”,“K”,“m”,“B”,“k”)) damgan <- damgan %>% mutate(Propdmgrn = case_when ( PROPDMGEXP == “B” ~ (PROPDMG * 1e9 ), PROPDMGEXP == “M” ~ (PROPDMG * 1e6 ), PROPDMGEXP == “K” ~ (PROPDMG * 1e3 ) ))
damgan <- damgan %>% mutate(Cropdmgrn = case_when ( CROPDMGEXP == “B” ~ (CROPDMG * 1e9 ), CROPDMGEXP == “M” ~ (CROPDMG * 1e6 ), CROPDMGEXP == “m” ~ (CROPDMG * 1e6 ), CROPDMGEXP == “K” ~ (CROPDMG * 1e3 ), CROPDMGEXP == “k” ~ (CROPDMG * 1e3 ) ))
damgsel<-aggregate(Propdmgrn + Cropdmgrn ~ EVTYPE, data = damgan, FUN = sum) Here are the first rows of the first data aggregation after cleaning the data and calculting the aggregate cost of the damages :
| EVTYPE | Propdmgrn + Cropdmgrn |
|---|---|
| ASTRONOMICAL HIGH TIDE | 5000 |
| ASTRONOMICAL LOW TIDE | 320000 |
| AVALANCHE | 2385800 |
| BLIZZARD | 207041000 |
| COASTAL FLOOD | 167580560 |
| COASTAL FLOODING | 25356000 |
In order to find the most harmful events, I only used the entries
with the values above the average damage cost. r code:
avgDmg<-mean(damgsel$Propdmgrn + Cropdmgrn)
damgabvavg<-subset(damgsel, Propdmgrn + Cropdmgrn
> avgDmg)
| EVTYPE | Propdmgrn + Cropdmgrn | |
|---|---|---|
| 19 | FLASH FLOOD | 8715295130 |
| 23 | FLOOD | 138007444500 |
| 37 | HAIL | 10020591590 |
| 52 | HIGH WIND | 3057666640 |
| 56 | HURRICANE | 12405268000 |
| 59 | HURRICANE OPAL | 2157000000 |
And finally I used the “upper fence” statistical value to show the
outlier events that have impacted the US population r
code:qs<-quantile(damgabvavg$Propdmgrn + Cropdmgrn,
probs= seq(0,1,1/4)) ## Obtain 3rd Quantile q3<-qs[4] q1<-qs[2]
uppfn<-q3 + (1.5*(q3 - q1)) ## subset only upper outliers
damgabvavg<-subset(damgabvavg, Propdmgrn + Cropdmgrn
>= uppfn)
| EVTYPE | Propdmgrn + Cropdmgrn | |
|---|---|---|
| 23 | FLOOD | 138007444500 |
| 61 | HURRICANE/TYPHOON | 29348167800 |
Here are the plot analysis by events and through the data collection period
rcode:
outliers<-ggplot(data=damgabvavg,aes(y =
Propdmgrn + Cropdmgrn,x = EVTYPE)) + geom_point() +
labs(y=“Cost of damage (US Dollars)”,x=“Weather Event”)
evvasdmg<-damgabvavg$EVTYPE damgandmg<-subset(damgan,EVTYPE %in%
evvasdmg)
dmgplot<-ggplot(data=damgandmg,aes(y =(Propdmgrn + Cropdmgrn),x = Date2)) + geom_point() + aes(color = EVTYPE, group =1) + labs(y=“Cost of damage (US Dollars)”,x=“Period : Jan 3 1950 - Nov 30 2011”) dmgalplot<-grid.arrange(outliers,blankpan,dmgplot,nrow = 1,widths = c(4,1,6))