Synopsis

The U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database is investigated to answer the following two 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?

Fatalities and injuries were identified as relevant to population’s health, and damage of property and crop were identified as relevant to economic consequences. The result of this study indicated that tornado causes biggest number of fatalities and injuries, flood causes the biggest property damage, and drought causes the greatest crop damage.

Introduction

This is a report for 2nd Project of Course Reproducible Research offered by Johns Hopkins University on Coursera. The project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This report answers the following two 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?

Data and explanation manual can be accessed as the followings:

After exploring the data and reading the manual, the following columns were determined relevant to the two questions:

  1. FATALITIES — fatalities
  2. INJURIES — injuries
  3. PROPDMG — property damage
  4. PROPDMGEXP — exponents of PROPDMG
  5. CROPDMG — crop damage
  6. CROPDMGEXP — exponents of CROPDMGEXP

Data Processing

Load the necessary libraries.

library(ggplot2)
library(grid)
library(gridExtra)
library(hash)
## hash-2.2.6 provided by Decision Patterns
library(data.table)
## 
## Attaching package: 'data.table'
## 
## The following object is masked from 'package:hash':
## 
##     copy

Firstly, the neccessary columns were read into memory.

### figure out which col is in need
# read in limited row
data.tempo <- read.csv("repdata-data-StormData.csv", header = T, nrows = 2)
# get the names
col.names <- names(data.tempo)
# specify needed names
col.names.need <- c("EVTYPE","FATALITIES", "INJURIES","PROPDMG","PROPDMGEXP", "CROPDMG", "CROPDMGEXP")
# get the numbers of col with specified names
col.need <- as.integer()
for (i in 1: length(col.names.need)) {
  col.need[i] <- which(col.names == col.names.need[i])
}
# show the col numbers
col.need
## [1]  8 23 24 25 26 27 28
### read in only the needed cols
data <- read.csv("repdata-data-StormData.csv", header = T, 
                 colClasses = 
                   c(rep("NULL", 7), 
                     "character", 
                     rep("NULL", 14), 
                     rep("character", 6), 
                     rep("NULL", 9)))
# show the head of the data
head(data)
##    EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO       0.00    15.00   25.00          K    0.00           
## 2 TORNADO       0.00     0.00    2.50          K    0.00           
## 3 TORNADO       0.00     2.00   25.00          K    0.00           
## 4 TORNADO       0.00     2.00    2.50          K    0.00           
## 5 TORNADO       0.00     2.00    2.50          K    0.00           
## 6 TORNADO       0.00     6.00    2.50          K    0.00

Secondly, the data were reorganized to generate a new data frame that is ready for the use of plotting.

### process data for analysis
# make a map of exponents
map<-hash(keys=c("1", "2", "3", "4", "5", "6", "7", "8", "B", "H", "K", "M", "m"),
          values=c(1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e2, 1e3, 1e6, 1e6))

# get exp number
mapExp<-function(exp){
  sapply(exp,function(x){
    if (x=="") {
      return (1)
    }
      
    if (!has.key(x,map)) {
      return (1)
    }
      
    return (map[[x]])
  })
}

# recalculate propdmg
data$PROPDMG <- as.double(data$PROPDMG)
data$PROPDMG <- data$PROPDMG* mapExp(data$PROPDMGEXP)
# recalculate cropdmg
data$CROPDMG <- as.double(data$CROPDMG)
data$CROPDMG <- data$CROPDMG* mapExp(data$CROPDMGEXP)
# prep fatality and injure
data$FATALITIES <- as.double(data$FATALITIES)
data$INJURIES <- as.double(data$INJURIES)

### analysis
data.tb <- data.table(data)
# fatality sum by type
data.tb.fatal <- data.tb[,as.double(sum(FATALITIES, na.rm = TRUE)), by = EVTYPE]
# get types and fatal
data.types <- data.tb.fatal[[1]]
data.fatal <- data.tb.fatal[[2]]
# injures sum by type
data.tb.injure <- data.tb[,as.double(sum(INJURIES, na.rm = TRUE)), by = EVTYPE]
# get injure
data.injure <- data.tb.injure[[2]]
# prop dmg by type
data.tb.propdmg <- data.tb[,as.double(sum(PROPDMG, na.rm = TRUE)), by = EVTYPE]
# get prop dmg
data.propdmg <- data.tb.propdmg[[2]]
# crop dmg by type
data.tb.cropdmg <- data.tb[,as.double(sum(CROPDMG, na.rm = TRUE)), by = EVTYPE]
# get prop dmg
data.cropdmg <- data.tb.cropdmg[[2]]
# prep a new data frame for plotting
data.total <- cbind(data.fatal, data.injure, data.propdmg, data.cropdmg)
data.total <- as.data.frame(data.total)
data.total$type <- data.types
names(data.total) <- c("fatal", "injure", "propdmg", "crpdmg", "type")
# show new data
head(data.total)
##   fatal injure   propdmg    crpdmg                  type
## 1  5633  91346 5.695e+10 4.150e+08               TORNADO
## 2   504   6957 4.485e+09 5.540e+08             TSTM WIND
## 3    15   1361 1.574e+10 3.026e+09                  HAIL
## 4     7     23 8.112e+06 0.000e+00         FREEZING RAIN
## 5     5     29 1.476e+07 1.000e+04                  SNOW
## 6     0      2 0.000e+00 0.000e+00 ICE STORM/FLASH FLOOD

Results

The following code generates the first plot that answers the first question.

# plot fatality
data.total <- data.total[order(-data.total$fatal),]
data.subset.fatal <- data.total[1:6,]
data.subset.fatal$type <- as.factor(data.subset.fatal$type)
plot.fatal = ggplot(data.subset.fatal, aes(x=reorder(type,1:6),y=fatal)) 
plot.fatal = plot.fatal + geom_bar(stat="identity",fill="orange") 
plot.fatal = plot.fatal + theme(panel.background = element_rect(fill = 'white'), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
plot.fatal = plot.fatal + labs(title="Fatalities by Event", x="Event", y="Number of Fatality")

# plot injure
data.total <- data.total[order(-data.total$injure),]
data.subset.inj <- data.total[1:6,]
data.subset.inj$type <- as.factor(data.subset.inj$type)
plot.inj = ggplot(data.subset.inj, aes(x=reorder(type,1:6),y=injure)) 
plot.inj = plot.inj + geom_bar(stat="identity",fill="#56B4E9") 
plot.inj = plot.inj + theme(panel.background = element_rect(fill = 'white'), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
plot.inj = plot.inj + labs(title="Injuries by Event", x="Event", y="Number of Injuries")

# make two plots in one graph
grid.draw(cbind(ggplotGrob(plot.fatal), ggplotGrob(plot.inj), size="last"))

plot of chunk unnamed-chunk-4

As is shown, TORNADO is the most harmful with respect to population health. ECSESSIVE HEAT and FLASH FLOOD also cause many more fatalities than other events.

The following code generates the second plot that answers the second question.

# plot prop dmg
data.total <- data.total[order(-data.total$propdmg),]
data.subset.propdmg <- data.total[1:6,]
data.subset.propdmg$type <- as.factor(data.subset.propdmg$type)
plot.propdmg = ggplot(data.subset.propdmg, aes(x=reorder(type,1:6),y=propdmg/map[["B"]])) 
plot.propdmg = plot.propdmg + geom_bar(stat="identity",fill="orange") 
plot.propdmg = plot.propdmg + theme(panel.background = element_rect(fill = 'white'), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
plot.propdmg = plot.propdmg + labs(title="Property Damage by Event", x="Event", y="Sum of Property Damage (in billion)")

# plot crop dmg
data.total <- data.total[order(-data.total$crpdmg),]
data.subset.crpdmg <- data.total[1:6,]
data.subset.crpdmg$type <- as.factor(data.subset.crpdmg$type)
plot.cropdmg = ggplot(data.subset.crpdmg, aes(x=reorder(type,1:6),y=crpdmg/map[["B"]])) 
plot.cropdmg = plot.cropdmg + geom_bar(stat="identity",fill="#56B4E9") 
plot.cropdmg = plot.cropdmg + theme(panel.background = element_rect(fill = 'white'), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
plot.cropdmg = plot.cropdmg + labs(title="Crop Damage by Event", x="Event", y="Sum of Crop Damage (in billion)")

# make two plots in one graph
grid.draw(cbind(ggplotGrob(plot.propdmg), ggplotGrob(plot.cropdmg), size="last"))

plot of chunk unnamed-chunk-5

As is shown, FLOOD has the greatest economic consequences in terms of property damage, while DROUGHT has the greatest economic consequences in terms of crop damage. FLOOD has bigger economic consequences if we only consider the consequences in terms of monetary losses.