Time Series Forecasting For Road Accidents in UK

In this article, We aim to predict the number of future road accidents in the UK. The data has used from the Department for Transport (GB) [1]. This data is providing detailed road safety information about the circumstances of personal injury road accidents in GB from 2014 to 2017. We are going to implement Time Series Forecasting using ARIMA & Prophet to find out the number of road accidents in the future.

What is Time Series?

Time Series is a set of observations on the values that a variable takes at different times. For example Sales Trend, Stock Market Price, Weather Forecast etc. Time Series is using to predict the future values based on the previous observed value.

Component of Time Series

  • Trend: Tend may show the growth or decline in a time series over a long period. This is the type of tendency which continues to persist for a very long period. Prices and export and import data, for example, reflect obviously increasing tendencies over time.
  • Seasonality: These are short term movements occurring in data due to seasonal factors. The short term is generally considered as a period in which changes occur in a time series with variations in weather or festivities
  • Irregularity: These are sudden changes occurring in a time series which are unlikely to be repeated. They are components of a time series that cannot be explained by trends, seasonal or cyclic movements. These variations are sometimes called residual or random components.
  • Cyclic: These are long term oscillations occurring in a time series. These oscillations are mostly observed in economic data and the periods of such oscillations are generally extended from five to twelve years or more. These oscillations are associated with well-known business cycles.[2]

What is ARIMA model?

ARIMA stands for Auto Regressive Integrated Moving Average. There are seasonal and Non-seasonal ARIMA models that can be used for forecasting. An ARIMA model is characterized by 3 terms: p, d, q where p is the order of the AR term, q is the order of the MA term and d is the number of differences required to make the time series stationary. If a time series, has seasonal patterns, then you need to add seasonal terms and it becomes SARIMA, short for ‘Seasonal ARIMA’. More on that once we finish ARIMA. [3]
let’s get our environment ready with the libraries we’ll need and then import the data!
Check out the Data
Convert the Date column to Date type
Sorting the data by Date
Set the Date for index
Let’s extract the average number of accidents in each month.
Let’s visualise the average number of accident in each month.
Let’s visualise the data using time-series decomposition that allows us to decompose our time series into three distinct components: trend, seasonality, and noise.
Examples of parameter combinations for seasonal ARIMA
This step is parameter Selection for our ARIMA Time Series Model. Our goal here is to use a “grid search” to find the optimal set of parameters that yields the best performance for our model.
The above output suggests that SARIMAX (1, 1, 1)x(1, 1, 0, 12) yields the lowest AIC value of 186.26. Therefore we should consider this to be an optimal option.

Fitting the ARIMA Model

We need to always run model diagnostics to investigate any unusual behavior.

Evaluation of Forecasts

In order to understand the accuracy of our forecasts, we compare predicted number of accidents to the real number of accidents of the time series, and we set forecasts to start at 2017–01–01 to the end of the data.
Let’s find out the MSE to see the accuracy of our model. The mean squared error (MSE) is largely used as a metric to determine the performance of an algorithm. In addition, MSE is the average of the square of the difference between the observed and predicted values of a variable.

Visualising Forecasts

As we can see in the below graph the number of road accident in UK will be declined in next years.

What is Prophet model?

Prophet is an open source Time Series Forecasting Algorithm from Facebook and it designed for ease of use without expert knowledge on Time Series Forecasting or Statistics. Time Series Forecasting builds model by finding a best smooth line which can be represented as sum of the following component:
  • Overal Growth Trend
  • early Seasonality
  • Weekly Seasonality
  • Holiday Affects
The benefit of Prophet Approach:
  • Uneven time interval between data is not a problem
  • Day with NA is not a problem
  • Seasonality with multiple periods (Week & Year) is handled by default
  • Works well by default setting, parameters are easily interpretable
Let’s sort the values by Date
Prophet requires the variable names in the time series to be:
  • y — Target
  • ds — Datetime
So, the next step is to convert the dataframe according to the above specifications
Let’s visualise the number of road accident for each day

Fitting the Prophet model

Set the uncertainty interval to 95% (the Prophet default is 80%)
To create a forecast with our model we need to create some futue dates. Prophet provides us with a helper function called make_future_dataframe. We pass in the number of future periods and frequency. Above we created a forecast for the next 36 month or 3 years.
As we can see in the below table, yhat is our forecasted value.
Let’s create a graph of our actuals and forecast
plot_components provides us a graph of our trend and seasonality

Model Evaluation


Many thanks for reading my article!!!

References:

[2]. Cleveland, R. B., Cleveland, W. S., McRae, J. E., & Terpenning, I. J. (1990). STL: A seasonal-trend decomposition procedure based on loess. Journal of Official Statistics6(1), 3–33.
[3]. Hyndman, R. J., & Khandakar, Y. (2008). Automatic time series forecasting: The forecast package for R. Journal of Statistical Software27(1), 1–22.

Towards Data Science

Sharing concepts, ideas, and codes.

You're following Towards Data Science.

You’ll see more from Towards Data Science across Medium and in your inbox.

WRITTEN BY

Data Scientist

Comments

Popular posts from this blog

Easy Text-to-Speech with Python

Flutter for Single-Page Scrollable Websites with Navigator 2.0

Better File Storage in Oracle Cloud