<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/2015-us-fatal-vehicle-accidents-analysis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nycdatascience.com/blog/r/2015-us-fatal-vehicle-accidents-analysis/"> R – NYC Data Science Academy Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>—<br>
title: “2015 US Fatal Vehicle Accidents Analysis”<br>
author: “Matthew Sun”<br>
date: “2/01/2018”<br>
output:<br>
html_document: default<br>
—</p>
<p><br><br>
<br></p>
<p>#### Introduction</p>
<p> </p>
<p>There are over 5 millions of vehicle accidents reported in the US in 2015, according to data from National Highway Traffic Safety Administration (NHTSA). About 7% of them are fatal accidents. As the new technology of artificial intelligence going to mature in the next one or two decades, self-driving vehicles will become part of a normal life in human society. I am just wondering how this new technology can help reduce the car accidents on the road.</p>
<p><br><br>
<br></p>
<p>#### Data Overview</p>
<p> </p>
<p>There is a dataset on the NHTSA website for 2015 fatal accident: ftp.nhtsa.dot.gov. It reports the fatal incidents in major US cities, counties and states. It also includes the longitude and latitude of the accidents so the data can be visualized on the map. The state, county and city are recorded in FIPS codes. After proper data cleaning and re-arrangement, the following data fields are used in the analysis.</p>
<p>* State<br>
* Population<br>
* County<br>
* City<br>
* Longitude<br>
* Latitude<br>
* Month<br>
* Day<br>
* Hour<br>
* Time of Day<br>
* Day Week<br>
* Day of Week<br>
* Collision Manner<br>
* Road Function<br>
* Interstate<br>
* Junction<br>
* Trafficway<br>
* Vehicles Involved<br>
* Drunken<br>
* Road Departure<br>
* Weather</p>
<p><br><br>
<br></p>
<p>#### Geographical Analysis</p>
<p> </p>
<p>Each fatal accident can be represented on the US geographical map as a blue circle dot, by its longitude and latitude number. By slicing the data according to the combination of chosen state, including all states as an option, the fatal accidents can be visually inspected on US map in each state. The data can also be sliced by a timeframe of continuous number of hours ranging from one to 24. All the vehicle accident volumes on the map are aggregated into annual summation. The volume of the accidents can be differentiated by the intensity of blue color in each dot. By hovering the mouse over each dot, it will show a pop up message with the county name, a total number of vehicle accidents within the chosen timeframe and the state name.</p>
<p><br><br>
“`{r map, message=FALSE, include=FALSE}<br>
knitr::opts_chunk$set(eval = TRUE, echo=TRUE)<br>
library(dplyr)<br>
library(data.table)<br>
library(geojsonio)<br>
library(leaflet)<br>
library(shiny)<br>
library(ggplot2)<br>
setwd(‘~/NYC_data_science/projects/’)<br>
accidents &lt;-fread(file = ‘./project1/vehicle_accidents.csv’)<br>
collision_total &lt;-accidents %>% filter(Hour &lt; 24) %>%<br>
select(Hour, Collision_manner, Road_function)</p>
<p>hour_range &lt;-seq(from = 0, to = 1)<br>
state_range &lt;-c(‘California’, ‘New York’)<br>
accidents_selection &lt;-accidents %>% filter(State %in% state_range &amp; Hour %in% hour_range) %>%<br>
select(State, State_ab, Longitude, Latitude, County, Hour, N_state, N_nation,<br>
Density_state, Density_nation) %>% group_by(State) %>%<br>
mutate(N_hour_state = length(State), Ratio_state = N_hour_state/N_state) %>%<br>
ungroup()</p>
<p>accidents_temp &lt;-accidents_selection %>% select(State, N_hour_state, N_nation) %>%<br>
distinct() %>% mutate(N_hour_nation = sum(N_hour_state),<br>
Ratio_nation = N_hour_nation/N_nation) %>%<br>
select(-N_nation, -N_hour_state)<br>
accidents_selection &lt;-accidents_selection %>% left_join(., accidents_temp, by = ‘State’) %>%<br>
group_by(State) %>% arrange(desc(N_hour_state))</p>
<p>states &lt;-geojson_read(‘./project1/us_population_density.json’, what = “sp”)<br>
bins &lt;-c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)<br>
pal &lt;-colorBin(“YlOrRd”, domain = states$density, bins = bins)</p>
<p>leaflet(data = states) %>% addProviderTiles(“Esri.WorldStreetMap”) %>%<br>
setView(lng = -93.85, lat = 37.45, zoom = 4) %>%<br>
clearMarkers() %>%<br>
clearShapes() %>%<br>
addPolygons(<br>
fillColor = ~pal(density),<br>
weight = 2,<br>
opacity = 1,<br>
color = “white”,<br>
dashArray = “3”,<br>
fillOpacity = 0.5) %>%<br>
addCircleMarkers(data = accidents_selection, ~Longitude, ~Latitude, radius = 2,<br>
stroke = F, fillOpacity = 1, fillColor = ‘blue’,<br>
label = paste0(‘County: ‘, accidents_selection$County,<br>
‘: ‘, accidents_selection$N_state))<br>
“`<br>
<br></p>
<p>Since the population of each state varies, the pure volume comparison is only one side of the story. The US basemap is at the same time, plotted as choropleth map according to the 2012 population density of each state.</p>
<p><br><br>
“`{r graph2, echo=FALSE, fig.height=5, fig.width=8, dev=’png’}<br>
qty_vs_density &lt;-accidents %>%<br>
select(State, N_state, Density_state, Density_nation) %>%<br>
group_by(State) %>% distinct() %>% arrange(desc(N_state))</p>
<p>g &lt;-ggplot(qty_vs_density, aes(x=factor(State, levels = unique(State)), y=N_state+Density_state))<br>
g + geom_col(aes(fill=State)) + guides(fill=F) +<br>
theme(axis.text.x = element_text(angle = 50, hjust = 1, size = 6.5)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘US States’) +<br>
ggtitle(‘US Fatal Accidents Distribution – Quantity vs Density’) +<br>
theme(plot.title = element_text(hjust = 0.5)) +<br>
geom_col(aes(x=factor(State, levels = unique(State)),y=Density_state),<br>
fill = ‘black’, alpha = 0.9, size = 0.25) +<br>
geom_hline(aes(yintercept =Density_nation),<br>
linetype = ‘solid’, color = ‘red’, size = 0.5) +<br>
annotate(‘text’, x=’Vermont’, y = 480, label = ‘National Density\nLine (pp3m) – Red’,<br>
size = 2.4, color = ‘red’)<br>
“`<br>
<br></p>
<p>The bar chart visualizes the fatal vehicle accidents volume against the accident density, which is normalized by the population of each state. Since the density number is extremely low, I used parts per 3 million for visualization comparison. The density of each state is plotted as black color, against a red line, which is national average density number.</p>
<p>From the bar chart, only South Carolina and Mississippi present both relative high volume and high density. The remaining majority of high volume states either align with or lower than the national average, after normalized with the population.</p>
<p><br><br>
<br></p>
<p>#### Cause Analysis</p>
<p> </p>
<p>These three charts demonstrate the three major reasons that have caused the fatal accidents.</p>
<p><br><br>
“`{r graph4, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}<br>
collision_rdeparture &lt;-accidents %>% group_by(Collision_manner, Road_departure) %>%<br>
summarise(Num_collisions = n()) %>% group_by(Collision_manner) %>%<br>
mutate(Total = sum(Num_collisions)) %>%<br>
arrange(Total, Num_collisions)</p>
<p>g &lt;-ggplot(collision_rdeparture, aes(x = factor(Collision_manner,<br>
levels = unique(Collision_manner)), y = Num_collisions))<br>
g + geom_bar(aes(fill = Road_departure), stat = ‘identity’, position = ‘dodge’) +<br>
theme(axis.text.x = element_text(angle = 50, hjust = 1, size = 8.5)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Collision Manner’) +<br>
ggtitle(‘Impact of Road Departure on Fatal Accidents’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 11)) +<br>
guides(fill = guide_legend(‘Road Departure’)) +<br>
theme(legend.title = element_text(size = 8),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.2,’cm’),<br>
legend.text = element_text(size = 7))<br>
“`<br>
<br></p>
<p> </p>
<p>Road Departure away from the road is the major contributor towards the stationary collision and Head-on collision.</p>
<p><br></p>
<p>“`{r graph5, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}</p>
<p>collision_drunken &lt;-accidents %>% group_by(Collision_manner, Drunken) %>%<br>
summarise(Num_collisions = n()) %>% group_by(Collision_manner) %>%<br>
mutate(Total = sum(Num_collisions)) %>%<br>
arrange(Total, Num_collisions)</p>
<p>g &lt;-ggplot(collision_drunken, aes(x = factor(Collision_manner,<br>
levels = unique(Collision_manner)), y = Num_collisions))<br>
g + geom_bar(aes(fill = Drunken), stat = ‘identity’, position = ‘dodge’) +<br>
theme(axis.text.x = element_text(angle = 50, hjust = 1, size = 8.5)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Collision Manner’) +<br>
ggtitle(‘Impact of Drunken on Fatal Accidents’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 11)) +<br>
guides(fill = guide_legend(‘Drunken – BAC’)) +<br>
theme(legend.title = element_text(size = 8),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.2,’cm’),<br>
legend.text = element_text(size = 7))<br>
“`</p>
<p><br></p>
<p>Drunken represented only 25% of stationary collision.</p>
<p><br></p>
<p>“`{r graph6, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}<br>
collision_weather &lt;-accidents %>% group_by(Weather, Collision_manner) %>%<br>
summarise(Num_collisions = n()) %>% group_by(Weather) %>%<br>
mutate(Total = sum(Num_collisions)) %>%<br>
arrange(Total, Num_collisions)</p>
<p>top_5_weather &lt;-accidents %>% select(Weather, Collision_manner) %>%<br>
group_by(Weather) %>% summarise(Num_collisions = n()) %>%<br>
distinct() %>% top_n(5, wt = Num_collisions)</p>
<p>collision_weather &lt;-collision_weather %>% filter(Weather %in% top_5_weather$Weather)</p>
<p>g &lt;-ggplot(collision_weather, aes(x = factor(Weather,<br>
levels = unique(Weather)), y = Num_collisions))<br>
g + geom_bar(aes(fill = Collision_manner), stat = ‘identity’, position = ‘dodge’) +<br>
theme(axis.text.x = element_text(angle = 50, hjust = 1, size = 8.5)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Weather’) +<br>
ggtitle(‘Impact of Weather on Fatal Accidents – Top 5’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 11)) +<br>
guides(fill = guide_legend(‘Collision Manner’)) +<br>
theme(legend.title = element_text(size = 8),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.2,’cm’),<br>
legend.key.height = unit(0.25,’cm’),<br>
legend.text = element_text(size = 7))<br>
“`<br>
<br></p>
<p>Surprisingly, the highest number of vehicle accidents, about two thirds of them are under clear weather condition, and one of the involving vehicles was in stationary position. Definitely weather is not the major reason why accidents happened.</p>
<p><br><br>
<br></p>
<p>#### Trend Analysis</p>
<p> </p>
<p>The collision manner time distribution by the hour clearly shows the top three collision manner, which is stationary, angle and head-on. I will investigate these three categories in more details.</p>
<p><br><br>
“`{r graph3, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}<br>
collision_qty &lt;-collision_total %>%<br>
group_by(Hour,Collision_manner) %>% summarise(Num_collisions =n())</p>
<p>g &lt;-ggplot(collision_qty, aes(x=Hour, y=Num_collisions))<br>
g + geom_area(aes(fill = Collision_manner)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Hour’) +<br>
ggtitle(‘Collision Manner Time Distribution by Hour’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 13)) +<br>
guides(fill = guide_legend(‘Collision Manner’)) +<br>
theme(legend.title = element_text(size = 9),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.3,’cm’),<br>
legend.key.height = unit(0.35,’cm’),<br>
legend.text = element_text(size = 8))<br>
“`</p>
<p><br></p>
<p>The next three area maps plot how each collision manner volume varies with the time during the day.</p>
<p><br><br>
“`{r graph7, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}<br>
collision_station &lt;-collision_total %>%<br>
filter(Collision_manner==’Stationary collision’) %>%<br>
group_by(Hour, Road_function) %>%<br>
summarise(Num_collisions =n())</p>
<p>g &lt;-ggplot(collision_station, aes(x=Hour, y=Num_collisions))<br>
g + geom_area(aes(fill = Road_function)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Hour’) +<br>
ggtitle(‘Trend for Collision Manner – Stationary Collision’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 11)) +<br>
guides(fill = guide_legend(‘Road Function’)) +<br>
theme(legend.title = element_text(size = 8),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.2,’cm’),<br>
legend.key.height = unit(0.25,’cm’),<br>
legend.text = element_text(size = 7))<br>
“`<br>
<br></p>
<p>For stationary collision, the peak happened around 8:00 to 9:00pm. The second peak happened at midnight.</p>
<p><br><br>
“`{r graph8, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}<br>
collision_angle &lt;-collision_total %>%<br>
filter(Collision_manner==’Angle’) %>%<br>
group_by(Hour, Road_function) %>%<br>
summarise(Num_collisions =n())</p>
<p>g &lt;-ggplot(collision_angle, aes(x=Hour, y=Num_collisions))<br>
g + geom_area(aes(fill = Road_function)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Hour’) +<br>
ggtitle(‘Trend for Collision Manner – Angle’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 11)) +<br>
guides(fill = guide_legend(‘Road Function’)) +<br>
theme(legend.title = element_text(size = 8),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.2,’cm’),<br>
legend.key.height = unit(0.25,’cm’),<br>
legend.text = element_text(size = 7))<br>
“`<br>
<br></p>
<p>For angle collision manner, the highest peak happened between 3:00 ~ 6:00pm evening rush hour. The second peak between 10:00am to 12:00pm.</p>
<p><br><br>
“`{r graph9, echo=FALSE, fig.height=4, fig.width=7, dev=’png’}<br>
collision_headon &lt;-collision_total %>%<br>
filter(Collision_manner==’Head-On’) %>%<br>
group_by(Hour, Road_function) %>%<br>
summarise(Num_collisions =n())</p>
<p>g &lt;-ggplot(collision_headon, aes(x=Hour, y=Num_collisions))<br>
g + geom_area(aes(fill = Road_function)) +<br>
ylab(‘No. of Accidents’) +<br>
xlab(‘Hour’) +<br>
ggtitle(‘Trend for Collision Manner – Head-On’) +<br>
theme(plot.title = element_text(hjust = 0.5, size = 11)) +<br>
guides(fill = guide_legend(‘Road Function’)) +<br>
theme(legend.title = element_text(size = 8),<br>
legend.position = ‘bottom’,<br>
legend.key.size = unit(0.2,’cm’),<br>
legend.key.height = unit(0.25,’cm’),<br>
legend.text = element_text(size = 7))<br>
“`<br>
<br></p>
<p>The head-on collision manner happened during morning and evening rush hours, namely between 6:00 to 8:00am, and between 2:00pm to 6:00pm.</p>
<p><br><br>
<br></p>
<p>#### Conclusion</p>
<p>The surprising findings from the analysis are that two thirds of the total fatal vehicle accidents happened in stationary manner under clear weather condition. During morning and evening rush hours, when the driving conditions are much more complicated, human beings actually did a much better job navigating through the busy traffic. This pattern fits well with self-driving technology, which can handle simple, repetitive tasks much more competently and effectively than human beings. I would expect the number of vehicle fatal accidents dramatically reduced with the advent of self-driving technology within the next one or two decades.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://nycdatascience.com/blog/r/2015-us-fatal-vehicle-accidents-analysis/"> R – NYC Data Science Academy Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/0QRVVHlf6kg" height="1"></a></div>