Course Project

Reproducible Research Course Project 2

Synonpsis

Severe weather events, including storms, can pose dual challenges to public health and local economies. This report presents the outcomes of an analysis focused on identifying the most hazardous weather events in the United States from 1950 to 2011. Utilizing data from the U.S. National Oceanic and Atmospheric Administration (NOAA), the analysis considers factors such as fatalities, injuries, and economic damage to properties and crops for each event. The assessment prioritizes events based on their impact on population health and economic consequences, aiming to inform strategies for proactive mitigation and preparedness.

Importing nessesary library

library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(xtable, warn.conflicts = FALSE)

Load Data

Load the data using read.csv

stormData <- read.csv("D:\\R Coursera\\RP\\repdata_data_StormData.csv\\repdata_data_StormData.csv", sep = ",", header = TRUE)

Prilimnary EDA on data

names(stormData)
##  [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"
str(stormData)
## 'data.frame':    902297 obs. of  37 variables:
##  $ STATE__   : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ BGN_DATE  : chr  "4/18/1950 0:00:00" "4/18/1950 0:00:00" "2/20/1951 0:00:00" "6/8/1951 0:00:00" ...
##  $ BGN_TIME  : chr  "0130" "0145" "1600" "0900" ...
##  $ TIME_ZONE : chr  "CST" "CST" "CST" "CST" ...
##  $ COUNTY    : num  97 3 57 89 43 77 9 123 125 57 ...
##  $ COUNTYNAME: chr  "MOBILE" "BALDWIN" "FAYETTE" "MADISON" ...
##  $ STATE     : chr  "AL" "AL" "AL" "AL" ...
##  $ EVTYPE    : chr  "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
##  $ BGN_RANGE : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ BGN_AZI   : chr  "" "" "" "" ...
##  $ BGN_LOCATI: chr  "" "" "" "" ...
##  $ END_DATE  : chr  "" "" "" "" ...
##  $ END_TIME  : chr  "" "" "" "" ...
##  $ 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   : chr  "" "" "" "" ...
##  $ END_LOCATI: chr  "" "" "" "" ...
##  $ 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: chr  "K" "K" "K" "K" ...
##  $ CROPDMG   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ CROPDMGEXP: chr  "" "" "" "" ...
##  $ WFO       : chr  "" "" "" "" ...
##  $ STATEOFFIC: chr  "" "" "" "" ...
##  $ ZONENAMES : chr  "" "" "" "" ...
##  $ 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   : chr  "" "" "" "" ...
##  $ REFNUM    : num  1 2 3 4 5 6 7 8 9 10 ...
head(stormData)
##   STATE__           BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE  EVTYPE
## 1       1  4/18/1950 0:00:00     0130       CST     97     MOBILE    AL TORNADO
## 2       1  4/18/1950 0:00:00     0145       CST      3    BALDWIN    AL TORNADO
## 3       1  2/20/1951 0:00:00     1600       CST     57    FAYETTE    AL TORNADO
## 4       1   6/8/1951 0:00:00     0900       CST     89    MADISON    AL TORNADO
## 5       1 11/15/1951 0:00:00     1500       CST     43    CULLMAN    AL TORNADO
## 6       1 11/15/1951 0:00:00     2000       CST     77 LAUDERDALE    AL TORNADO
##   BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1         0                                               0         NA
## 2         0                                               0         NA
## 3         0                                               0         NA
## 4         0                                               0         NA
## 5         0                                               0         NA
## 6         0                                               0         NA
##   END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG
## 1         0                      14.0   100 3   0          0       15    25.0
## 2         0                       2.0   150 2   0          0        0     2.5
## 3         0                       0.1   123 2   0          0        2    25.0
## 4         0                       0.0   100 2   0          0        2     2.5
## 5         0                       0.0   150 2   0          0        2     2.5
## 6         0                       1.5   177 2   0          0        6     2.5
##   PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE
## 1          K       0                                         3040      8812
## 2          K       0                                         3042      8755
## 3          K       0                                         3340      8742
## 4          K       0                                         3458      8626
## 5          K       0                                         3412      8642
## 6          K       0                                         3450      8748
##   LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1       3051       8806              1
## 2          0          0              2
## 3          0          0              3
## 4          0          0              4
## 5          0          0              5
## 6          0          0              6

Data Processing

Create Subset of Data

Enhancing computational performance during the processing of a substantial dataset involves optimizing by selecting a subset of essential variables for analysis. In this context, the dataset will be streamlined to encompass only the pertinent variables listed below. Furthermore, the analysis will exclusively consider observations where the variable value exceeds zero, refining the dataset to focus on relevant and impactful data points.

stormDataTidy <- subset(stormData, EVTYPE != "?"
                                   &
                                   (FATALITIES > 0 | INJURIES > 0 | PROPDMG > 0 | CROPDMG > 0),
                                   select = c("EVTYPE",
                                              "FATALITIES",
                                              "INJURIES", 
                                              "PROPDMG",
                                              "PROPDMGEXP",
                                              "CROPDMG",
                                              "CROPDMGEXP",
                                              "BGN_DATE",
                                              "END_DATE",
                                              "STATE"))
dim(stormDataTidy)
## [1] 254632     10
sum(is.na(stormDataTidy))
## [1] 0

The tidy working dataset comprises 254,632 observations with 10 variables, and it is noteworthy that there are no missing values present in the dataset.

Clean Event Type Data

The current tidy dataset encompasses a total of 487 unique Event Type values.

length(unique(stormDataTidy$EVTYPE))
## [1] 487

Event Type data inconsistencies, including different spellings, pluralizations, and case variations, were resolved by normalizing the dataset. All values were converted to uppercase, consolidating similar entries and promoting consistency.

stormDataTidy$EVTYPE <- toupper(stormDataTidy$EVTYPE)
# Define the standardizeEventType function
standardizeEventType <- function(data, pattern, replacement) {
  data$EVTYPE <- gsub(pattern, replacement, data$EVTYPE, ignore.case = TRUE)
  return(data)
}

# Apply the standardization function for each event type

# AVALANCHE
stormDataTidy <- standardizeEventType(stormDataTidy, '.*AVALANCHE.*', 'AVALANCHE')

# BLIZZARD
stormDataTidy <- standardizeEventType(stormDataTidy, '.*BLIZZARD.*', 'BLIZZARD')

# CLOUD
stormDataTidy <- standardizeEventType(stormDataTidy, '.*CLOUD.*', 'CLOUD')

# COLD
pattern_cold <- '.*COLD.*|.*FREEZ.*|.*FROST.*|.*ICE.*|.*LOW TEMPERATURE RECORD.*|.*LO.*TEMP.*'
stormDataTidy <- standardizeEventType(stormDataTidy, pattern_cold, 'COLD')

# DRY
stormDataTidy <- standardizeEventType(stormDataTidy, '.*DRY.*', 'DRY')

# DUST
stormDataTidy <- standardizeEventType(stormDataTidy, '.*DUST.*', 'DUST')

# FIRE
stormDataTidy <- standardizeEventType(stormDataTidy, '.*FIRE.*', 'FIRE')

# FLOOD
stormDataTidy <- standardizeEventType(stormDataTidy, '.*FLOOD.*', 'FLOOD')

# FOG
stormDataTidy <- standardizeEventType(stormDataTidy, '.*FOG.*', 'FOG')

# HAIL
stormDataTidy <- standardizeEventType(stormDataTidy, '.*HAIL.*', 'HAIL')

# HEAT
pattern_heat <- '.*HEAT.*|.*WARM.*|.*HIGH.*TEMP.*|.*RECORD HIGH TEMPERATURES.*'
stormDataTidy <- standardizeEventType(stormDataTidy, pattern_heat, 'HEAT')

# HYPOTHERMIA/EXPOSURE
stormDataTidy <- standardizeEventType(stormDataTidy, '.*HYPOTHERMIA.*', 'HYPOTHERMIA/EXPOSURE')

# LANDSLIDE
stormDataTidy <- standardizeEventType(stormDataTidy, '.*LANDSLIDE.*', 'LANDSLIDE')

# LIGHTNING
pattern_lightning <- '^LIGHTNING.*|^LIGNTNING.*|^LIGHTING.*'
stormDataTidy <- standardizeEventType(stormDataTidy, pattern_lightning, 'LIGHTNING')

# MICROBURST
stormDataTidy <- standardizeEventType(stormDataTidy, '.*MICROBURST.*', 'MICROBURST')

# MUDSLIDE
pattern_mudslide <- '.*MUDSLIDE.*|.*MUD SLIDE.*'
stormDataTidy <- standardizeEventType(stormDataTidy, pattern_mudslide, 'MUDSLIDE')

# RAIN
stormDataTidy <- standardizeEventType(stormDataTidy, '.*RAIN.*', 'RAIN')

# RIP CURRENT
stormDataTidy <- standardizeEventType(stormDataTidy, '.*RIP CURRENT.*', 'RIP CURRENT')

# STORM
stormDataTidy <- standardizeEventType(stormDataTidy, '.*STORM.*', 'STORM')

# SUMMARY
stormDataTidy <- standardizeEventType(stormDataTidy, '.*SUMMARY.*', 'SUMMARY')

# TORNADO
pattern_tornado <- '.*TORNADO.*|.*TORNDAO.*|.*LANDSPOUT.*|.*WATERSPOUT.*'
stormDataTidy <- standardizeEventType(stormDataTidy, pattern_tornado, 'TORNADO')

# SURF
stormDataTidy <- standardizeEventType(stormDataTidy, '.*SURF.*', 'SURF')

# VOLCANIC
stormDataTidy <- standardizeEventType(stormDataTidy, '.*VOLCANIC.*', 'VOLCANIC')

# WET
stormDataTidy <- standardizeEventType(stormDataTidy, '.*WET.*', 'WET')

# WIND
stormDataTidy <- standardizeEventType(stormDataTidy, '.*WIND.*', 'WIND')

# WINTER
pattern_winter <- '.*WINTER.*|.*WINTRY.*|.*SNOW.*'
stormDataTidy <- standardizeEventType(stormDataTidy, pattern_winter, 'WINTER')

Following the tidying process, the dataset now comprises 81 unique Event Type values, reflecting a reduction from the original count due to normalization and categorization efforts.

length(unique(stormDataTidy$EVTYPE))
## [1] 82

Clean Date Data

To facilitate optional reporting or further analysis, the date variables in the raw dataset, specifically BNG_START and END_DATE, are currently stored as factors. To enhance their usability, these variables will be transformed into actual date types that can be manipulated and reported on. For the initial phase, time variables will be disregarded.

Four new variables will be created based on the date variables in the tidy dataset:

stormDataTidy$DATE_START <- as.Date(stormDataTidy$BGN_DATE, format = "%m/%d/%Y")
stormDataTidy$DATE_END <- as.Date(stormDataTidy$END_DATE, format = "%m/%d/%Y")
stormDataTidy$YEAR <- as.integer(format(stormDataTidy$DATE_START, "%Y"))
stormDataTidy$DURATION <- as.numeric(stormDataTidy$DATE_END - stormDataTidy$DATE_START)/3600

Clean Economic Data

table(toupper(stormDataTidy$PROPDMGEXP))
## 
##             -      +      0      2      3      4      5      6      7      B 
##  11585      1      5    210      1      1      4     18      3      3     40 
##      H      K      M 
##      7 231427  11327
table(toupper(stormDataTidy$CROPDMGEXP))
## 
##             ?      0      B      K      M 
## 152663      6     17      7  99953   1986

To facilitate cost calculations, the PROPDMGEXP and CROPDMGEXP variables will be mapped to a multiplier factor. This factor will then be utilized to compute the actual costs for both property and crop damage. Two new variables, PROP_COST and CROP_COST, will be introduced to store these calculated damage costs.

# function to get multiplier factor
getMultiplier <- function(exp) {
    exp <- toupper(exp);
    if (exp == "")  return (10^0);
    if (exp == "-") return (10^0);
    if (exp == "?") return (10^0);
    if (exp == "+") return (10^0);
    if (exp == "0") return (10^0);
    if (exp == "1") return (10^1);
    if (exp == "2") return (10^2);
    if (exp == "3") return (10^3);
    if (exp == "4") return (10^4);
    if (exp == "5") return (10^5);
    if (exp == "6") return (10^6);
    if (exp == "7") return (10^7);
    if (exp == "8") return (10^8);
    if (exp == "9") return (10^9);
    if (exp == "H") return (10^2);
    if (exp == "K") return (10^3);
    if (exp == "M") return (10^6);
    if (exp == "B") return (10^9);
    return (NA);
}

# calculate property damage and crop damage costs (in billions)
stormDataTidy$PROP_COST <- with(stormDataTidy, as.numeric(PROPDMG) * sapply(PROPDMGEXP, getMultiplier))/10^9
stormDataTidy$CROP_COST <- with(stormDataTidy, as.numeric(CROPDMG) * sapply(CROPDMGEXP, getMultiplier))/10^9

Summarize Data

A summarized dataset focused on health impact data, specifically the combined information of fatalities and injuries, will be created. The results will be sorted in descending order based on the overall health impact.

healthImpactData <- aggregate(x = list(HEALTH_IMPACT = stormDataTidy$FATALITIES + stormDataTidy$INJURIES), 
                                  by = list(EVENT_TYPE = stormDataTidy$EVTYPE), 
                                  FUN = sum,
                                  na.rm = TRUE)
healthImpactData <- healthImpactData[order(healthImpactData$HEALTH_IMPACT, decreasing = TRUE),]

A summarized dataset concentrating on damage impact costs, encompassing both property damage and crop damage, will be generated. The results will be sorted in descending order based on the total damage cost.

damageCostImpactData <- aggregate(x = list(DAMAGE_IMPACT = stormDataTidy$PROP_COST + stormDataTidy$CROP_COST), 
                                  by = list(EVENT_TYPE = stormDataTidy$EVTYPE), 
                                  FUN = sum,
                                  na.rm = TRUE)
damageCostImpactData <- damageCostImpactData[order(damageCostImpactData$DAMAGE_IMPACT, decreasing = TRUE),]

Results

Event Types Most Harmful to Population Health

Fatalities and injuries represent the most detrimental impact on population health. The following results showcase the top 10 weather events with the most harmful effects on population health in the U.S.

print(head(healthImpactData, 10))
##    EVENT_TYPE HEALTH_IMPACT
## 70    TORNADO         97075
## 25       HEAT         12392
## 20      FLOOD         10127
## 81       WIND          9893
## 51  LIGHTNING          6049
## 68      STORM          4780
## 11       COLD          3100
## 82     WINTER          1924
## 19       FIRE          1698
## 24       HAIL          1512
healthImpactChart <- ggplot(head(healthImpactData, 10),
                            aes(x = reorder(EVENT_TYPE, HEALTH_IMPACT), y = HEALTH_IMPACT, fill = EVENT_TYPE)) +
                            coord_flip() +
                            geom_bar(stat = "identity") + 
                            xlab("Event Type") +
                            ylab("Total Fatalities and Injures") +
                            theme(plot.title = element_text(size = 14, hjust = 0.5)) +
                            ggtitle("Top 10 Weather Events Most Harmful to\nPopulation Health")
print(healthImpactChart)

Event Types with Greatest Economic Consequences

Property and crop damage constitute the most detrimental impact on the economy. The following results highlight the top 10 weather events with the most harmful economic consequences in the U.S.

print(head(damageCostImpactData, 10))
##           EVENT_TYPE DAMAGE_IMPACT
## 20             FLOOD     180.58156
## 45 HURRICANE/TYPHOON      71.91371
## 68             STORM      70.44994
## 70           TORNADO      57.42785
## 24              HAIL      20.73720
## 15           DROUGHT      15.01867
## 37         HURRICANE      14.61023
## 11              COLD      12.69944
## 81              WIND      12.00554
## 19              FIRE       8.90491
damageCostImpactChart <- ggplot(head(damageCostImpactData, 10),
                            aes(x = reorder(EVENT_TYPE, DAMAGE_IMPACT), y = DAMAGE_IMPACT, fill = EVENT_TYPE)) +
                            coord_flip() +
                            geom_bar(stat = "identity") + 
                            xlab("Event Type") +
                            ylab("Total Property / Crop Damage Cost\n(in Billions)") +
                            theme(plot.title = element_text(size = 14, hjust = 0.5)) +
                            ggtitle("Top 10 Weather Events with\nGreatest Economic Consequences")
print(damageCostImpactChart)

Final Conclusion

Drawing conclusions from the evidence presented in this analysis, supported by the included data and graphs, the following key insights emerge: