The project aims to find out the type of weather events in the US
that
1. does the most harm to population health
2. results in the greatest economic consequences
with data from National Weather Service.
require(tidyverse)
require(lubridate)
require(data.table)
require(stringdist)
file.url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
file.path <- "data/repdata_data_StormData.csv.bz2"
if (!file.exists(file.path)) {
download.file(file.url, file.path)
}
dt <- fread(file.path)
colnames(dt)
## [1] "STATE__" "BGN_DATE" "BGN_TIME" "TIME_ZONE" "COUNTY"
## [6] "COUNTYNAME" "STATE" "EVTYPE" "BGN_RANGE" "BGN_AZI"
## [11] "BGN_LOCATI" "END_DATE" "END_TIME" "COUNTY_END" "COUNTYENDN"
## [16] "END_RANGE" "END_AZI" "END_LOCATI" "LENGTH" "WIDTH"
## [21] "F" "MAG" "FATALITIES" "INJURIES" "PROPDMG"
## [26] "PROPDMGEXP" "CROPDMG" "CROPDMGEXP" "WFO" "STATEOFFIC"
## [31] "ZONENAMES" "LATITUDE" "LONGITUDE" "LATITUDE_E" "LONGITUDE_"
## [36] "REMARKS" "REFNUM"
dim(dt)
## [1] 902297 37
According to NOAA, the data recording start from Jan. 1950. At that time they recorded one event type, tornado. They add more events gradually and only from Jan. 1996 they start recording all events type. Subset the data to get the part with all events type recorded.
dt1 <- dt[,c('BGN_DATE','EVTYPE', 'FATALITIES', 'INJURIES', 'PROPDMG', 'PROPDMGEXP', 'CROPDMG', 'CROPDMGEXP')]
dt2 <- dt1[mdy_hms(BGN_DATE)>="1996-01-01",]
dim(dt2)
## [1] 653507 8
Show unique values of CROPDMGEXP and PROPDMGEXP
with(dt2, unique(PROPDMGEXP))
## [1] "K" "" "M" "B" "0"
with(dt2, unique(CROPDMGEXP))
## [1] "K" "" "M" "B"
Set keys for the unique values of PROPDMGEXP and CROPDMGEXP to decode them
propdmgKey <- c("0" = 10^1,"K" = 10^3,"M" = 10^6, "B" = 10^9)
cropdmgKey<- c("K" = 10^3, "M" = 10^6, "B" = 10^9)
dt2[, PROPDMGEXP := propdmgKey[as.character(dt2[, PROPDMGEXP])]]
dt2[, CROPDMGEXP := cropdmgKey[as.character(dt2[, CROPDMGEXP])]]
dt2[is.na(PROPDMGEXP), PROPDMGEXP := 10^0 ]
dt2[is.na(CROPDMGEXP), CROPDMGEXP := 10^0 ]
Preview first 6 rows of data and check unique EVTYPE; Around 87.95% of all the events are categorized into the 10 most frequent events, while about 12.05% of all events being categorized into the other 505 event types. It’s possible that one type of the 12.05% events does the most harm to population health and economy. But we could be quite confident that the type of event does the most harm is amoung the 10 most frequent types.
head(dt2)
## BGN_DATE EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP
## 1: 1/6/1996 0:00:00 WINTER STORM 0 0 380 1000
## 2: 1/11/1996 0:00:00 TORNADO 0 0 100 1000
## 3: 1/11/1996 0:00:00 TSTM WIND 0 0 3 1000
## 4: 1/11/1996 0:00:00 TSTM WIND 0 0 5 1000
## 5: 1/11/1996 0:00:00 TSTM WIND 0 0 2 1000
## 6: 1/18/1996 0:00:00 HAIL 0 0 0 1
## CROPDMG CROPDMGEXP
## 1: 38 1000
## 2: 0 1
## 3: 0 1
## 4: 0 1
## 5: 0 1
## 6: 0 1
length(unique(dt2$EVTYPE))
## [1] 515
sum(sort(table(dt2$EVTYPE),decreasing = T)[1:10])
## [1] 574781
sum(sort(table(dt2$EVTYPE),decreasing = T)[1:10])/dim(dt2)[[1]]
## [1] 0.879533
Combining fatalities and injuries and sorting the combined data give us the event types that are most harmful with respect to population health. Tornado harms population health most.
pop <- dt2 %>% dplyr::group_by(EVTYPE) %>% dplyr::summarize(fatalities = sum(FATALITIES), injuries= sum(INJURIES), pop = fatalities+ injuries) %>% arrange(-pop)
head(pop)
## # A tibble: 6 × 4
## EVTYPE fatalities injuries pop
## <chr> <dbl> <dbl> <dbl>
## 1 TORNADO 1511 20667 22178
## 2 EXCESSIVE HEAT 1797 6391 8188
## 3 FLOOD 414 6758 7172
## 4 LIGHTNING 650 4140 4790
## 5 TSTM WIND 241 3629 3870
## 6 FLASH FLOOD 887 1674 2561
t <- pop %>% slice(1:10) %>%
select(-pop)
t <- melt(setDT(t),id.vars= "EVTYPE",variable.name ="type")
ggplot(t, aes(reorder(EVTYPE,-value),value ,fill= type)) +
geom_bar(position = "dodge", stat= "identity")+
labs(title = "top 10 harmful events with respect to population health",x="Event Type",y= "Count")+
theme(axis.text.x = element_text(angle = 35))
Multiplying property damage and the corresponding multiplier, crop damage and corresponding multiplier combined give the top events that have the greatest economic consequences. Flood does the most harm to economy.
eco_summary <- dt2 %>%
mutate(propdmg = PROPDMG * PROPDMGEXP, cropdmg= CROPDMG * CROPDMGEXP, eco = propdmg + cropdmg) %>%
group_by(EVTYPE) %>%
summarise(propdmg= sum(propdmg), cropdmg=sum(cropdmg), eco = sum(eco)) %>%
arrange(by = -eco) %>%
slice(1:10)
t2 <- eco_summary %>% select(-eco)
t2 <- melt(setDT(t2),id.vars= "EVTYPE",variable.name ="type")
ggplot(t2, aes(reorder(EVTYPE,-value),value ,fill= type)) + geom_bar(position = "dodge", stat= "identity")+ labs(title = "top 10 events the greatest economic consequences",x="Event Type",y= "Dollar Amount")+
theme(axis.text.x = element_text(angle = 35))