-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepData_PeerAssessment2.Rmd
245 lines (196 loc) · 8.15 KB
/
RepData_PeerAssessment2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
---
title: "RepData_PeerAssessment2, An analysis of the effects of extreme weather events on public health, property & crops in the US: 1950-2011"
author: "james"
date: "June 12, 2016"
output:
html_document:
fig_height: 4
fig_width: 8
highlight: null
self_contained: no
smart: no
theme: null
---
---
# **SYNOPSIS:**
## Storms and other severe weather events can cause both public health and
## economic problems for communities and municipalities. These events
## can result in fatalities, injuries, property and crop damage. A key concern
## among policy makers is preventing such outcomes to the extent possible.
## This analysis involves exploring the U.S. National Oceanic and Atmospheric
## Administration's (NOAA) storm database between 1950 and 2011. 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 as well as property and crop losses.
## This analysis found that tornados had the greatest impact on public health,
## floods caused the greatests economic losses to property and that drought
## caused the greatest losses to crops.
---
# **RAW 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]*
## *There is also some documentation of the database available. Here you*
*will find how some of the variables are constructed/defined.*
## *National Weather Service Storm Data Documentation*
## *National Climatic Data Center Storm Events FAQ*
## *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. More recent*
*years should be considered more complete.*
#################################################
## 1 - **PROJECT - CONFIGURATION**
---
#### 1a. Set working directory, set R chunk setting echo = TRUE, turn off
#### scientific notation and cleanup workspace of asny pre existing environment
#### values and variables
```{r setwdoptions, echo=TRUE, cache=TRUE}
setwd("~/Desktop/Coursera_R/RepData_PeerAssessment2")
options(echo = TRUE, cache = TRUE, scipen = 0)
```
---
#### 1b. Install packages most likely to be used
```{r installpackage, echo=TRUE, cache=TRUE}
library(dplyr)
library(reshape2)
library(dplyr)
library(magrittr)
library(reshape2)
library(stringr)
library(testthat)
```
#################################################
## 2 - **ACCESS, DOWNLOAD & READ & PRE-PROCESS THE DATA**
---
#### 2a. Source data with url, download zipfile, unzip, read the .csv
#### data file, record date and time of download, print list of files in this
#### directory
```{r downloaddata, echo=TRUE, cache=TRUE}
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
zipFile <- "StormData.csv.bz2"
if (!file.exists("StormData.csv.bz2")) {
message(paste("Downloding", zipFile))
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl, destfile="StormData.csv.bz2", method = "curl")
}else{
message(paste("File exists;", zipFile))
}
```
---
#### 2b. Get time stamp on download
```{r downloaddate, echo=TRUE, cache=TRUE}
dateDownLoaded <- date()
dateDownLoaded
```
---
#### 2c. Unzip & read the downloaded files
```{r unzipreadcsv, echo=TRUE, cache=TRUE}
rawstormdata <<- read.csv(bzfile("StormData.csv.bz2"), header=TRUE, stringsAsFactors = FALSE)
```
---
#### 2d. Print contents - current directory
```{r listfiles, echo=, cache=TRUE}
ls()
```
#################################################
## 3 - **DATA PROCESSING**
---
#### 3a. Subset relevant columns to this analysis
```{r subsetcolumns, echo=TRUE, cache=TRUE}
newstormdata <<- rawstormdata[, c(8,23:28)]
```
---
#### 3b. Determine public health effects by event type
```{r healthfatlaities, echo=TRUE, cache=TRUE}
dfhealth <- newstormdata[,c(1:3)]
totaldeaths <- dfhealth %>% group_by(EVTYPE) %>% summarise(sum(FATALITIES))
arrange(totaldeaths, desc(`sum(FATALITIES)`))
```
---
#### 3c. Determine public health effects by event type for injuries
```{r healthinjuries, echo=TRUE, cache=TRUE}
dfhealth <- newstormdata[,c(1:3)]
totalinjuries <- dfhealth %>% group_by(EVTYPE) %>% summarise(sum(INJURIES))
arrange(totalinjuries, desc(`sum(INJURIES)`))
```
---
#### 3d. Write function "useexp" that converts the "PROPDMGEXP" &
#### "CROPDMGEXP alpha codes to integers
```{r replaceexponent, echo=TRUE, cache=TRUE}
useexp <- function(e) {
if (e %in% c("H"))
return(2)
else if (e %in% c("K"))
return(3)
else if (e %in% c("M"))
return(6)
else if (e %in% c("B"))
return(9)
else if (!is.na(as.numeric(e)))
return(as.numeric(e))
else if (e %in% c("","+","?","-"))
return(0)
else {
stop("Not a valid exponent")
}
}
```
---
#### 3e. Make a smaller data subset of the property, convert the damage
#### exponent codes to all upper case lestter, apply the 'useexp' function
#### to convert the the exponential sums of losses into dollar amounts
#### then group by event type, then arrange the top 15 items in decreasing
#### magnitude and save as a new data set
```{r property, echo=TRUE, cache=TRUE}
dfproperty <- newstormdata[,c(1,4,5)]
dfproperty$PROPDMGEXP <- toupper(dfproperty$PROPDMGEXP)
dfproperty$PROPDMGEXP <- sapply(dfproperty$PROPDMGEXP, FUN=useexp)
dfproperty$loss <- dfproperty$PROPDMG * (10 ** dfproperty$PROPDMGEXP)
dfproperty2 <- dfproperty %>% group_by(EVTYPE) %>% summarise(sum(loss))
dfpropertylossbytype <<- arrange(dfproperty2,desc(`sum(loss)`))[1:15,]
dfpropertylossbytype
```
---
#### 3e. Make a smaller data subset of the crops, convert the damage
#### exponent codes to all upper case lestter, apply the 'useexp' function
#### to convert the the exponential sums of losses into dollar amounts
#### then group by event type, then arrange the top 15 items in decreasing
#### magnitude and save as a new data set
```{r crops, echo=TRUE, cache=TRUE}
dfcrops <- newstormdata[,c(1,6,7)]
dfcrops$CROPDMGEXP <- toupper(dfcrops$CROPDMGEXP)
dfcrops$CROPDMGEXP <- sapply(dfcrops$CROPDMGEXP, FUN=useexp)
dfcrops$loss <- dfcrops$CROPDMG * (10 ** dfcrops$CROPDMGEXP)
dfcrops2 <- dfcrops %>% group_by(EVTYPE) %>% summarise(sum(loss))
dfcropslossbytype <<- arrange(dfcrops2,desc(`sum(loss)`))[1:15,]
dfcropslossbytype
```
#################################################
---
## 4 - **DISPLAY RESULTS**
---
#### 4a. Isolate the top ten values indicating total deaths and injuries by
#### type of weather event
```{r toptenvalueshealth, echo=TRUE, cache=TRUE}
totaldeaths <- arrange(totaldeaths, desc(`sum(FATALITIES)`))[1:10,]
totalinjuries <- arrange(totalinjuries, desc(`sum(INJURIES)`))[1:10,]
```
---
#### 4b. Plot the total fatalities and injuries by type of severe
#### weather event
```{r publicheatheffects, echo=TRUE, cache=TRUE}
par(mfrow = c(1, 2), mar = c(11.5, 5.5, 3.1, 2.2), mgp = c(3, 1, 0), cex = 0.7, las = 3)
options(scipen = 0)
barplot(totaldeaths$`sum(FATALITIES)`, names.arg = totaldeaths$EVTYPE, col = 'brown', main = "Fatalities From Weather", ylab = "Number of Fatalities")
barplot(totalinjuries$`sum(INJURIES)`, names.arg = totalinjuries$EVTYPE, col = 'orange', main = "Injuries From Weather", ylab = "Number of Injuries")
```
---
#### 4c. Plot the total dollar losses to property and crops caused by
#### each type of severe weather event
```{r propertycroploss, echo=TRUE, cache=TRUE}
par(mfrow = c(1, 2), mar = c(11.5, 5.5, 3.1, 2.2), mgp = c(3, 1, 0), cex = 0.7, las = 3)
options(scipen = 0)
dfpropertylossbytype
barplot(dfpropertylossbytype$`sum(loss)`, names.arg = dfpropertylossbytype$EVTYPE, col = 'brown', main = "Value of Property Loss From Weather", ylab = "US Dollar Amount")
dfcropslossbytype
barplot(dfcropslossbytype$`sum(loss)`, names.arg = dfcropslossbytype$EVTYPE, col = 'green', main = "Value of Crop Loss From Weather", ylab = "US Dollar Amount")
```