Introduction*

“Storms and other severe weather events can cause both public health and economic problems for communities and municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.

This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage."

*As explained on the assignment

Sinopsys

The purpose of this project is to provide answers to the following questions:

1.Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?

2.Across the United States, which types of events have the greatest economic consequences?

The dataset was downloaded from NOAA site and it has been segregated on a new data set with variables required to answer these questions. From this dataset the variables related to population health( injuries and fatalities) have been tallied up to produce a summary by event type. Consequently, the casualties (injuries+fatalities) were sorted in descending order to find out the top offending events.

A similar approach was applied to the events that have the greatest economic consequences (property damage and crop damage). These variables were expanded to derive the total damage cost per event type. The property and crop damage cost were added up to produce the total damage cost and aggregated per event type. The total cost was sorted in descending order to determine the top events with the greatest economic impact.

Data Processing

Load Libraries

# Load libraries
library(lattice)
library(ggplot2)
library(car)
## Warning: package 'car' was built under R version 3.2.5
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.2.5

Download and open the compressed file from NOAA site

setwd("~/Coursera/DataScience/Reproducible Research/Week 4 Assignment")

zipFile <- "./repdata%2Fdata%2FStormData.csv.bz2"

if (!file.exists(zipFile)){
    fileURL <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
    download.file(fileURL, zipFile, mode="wb")
} 

if (file.exists(zipFile)) {
    storm <- read.csv(zipFile) 
}

# Verify data was loaded properly
head(storm)
##   STATE__           BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE
## 1       1  4/18/1950 0:00:00     0130       CST     97     MOBILE    AL
## 2       1  4/18/1950 0:00:00     0145       CST      3    BALDWIN    AL
## 3       1  2/20/1951 0:00:00     1600       CST     57    FAYETTE    AL
## 4       1   6/8/1951 0:00:00     0900       CST     89    MADISON    AL
## 5       1 11/15/1951 0:00:00     1500       CST     43    CULLMAN    AL
## 6       1 11/15/1951 0:00:00     2000       CST     77 LAUDERDALE    AL
##    EVTYPE BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END
## 1 TORNADO         0                                               0
## 2 TORNADO         0                                               0
## 3 TORNADO         0                                               0
## 4 TORNADO         0                                               0
## 5 TORNADO         0                                               0
## 6 TORNADO         0                                               0
##   COUNTYENDN END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES
## 1         NA         0                      14.0   100 3   0          0
## 2         NA         0                       2.0   150 2   0          0
## 3         NA         0                       0.1   123 2   0          0
## 4         NA         0                       0.0   100 2   0          0
## 5         NA         0                       0.0   150 2   0          0
## 6         NA         0                       1.5   177 2   0          0
##   INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES
## 1       15    25.0          K       0                                    
## 2        0     2.5          K       0                                    
## 3        2    25.0          K       0                                    
## 4        2     2.5          K       0                                    
## 5        2     2.5          K       0                                    
## 6        6     2.5          K       0                                    
##   LATITUDE LONGITUDE LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1     3040      8812       3051       8806              1
## 2     3042      8755          0          0              2
## 3     3340      8742          0          0              3
## 4     3458      8626          0          0              4
## 5     3412      8642          0          0              5
## 6     3450      8748          0          0              6
dim(storm)
## [1] 902297     37
str(storm)
## 'data.frame':    902297 obs. of  37 variables:
##  $ STATE__   : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ BGN_DATE  : Factor w/ 16335 levels "1/1/1966 0:00:00",..: 6523 6523 4242 11116 2224 2224 2260 383 3980 3980 ...
##  $ BGN_TIME  : Factor w/ 3608 levels "00:00:00 AM",..: 272 287 2705 1683 2584 3186 242 1683 3186 3186 ...
##  $ TIME_ZONE : Factor w/ 22 levels "ADT","AKS","AST",..: 7 7 7 7 7 7 7 7 7 7 ...
##  $ COUNTY    : num  97 3 57 89 43 77 9 123 125 57 ...
##  $ COUNTYNAME: Factor w/ 29601 levels "","5NM E OF MACKINAC BRIDGE TO PRESQUE ISLE LT MI",..: 13513 1873 4598 10592 4372 10094 1973 23873 24418 4598 ...
##  $ STATE     : Factor w/ 72 levels "AK","AL","AM",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ EVTYPE    : Factor w/ 985 levels "   HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
##  $ BGN_RANGE : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ BGN_AZI   : Factor w/ 35 levels "","  N"," NW",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ BGN_LOCATI: Factor w/ 54429 levels "","- 1 N Albion",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ END_DATE  : Factor w/ 6663 levels "","1/1/1993 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ END_TIME  : Factor w/ 3647 levels ""," 0900CST",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ 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   : Factor w/ 24 levels "","E","ENE","ESE",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ END_LOCATI: Factor w/ 34506 levels "","- .5 NNW",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ 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: Factor w/ 19 levels "","-","?","+",..: 17 17 17 17 17 17 17 17 17 17 ...
##  $ CROPDMG   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ CROPDMGEXP: Factor w/ 9 levels "","?","0","2",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ WFO       : Factor w/ 542 levels ""," CI","$AC",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ STATEOFFIC: Factor w/ 250 levels "","ALABAMA, Central",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ ZONENAMES : Factor w/ 25112 levels "","                                                                                                                               "| __truncated__,..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ 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   : Factor w/ 436781 levels "","-2 at Deer Park\n",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ REFNUM    : num  1 2 3 4 5 6 7 8 9 10 ...

Analysis

1.Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?

#Create a dataset with the relevant variables to answers the questions

stormData <- subset(storm,select=c(EVTYPE,FATALITIES,INJURIES, PROPDMG, CROPDMG, PROPDMGEXP, CROPDMGEXP))

# Add variable for population health issues and include as new variable in the data set.
stormData$HUMCAS <- stormData$INJURIES + stormData$FATALITIES

# Aggregate injuries, fatalities and casualties by event type
sumCasualties <- aggregate(x = list(injuries = stormData$INJURIES,  
                           fatalities=stormData$FATALITIES,
                           casualties=stormData$HUMCAS), 
                           by = list(eventType = stormData$EVTYPE), 
                           FUN = sum, na.rm = TRUE)

# Provide summary per variable to have an idea of the data distribution
summary(sumCasualties)
##                  eventType      injuries         fatalities     
##     HIGH SURF ADVISORY:  1   Min.   :    0.0   Min.   :   0.00  
##   COASTAL FLOOD       :  1   1st Qu.:    0.0   1st Qu.:   0.00  
##   FLASH FLOOD         :  1   Median :    0.0   Median :   0.00  
##   LIGHTNING           :  1   Mean   :  142.7   Mean   :  15.38  
##   TSTM WIND           :  1   3rd Qu.:    0.0   3rd Qu.:   0.00  
##   TSTM WIND (G45)     :  1   Max.   :91346.0   Max.   :5633.00  
##  (Other)              :979                                      
##    casualties   
##  Min.   :    0  
##  1st Qu.:    0  
##  Median :    0  
##  Mean   :  158  
##  3rd Qu.:    0  
##  Max.   :96979  
## 
#calculate top events based on injuries

top10Inj <- head(sumCasualties[order(sumCasualties[2],decreasing="TRUE"),],10)

#calculate top events based on fatalities

top10Fatal <- head(sumCasualties[order(sumCasualties[3],decreasing="TRUE"),],10)

# Plot with events based on injuries and fatalities

injPlot <- xyplot(injuries~eventType,data=top10Inj,type="h",lw=5,scales=list(x=list(rot=60)),
                  main ="Injuries per Event Type")

fatalPlot <- xyplot(fatalities~eventType,data=top10Fatal,type="h",lw=5,scales=list(x=list(rot=60)),main ="Fatalities per Event Type")

grid.arrange(injPlot,fatalPlot,ncol=2)

#calculate and plot top events based on casualties =injuries + fatalities

top10Casualties <- head(sumCasualties[order(sumCasualties[4],decreasing="TRUE"),],10)

casualtyChart <-xyplot(casualties~eventType,data=top10Casualties,type="h",lw=5,scales=list(x=list(rot=60)),main ="Total Casualties caused by Storm and Weather Events")

rownames(top10Casualties)<-NULL
top10Casualties[c(1,4)]
##            eventType casualties
## 1            TORNADO      96979
## 2     EXCESSIVE HEAT       8428
## 3          TSTM WIND       7461
## 4              FLOOD       7259
## 5          LIGHTNING       6046
## 6               HEAT       3037
## 7        FLASH FLOOD       2755
## 8          ICE STORM       2064
## 9  THUNDERSTORM WIND       1621
## 10      WINTER STORM       1527

2.Across the United States, which types of events have the greatest economic consequences?

# Find out possible values for property/crop damage exp variables
unique(tolower(stormData$CROPDMGEXP))
## [1] ""  "m" "k" "b" "?" "0" "2"
#[1] ""  "m" "k" "b" "?" "0" "2"

unique(tolower(stormData$PROPDMGEXP))
##  [1] "k" "m" ""  "b" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "-" "1" "8"
# [1] "k" "m" ""  "b" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "-" "1"
#[17] "8"

#replace prop/crop damage exp values with numbers
stormData$PROPEXP2 <- as.numeric(recode(tolower(stormData$PROPDMGEXP), 
    "'h'=10^2; 'k'=10^3; 'm'=10^6; 'b'=10^9;'+'=1;'-'=0;'?'=0;''=0; else=10"))

stormData$CROPEXP2 <- as.numeric(recode(tolower(stormData$CROPDMGEXP),
     "'h'=10^2; 'k'=10^3; 'm'=10^6; 'b'=10^9;'+'=1;'-'=0;'?'=0;''=0; else=10"))


sumEconDmg <- aggregate(x = 
                list(propertyDamage = stormData$PROPDMG*stormData$PROPEXP2,
                cropDamage =stormData$CROPDMG*stormData$CROPEXP2,
                sumDamage  =(stormData$PROPDMG*stormData$PROPEXP2)+                                (stormData$CROPDMG*stormData$CROPEXP2)),                           by = list(eventType = stormData$EVTYPE), FUN = sum, na.rm = T)           

summary(sumEconDmg)      
##                  eventType   propertyDamage        cropDamage       
##     HIGH SURF ADVISORY:  1   Min.   :0.000e+00   Min.   :0.000e+00  
##   COASTAL FLOOD       :  1   1st Qu.:0.000e+00   1st Qu.:0.000e+00  
##   FLASH FLOOD         :  1   Median :0.000e+00   Median :0.000e+00  
##   LIGHTNING           :  1   Mean   :4.338e+08   Mean   :4.985e+07  
##   TSTM WIND           :  1   3rd Qu.:5.105e+04   3rd Qu.:0.000e+00  
##   TSTM WIND (G45)     :  1   Max.   :1.447e+11   Max.   :1.397e+10  
##  (Other)              :979                                          
##    sumDamage        
##  Min.   :0.000e+00  
##  1st Qu.:0.000e+00  
##  Median :0.000e+00  
##  Mean   :4.837e+08  
##  3rd Qu.:8.500e+04  
##  Max.   :1.503e+11  
## 
top10EconDamage <- head(sumEconDmg[order(sumEconDmg[,4],decreasing="TRUE"),],10)

econDamageChart <- xyplot(sumDamage/1000000~eventType,data=top10EconDamage,type="h",lw="7", 
       main ="Cost of Damage caused by Storm and Weather Events", 
       xlab="Event Type", bw=10, 
       ylab="Total Damage Cost (in Millions $)",
       scales=list(x=list(rot=60)))

rownames(top10EconDamage)<-NULL 
top10EconDamage[c(1,4)]
##            eventType    sumDamage
## 1              FLOOD 150319678250
## 2  HURRICANE/TYPHOON  71913712800
## 3            TORNADO  57352117607
## 4        STORM SURGE  43323541000
## 5               HAIL  18758224527
## 6        FLASH FLOOD  17562132111
## 7            DROUGHT  15018672000
## 8          HURRICANE  14610229010
## 9        RIVER FLOOD  10148404500
## 10         ICE STORM   8967041810

Results

  1. Based on the data analysis the top Storm and Severe Weather Events that caused must of the casualties on the human health are: TORNADOS, EXCESIVE HEAT and TUNDERSTORM WIND.

  1. Based on the data analysis the top Storm and Severe Weather Events that caused must of the damages to properties and crops are: FLOOD, HURRICANE/TYPHOON and TORNADOS.