Introduction

This report contains exploratory data anlysis on the data that are collected on accidental cases.

options(repos = c(CRAN = “https://cran.rstudio.com”))

#Install the packages

install.packages(“ggplot2”)

install.packages(“readxl”)

install.packages(“dplyr”)

install.packages(“reshape2”)

#Load the libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("ggplot2")
library(readxl)
library(dplyr)
library(reshape2)
## 
## Attaching package: 'reshape2'
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths

#Step1: Load the data

mydata1<-read.csv(file.choose(),header=T)

#Step:2 Summary

head(mydata1)
##   Wthr_Cond_ID     Light_Cond_ID  Road_Type_ID    Road_Algn_ID SurfDry
## 1        Clear Dark, not lighted 2 lane, 2 way Straight, level       1
## 2        Clear Dark, not lighted 2 lane, 2 way Straight, level       1
## 3        Clear          Daylight 2 lane, 2 way Straight, level       1
## 4        Clear          Daylight 2 lane, 2 way Straight, level       1
## 5        Clear Dark, not lighted 2 lane, 2 way Straight, grade       1
## 6        Clear          Daylight       Unknown Straight, level       1
##         Traffic_Cntl_ID               Harm_Evnt_ID Intrsct_Relat_ID
## 1          Marked lanes Motor vehicle in transport Non intersection
## 2 Center stripe/divider Motor vehicle in transport Non intersection
## 3          Marked lanes Motor vehicle in transport     Intersection
## 4 Center stripe/divider               Fixed object Non intersection
## 5                  None Motor vehicle in transport Non intersection
## 6                  None Motor vehicle in transport  Driveway access
##                     FHE_Collsn_ID Road_Part_Adj_ID         Road_Cls_ID
## 1 Sd both going straight-rear end Main/proper lane      Farm to market
## 2 Sd both going straight-rear end Main/proper lane Us & state highways
## 3                           Other Main/proper lane      Farm to market
## 4      Omv vehicle going straight Main/proper lane Us & state highways
## 5 Sd both going straight-rear end Main/proper lane      Farm to market
## 6                           Other Main/proper lane         County road
##          Pop_Group_ID Crash_Speed_LimitCat Veh_Body_Styl_ID
## 1 10,000 - 24,999 pop            30-40 mph   Farm equipment
## 2               Rural            65-70 mph   Farm equipment
## 3               Other            45-60 mph   Farm equipment
## 4               Rural            65-70 mph   Farm equipment
## 5               Rural            45-60 mph   Farm equipment
## 6               Rural            30-40 mph   Farm equipment
##                          Prsn_Ethnicity_ID GenMale TrafVol    Prsn_Age
## 1 White                                          1   23681 25-54 years
## 2 White                                          1    1384 25-54 years
## 3 White                                          1   16838       Other
## 4 White                                          1   13870 25-54 years
## 5                                    Other       1    1953 25-54 years
## 6 White                                          1    1324 55-64 years
##   Prsn_Injry_Sev_ID
## 1                 O
## 2                 O
## 3                 O
## 4                 O
## 5                 O
## 6                 O
summary(mydata1)
##  Wthr_Cond_ID       Light_Cond_ID      Road_Type_ID       Road_Algn_ID      
##  Length:1295        Length:1295        Length:1295        Length:1295       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##     SurfDry       Traffic_Cntl_ID    Harm_Evnt_ID       Intrsct_Relat_ID  
##  Min.   :0.0000   Length:1295        Length:1295        Length:1295       
##  1st Qu.:1.0000   Class :character   Class :character   Class :character  
##  Median :1.0000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :0.9143                                                           
##  3rd Qu.:1.0000                                                           
##  Max.   :1.0000                                                           
##  FHE_Collsn_ID      Road_Part_Adj_ID   Road_Cls_ID        Pop_Group_ID      
##  Length:1295        Length:1295        Length:1295        Length:1295       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##  Crash_Speed_LimitCat Veh_Body_Styl_ID   Prsn_Ethnicity_ID     GenMale      
##  Length:1295          Length:1295        Length:1295        Min.   :0.0000  
##  Class :character     Class :character   Class :character   1st Qu.:1.0000  
##  Mode  :character     Mode  :character   Mode  :character   Median :1.0000  
##                                                             Mean   :0.8842  
##                                                             3rd Qu.:1.0000  
##                                                             Max.   :1.0000  
##     TrafVol        Prsn_Age         Prsn_Injry_Sev_ID 
##  Min.   :  202   Length:1295        Length:1295       
##  1st Qu.: 8188   Class :character   Class :character  
##  Median :15426   Mode  :character   Mode  :character  
##  Mean   :15039                                        
##  3rd Qu.:22049                                        
##  Max.   :28993

#Step:3 Checking the structure of the data

str(data)
## function (..., list = character(), package = NULL, lib.loc = NULL, verbose = getOption("verbose"), 
##     envir = .GlobalEnv, overwrite = TRUE)

#Step:4 Count Missing Values

sum(is.na(mydata1))
## [1] 0

#Step:5 Adding column for traffictype

newdata<-mutate(mydata1, Traffictype=factor(TrafVol>5000,labels=c("Low","High")))
head(newdata)
##   Wthr_Cond_ID     Light_Cond_ID  Road_Type_ID    Road_Algn_ID SurfDry
## 1        Clear Dark, not lighted 2 lane, 2 way Straight, level       1
## 2        Clear Dark, not lighted 2 lane, 2 way Straight, level       1
## 3        Clear          Daylight 2 lane, 2 way Straight, level       1
## 4        Clear          Daylight 2 lane, 2 way Straight, level       1
## 5        Clear Dark, not lighted 2 lane, 2 way Straight, grade       1
## 6        Clear          Daylight       Unknown Straight, level       1
##         Traffic_Cntl_ID               Harm_Evnt_ID Intrsct_Relat_ID
## 1          Marked lanes Motor vehicle in transport Non intersection
## 2 Center stripe/divider Motor vehicle in transport Non intersection
## 3          Marked lanes Motor vehicle in transport     Intersection
## 4 Center stripe/divider               Fixed object Non intersection
## 5                  None Motor vehicle in transport Non intersection
## 6                  None Motor vehicle in transport  Driveway access
##                     FHE_Collsn_ID Road_Part_Adj_ID         Road_Cls_ID
## 1 Sd both going straight-rear end Main/proper lane      Farm to market
## 2 Sd both going straight-rear end Main/proper lane Us & state highways
## 3                           Other Main/proper lane      Farm to market
## 4      Omv vehicle going straight Main/proper lane Us & state highways
## 5 Sd both going straight-rear end Main/proper lane      Farm to market
## 6                           Other Main/proper lane         County road
##          Pop_Group_ID Crash_Speed_LimitCat Veh_Body_Styl_ID
## 1 10,000 - 24,999 pop            30-40 mph   Farm equipment
## 2               Rural            65-70 mph   Farm equipment
## 3               Other            45-60 mph   Farm equipment
## 4               Rural            65-70 mph   Farm equipment
## 5               Rural            45-60 mph   Farm equipment
## 6               Rural            30-40 mph   Farm equipment
##                          Prsn_Ethnicity_ID GenMale TrafVol    Prsn_Age
## 1 White                                          1   23681 25-54 years
## 2 White                                          1    1384 25-54 years
## 3 White                                          1   16838       Other
## 4 White                                          1   13870 25-54 years
## 5                                    Other       1    1953 25-54 years
## 6 White                                          1    1324 55-64 years
##   Prsn_Injry_Sev_ID Traffictype
## 1                 O        High
## 2                 O         Low
## 3                 O        High
## 4                 O        High
## 5                 O         Low
## 6                 O         Low

#step:6 Creating new data with two columns only

newdata<-newdata %>% transmute(ScaledTrafficVolume=scale(TrafVol),Personage=Prsn_Age)
head(newdata)
##   ScaledTrafficVolume   Personage
## 1           1.0468830 25-54 years
## 2          -1.6543011 25-54 years
## 3           0.2178834       Other
## 4          -0.1416768 25-54 years
## 5          -1.5853692 25-54 years
## 6          -1.6615698 55-64 years

#Step:7 Arranging the data in ascending order

newdata<-arrange(newdata,ScaledTrafficVolume)
head(newdata)
##   ScaledTrafficVolume   Personage
## 1           -1.797495 25-54 years
## 2           -1.788530 25-54 years
## 3           -1.785744 25-54 years
## 4           -1.783685 55-64 years
## 5           -1.778354 25-54 years
## 6           -1.776900 15-24 years

Step:7 Visualization

Barplot of Light_Condn_ID

lighting<-table(mydata1$Light_Cond_ID)
barplot(lighting, main="Lighting Condition During Crashes", col= "purple")

## Barplot of Rode_Type_ID
Roadtype<-table(mydata1$Road_Type_ID)
barplot(Roadtype, main="Road Type in which the crash occured", col= "Yellow")

## Histogram of Crash_Speed_LimitCat
ggplot(mydata1,aes(x=Crash_Speed_LimitCat))+geom_histogram(binwidth=400, stat="count",col="brown")+labs(title = "Histogram for Crash Speed Limit", x="Crash Speed Limit",y="frequency")
## Warning in geom_histogram(binwidth = 400, stat = "count", col = "brown"):
## Ignoring unknown parameters: `binwidth`, `bins`, and `pad`

## Histogram of Traffic Volume
hist(mydata1$TrafVol,main="Traffic Volume During Crashes",xlab="Traffic",col="pink",border="black")

## Histogram of GenMale
hist(mydata1$GenMale,main="Gender of Person Driving During Crashes",xlab="Gender",col="green",border="black")

##Boxplot for Road_Type_ID vs TrafVol
ggplot(mydata1, aes(x=Road_Type_ID,y=TrafVol))+geom_boxplot(fill="red")+labs(title="Traffic Volume by Road Type", x="Road Type",y="Traffic Volume")

##Box plot of Crash_Speed_LimitCat and Wthr_Condn_ID
ggplot(mydata1,aes(x=Wthr_Cond_ID,y=as.numeric(gsub("[^0-9]","",Crash_Speed_LimitCat))))+geom_boxplot(fill="purple",color="pink")+theme_bw()
## Warning: Removed 62 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

## Scatter plot for GenMale versus TrafVol
ggplot(mydata1,aes(x=Road_Cls_ID,y=TrafVol, ))+geom_point(color="blue",alpha=0.7)+labs(title="scatter plot of Gender vs Traffic Volume ", x="Road_Cls_ID",y="Traffic Volume",)

## Heatmap of Road_Algn versus Harm_Event_ID
heatmap_file<-table(mydata1$Harm_Evnt_ID,mydata1$Road_Algn_ID)
heatmap_dataframe<-melt(heatmap_file)
ggplot(heatmap_dataframe,aes(x=Var1,y=Var2,fill=value))+geom_tile()+theme_bw()+labs(title="heatmap of Road Algn versus Harm Event", x="Harm_Event_ID",y="Injury Severity")

##Heatmap of Road_Algn_ID versus Crash_Speed_LimitCat
heatmap_file1<-table(mydata1$Road_Algn_ID,mydata1$Crash_Speed_LimitCat)
heatmap_dataframe<-melt(heatmap_file1)
ggplot(heatmap_dataframe,aes(x=Var1,y=Var2,fill=value))+geom_tile()+theme_bw()+labs(title="heatmap of Road Algn versus Crash Speed", x="Raod_Algn_ID",y="Crash_Speed_LimitCat")

#Comments:

  1. From the data analysis we can conclude that most of the crashes occur during daylight condition and in the 2 way 2 lane roads.

  2. Most of the crashes occurs when the vehicle is in the speed of 45-60mph.

  3. Greater number of crashes seem to occur when the Traffic volume is around 20,000.

  4. It can be seen from the data, that mostly male are the victim of crashes.