knitr::opts_chunk$set( echo = TRUE )
library(fars)
library(readr)
library(dplyr)
library(magrittr)
library(tidyr)
library(bindrcpp)
library(maps)
library(graphics)
knitr::opts_chunk$set(comment = "#>", collapse = TRUE)

Introduction to package "fars"

The "fars" package is used to evaluate the reported accidents in the United States of America.

The statistics are collected and published by the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (NHTSF), which is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.

More information and the data files can be found on the website https://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS).

The package supports the following evaluations by means of functions:

The following functions of the "fars" package are supported and are described:

Prerequisites

  1. The following packages are used:

  2. library(fars)

  3. library(readr)
  4. library(dplyr)
  5. library(devtools)
  6. library(magrittr)
  7. library(tidyr)
  8. library(bindrcpp)
  9. library(maps)
  10. library(graphics)

  11. Download csv files from Website of NHTSF: The prerequisite for performing the evaluations is that the csv files ("accident_yyyy.csv.bz2") are downloaded with the accident data and are located in the current directory.

    Website https://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)

Read dataset with function fars_read()

The function fars_read (filename) is used to read a file (eg "accident_2015.csv.bz2") with accidents of the NHTSF. The argument of the function is the filename and the return is the tibble (or data frame).

Excample
tf <- "accident_2015.csv.bz2"
df <- fars_read( filename = tf )
str(df)

Let's have a look at an output of the first 10 table entries for some fields.

## selection of some fields 
df_sel <- dplyr::select( df, STATE, PERSONS, COUNTY, CITY, DAY, MONTH, YEAR, SP_JUR, FATALS )
knitr::kable( head(df, 10) )

Prepare fars data with function fars_summarize_years()

The function fars_summarize_years(years) is used to create a tabular representation of the accidents per month and year. The argument of the function is a vector with the years to be considered for the creation and the return is the tibble (or data frame).

Excample
yyyys <- c(2014, 2015)
df_summ <- fars_summarize_years( years = yyyys )
str(df_summ)

Here is the output for the year 2015.

knitr::kable(head(df_summ, 13))

Plot the distributation of accidents in an US state with function fars_map_state()

The function fars_summarize_years (state.num, year) is used to create a plot with the distribution of all accidents in one year on the map of a US state.

Excample
state_no <- 01
par( mfrow = c(1, 3) )
## title(main="Accidents in the US State Alabama")
yyyy <- 2013
fars_map_state( state.num=state_no, year=yyyy )
title(main="Alabama: Accidents in 2013")
yyyy <- 2014
fars_map_state( state.num=state_no, year=yyyy )
title(main="Alabama: Accidents in 2014")
yyyy <- 2015
fars_map_state( state.num=state_no, year=yyyy )
title(main="Alabama: Accidents in 2015")


ap-wi/fars documentation built on May 6, 2019, 4:37 p.m.