Time Series Analysis Using ARIMA Model With Python
By: Dilina Hirantha
Tags: Pandas, Python, Time Series Analysis, Forecasting, Arima

Time series is a sequence of time-based data points collected at specific intervals of a given phenomenon that undergoes changes over time. In other words, time series is a sequence taken at consecutive equally spaced points in the time period.

As a example, we can present few time series data sets in different domains such as pollution levels, Birth rates, heart rate monitoring, global temperatures and Consumer Price Index etc. At the processing level, above datasets are tracked, monitored, down sampled, and aggregated over time.

There are different kind of time series analysis techniques in the big data analytical field. Among them few are,

Autoregression (AR)

Moving Average (MA)

Autoregressive Moving Average (ARMA)

Autoregressive Integrated Moving Average (ARIMA)

Seasonal Autoregressive Integrated Moving-Average (SARIMA)

ARIMA Model

ARIMA Model is simple and flexible enough to capture relationship we would see in the data and It aims to explain the autocorrelation between the data points using past data. We can decompose the ARIMA model as follow to grab the key elements of it.

AR: Auto regression. This is a model that uses the dependent relationship between the data and the lagged data.

This is a model that uses the dependent relationship between the data and the lagged data. I: Integrated. The use of differencing of raw observations (e.g. subtracting an observation from an observation at the previous time step) in order to make the time series stationary.

The use of differencing of raw observations (e.g. subtracting an observation from an observation at the previous time step) in order to make the time series stationary. MA: Moving average. A model that uses the relationship between the observations and the residual error from the moving average model applied to lagged observations.

Dataset Explanation

For this practice, the dataset have been downloaded form the https://www.kaggle.com/sudalairajkumar/daily-temperature-of-major-cities. In this dataset, it includes daily average temperature values collected as Fahrenheit over different regions, countries, cities over the years from about 1995 to 2020.

For the preprocessing and further analytics of the dataset I have used pandas. Using pandas, we can explore, clean and process dataset by creating a pandas DataFrame.

In the preprocessing of the DataFrame we are done with few steps such as,

I have added new column as Date by combining the Year, Month and Day columns. Then dropped the Year, Month, Day, City and Region columns individually from the dataset.

by combining the Year, Month and Day columns. Then dropped the Year, Month, Day, City and Region columns individually from the dataset. Remove duplicate and null value rows from the data frame.

In this article I’m not focused to demonstrate the data preprocessing. Therefor let’s move into discussed a important fact of this observation.

In this preprocessed data frame, it has temperature values over long period for each country. So far it is so good! But, using the past temperature as far as old might not be a good indicator for today’s weather data, as there have been factors which impact to global temperature have been changing time to time.

Thus, it is better to analysis under recent influential factors, we would take only a subset of data from 2016 to 2020 for time series analysis. Also I have filtered the data frame for a specific country for the ease the analysis. Here, I have used country “Sri Lanka”.

Exploratory Analysis

First of all, you need to set the Date as the index for the data frame.

sample head records of data frame

sample tail records of data frame

Data Visualization

Next step is to visualize the data frame as a time - series plot. The plot below shows how the temperature spread through period of 2016 to 2020 of Sri Lanka. For the plotting I have imported matplotlib.pylot.

As you can see it is bit noisy to read the plot, because dataset contains daily temperature and plot visualize every day all over. Therefor we need to smooth the plot using proper technique.

How we can smooth the graph?

In the data science, smoothing techniques can be used to reduce or cancel the effect of these variations and noises. When properly applied, these techniques smooth out the fine-grained variation between time steps to improve the visualize and identifying of underlying trends. As example,

Exponential Smoothing

Moving Average Smoothing

Moving Average Smoothing is a popular smoothing method frequently used in the time series analysis which is easy to apply and less prone to a lot of false signals.

Pandas has prebuilt method to easier the application of the Moving Average Smoothing as rolling(). Throughout this method you can provide specific time period as a window size. As a example window size 30 means, we use 30 days as a one window. Using this technique, we can get the mean of the dataset per window size in the period.

You can choose your window size according to your attenuation needs. In this example I used window size as 30.

Time Series Decomposition

Time series data can exhibit a variety of patterns, and it is often helpful to split a time series into several components, each representing an underlying pattern category.

If we consider above plots, there we can slightly see the common pattern in the daily average temperature in the each year. At the beginning and the end of the each year it seems like cold. But when it’s reaching to the middle of the year, temperature is getting increase gradually.

Time series decomposition have different components,

Seasonality : Repeated patterns occurs over a fixed and known period in the dataset.

: Repeated patterns occurs over a fixed and known period in the dataset. Trend : includes persistent increasing or decreasing direction in the data.

: includes persistent increasing or decreasing direction in the data. Residual : terms after the seasonal and trend components of a time series have been estimated and removed. Data which are form short term fluctuations in the series which are neither systematic nor predictable.

Here I have done additive decomposition the above three patterns.

Forecasting With SARIMA Model

The problem with plain ARIMA model is it does not support seasonality.

In this daily average dataset we could see seasonality pattern, therefore, we proceed with SARIMA model which uses seasonal differencing. Seasonal differencing is just similar to regular differencing, but, instead of subtracting successive terms, we subtract the value from previous season.

ARIMA model includes three main parameters as p, d and q.

p : The order of the autoregressive model.

: The order of the autoregressive model. q : The order of the moving average model.

: The order of the moving average model. d: The number of differences required to make the time series stationary.

The SARIMA model builds upon the ARIMA model. It also includes the p, q, and d parameters, but also an extra set of parameters to account for time series seasonality.

P: The order of the seasonal autoregressive model.

Q: The order of the seasonal moving average model.

D: The number of seasonal differences applied to the time series.

Therefor we can denote the notation of the SARIMA model as SARIMA(p,d,q)(P,D,Q,s). Here s denotes the seasonality of the year.

How we select suitable parameter combination to fit to SARIMA model ?

Selecting the best ARIMA model configuration is usually an iterative process. For that, we can use a “grid search” to iteratively explore different combinations of parameters.

For each combination of parameters, we fit a new seasonal SARIMA model with the SARIMAX() function from the statsmodels module and calculate the AIC (Akaike information criterion). AIC is used to measure the relative quality of the statsmodels.

sample of different combination of parameters with AIC values

considering the different combinations and their AIC values, we can choose a combination which have minimum value of AIC. By considering the all the values in this combinations, I could get the minimum AIC (5281.62) combination as SARIMA(1, 1, 1)*(1, 1, 1, 12)12 . The reason to select lower combination AIC because it will be the better-fit to the model.

Fit the choose combination to SARIMA model

Now we have figured out the combination. Then we can compose it to the SARIMA model as below.

Validating The SARIMA Model With Forecasting

We can forecast using to ways.

Non- Dynamic Forecasting (Static) : This method produce one-step-ahead forecasts, meaning that forecasts at each point are generated using the full history up to that point.

In this example we mainly focused on the static forecasting where it will more suitable practice than dynamic forecasting for this daily average temperature example.

By this method We compare predicted values to actual values of the time series where we can assure the accuracy of out model. Below code snippet shows that we predict the daily average temperature as providing start date as 2018–05–01.

Get prediction using static forecasting

In above code snippet, we determined training time series period as 2016 to 2018. Therefore we forecast the temperature from starting 2018 to end of the 2020 period.

As visualized, at the first glance we can see forecasted daily average temperature behave along with the actual daily average temperature. We can quantify the accuracy of our forecast by evaluating the RMSE (Mean Squared Error), in which for each predicted value, we compute its distance to the true value and square the result.

Result : The RMSE error of forecast prediction is 1.32 for this SARIMA model which is very low means we have acquired high accuracy from this model. In the ideal scenario MSE should zero. but typically it is impossible.

Dynamic Forecasting :

By the dynamic forecasting we only use information from the time series up to a certain point, and after that, forecasts are generated using values from previous forecasted time points.

Get prediction using dynamic forecasting

For the dynamic forecasting we also calculate the RMSE value as below,

Result : The RMSE error of dynamic forecast prediction is 20.45 This is significantly higher than the one-step ahead. By considering the above prediction accuracy values, we can conclude that,

STATIC forecast allow results of previous forecast to be done singly as a predictor of future inflation outcomes and this is very well tracked as the margin of error is quite small when compared to DYNAMIC forecast outcomes.