Title:Analysis Storm data in USA

Course Title: Reproducible Research

Project 2

Summary

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.

Data

The data for this assignment come in the form of a comma-separated-value file compressed via

the bzip2 algorithm to reduce its size. You can download the file from the course web site:

Storm Data [47Mb].

The events in the database start in the year 1950 and end in November 2011. In the earlier years

of the database there are generally fewer events recorded, most likely due to a lack of good records.

# Set working dir - optional
# setwd('~/working_dir') 


# Download the dataset into workspace (wd), if it is not avalaible in local 
#fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
#fileZip <- "dataset.csv"
#if (!file.exists(fileZip)) download.file(fileUrl, fileZip, method = "auto")

# library for plotting
library(ggplot2)

# Read file
#storm <- read.csv("dataset.csv")

# ----------------------------------------------Question 1 -----------------------------------

# 1- Across the United States, which types of events (as indicated in the EVTYPE variable) are 
# most harmful with respect to population health?
# Look at data for FATALITIES with respect to events type


# Assign storm to a temp variable df for prep processing

 df <- storm 

# I have chosen to look for 13 major events. I have used the following list and replaced # # them in the main storm data set. The reason is, there a lot of data quality issues when # it comes to nameing the even types. Lots of event are a subset and also a combination of # the below 13 major events
# ------------------------------- # Data cleaning and processing -----------------------------
# Type of Event:
events <- c("TORNADO", "BLIZZARD", "STORM", "HURRICANE", "FLOOD", "SNOW", "RAIN"
            , "LIGHNING", "WIND", "WILDFIRE","COLD", "AVALANCHE","HEAT")
# Search and Replace malformed events type
df$EVTYPE[grep("*FLOOD*",df$EVTYPE, ignore.case=T)] <- "FLOOD"
df$EVTYPE[grep("*TORNADO*", df$EVTYPE, ignore.case=T)] <- "TORNADO"
df$EVTYPE[grep("*STORM*", df$EVTYPE, ignore.case=T)] <- "STORM"
## Warning: invalid factor level, NA generated
df$EVTYPE[grep("*SNOW*", df$EVTYPE, ignore.case=T)] <- "SNOW"
df$EVTYPE[grep("*BLIZZARD*", df$EVTYPE, ignore.case=T)] <- "BLIZZARD"
df$EVTYPE[grep("*HURRICANE*", df$EVTYPE, ignore.case=T)] <- "HURRICANE"
df$EVTYPE[grep("*HEAT*", df$EVTYPE, ignore.case=T)] <- "HEAT"
df$EVTYPE[grep("*WILDFIRE*", df$EVTYPE, ignore.case=T)] <- "WILDFIRE"
df$EVTYPE[grep("*LIGHNING*", df$EVTYPE, ignore.case=T)] <- "LIGHNING"
## Warning: invalid factor level, NA generated
df$EVTYPE[grep("*WIND*", df$EVTYPE, ignore.case=T)] <- "WIND"
df$EVTYPE[grep("*COLD*", df$EVTYPE, ignore.case=T)] <- "COLD"
df$EVTYPE[grep("*Avalanche*", df$EVTYPE, ignore.case=T)] <- "AVALANCHE"
# ------------------------------- # Summary of data -----------------------------
# Summary tables of events types
  head(table(df$EVTYPE))
## 
##    HIGH SURF ADVISORY         COASTAL FLOOD           FLASH FLOOD 
##                     1                     0                     0 
##             LIGHTNING             TSTM WIND       TSTM WIND (G45) 
##                     1                     0                     0
# Sample of list of unique event type

  head(unique(df$EVTYPE), 13)
##  [1] TORNADO       WIND          HAIL          FREEZING RAIN SNOW         
##  [6] FLOOD         <NA>          HURRICANE     COLD          HEAT         
## [11] LIGHTNING     DENSE FOG     RIP CURRENT  
## 985 Levels:    HIGH SURF ADVISORY  COASTAL FLOOD ... WND
# Sum the number of fatalities + injuries
  storm.FATALITIES <- aggregate(FATALITIES ~ EVTYPE,  data=df, FUN=sum)
  storm.INJURIES <- aggregate(INJURIES ~ EVTYPE, data=df, FUN=sum)

# order by high number of fatalities and create a new data frame for Fatality and Event Type
  Fatality.data <- head(storm.FATALITIES[order(-storm.FATALITIES$FATALITIES),])
# plot of fatalities by major events
# ------------------------------- # Ploting data  -----------------------------
# Figure 1: Number of Fatalities by top events
  qplot(EVTYPE, FATALITIES, xlab="EVENT TYPE", data= Fatality.data, ylab="FATALITY", 
      geom="line", group=1, main="# of Fatalities by top events")

plot of chunk unnamed-chunk-4

# Injuries
# order by high number of fatalities and create a new data frame for Fatality and Event Type
  Injury.data <- head(storm.FATALITIES[order(-storm.INJURIES $INJURIES),])
# plot of fatalities by major events
#Figure 2: Number of Injuries by top events
  qplot(EVTYPE, FATALITIES, xlab="EVENT TYPE", data= Injury.data, ylab="INJURY", 
      geom="line", group=1, main="# of Injuries by top events")

plot of chunk unnamed-chunk-6

#-------------------------- Question 2 ----------------------------------------------------#
# Across the United States, which types of events have the greatest economic consequences ?
#
# Analysis
# Extract data from strom that have Event type and property damages into a new df
damage.data <- subset(df, df$EVTYPE %in% events)

# sum all damage values by event
damage <- aggregate(PROPDMG ~ EVTYPE,  data=damage.data, FUN=sum)
# top 6 property damage values
property.damage <- head(damage[order(damage$PROPDMG),])
# plot of property damage values by major events

#Figure 3: Property damage cost in (millions) by top events
a <- qplot(EVTYPE, PROPDMG, xlab="EVENT TYPE", data= property.damage, ylab="Propery damage values", 
     , main="Property damage cost in (millions) by top events")
a + geom_smooth(aes(group = 1))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Chernobyl! trL>n 6
## Warning: Chernobyl! trL>n 6
## Warning: NaNs produced
## Warning: NaNs produced

plot of chunk unnamed-chunk-8