Time Series brief introduction and how to use interactive time series graph using dygraphs

Kailash Hari
Analytics Vidhya
Published in
5 min readJan 14, 2020

Here we are going to work on a Time Series data set that is on the inflation rate on Germany, where we are going to do some EDA work, then try to fit a time series model and plot an interactive Time Series plot using dygraphs.

Content :

  1. Introduction about the source and the data set
  2. Getting familiar with the data
  3. Outline of work
  4. Arima vs ETS method (choosing the best method)
  5. Introduction to dygraphs
  6. Plotting the interactive time-series graph with the best method.

Inflation Rate: A measure of the change in purchasing power per unit in money

The inflation rate was considered to be the lowest in Germany in December 2017.

Inflation data is very often used in time-series analysis since it :

  1. Affects investment opportunities
  2. Affects stocks prices
  3. Employment rate
  4. Oil and others.

Data Set Source :

Monthly Inflation Rate of Germany

This dataset was taken from https://www.statbureau.org/

#Monthly Inflation Rate of Germany.

Other country’s data is available to download if anyone would like to try it and analyze them.

Getting familiar with the dataset :

Once we do the EDA with the data set, there are multiple things that we come to know about :

Head for getting the first 5 values in the data set.

We can see that it is not in a time series format, R package allows us to make a time series with its inbuild package which converts the data into time-series based on the frequency of the user's input :

We can see that now we have a time series that is starting from 2008 and is represented as monthly data.

a)The data is not properly arranged.

b) no trend is present.

c)Seasonality is present.

d)Presence of negative values

Outline of the work after EDA :

As the frequency is given for monthly data and we can see that there is no trend but seasonality we do :

a)Seasonal Decomposition

b)Arima method

c)ETS method

d)Cross-validation

e)Taking the best method based on Error and plotting the graph.

A) Seasonal Decomposition:

This technique divides the data into its components :

  1. Trend
  2. Seasonality
  3. White noise

Few interesting points/guesses where seasonality is forming a pattern: We can see that during the end of the year (holidays time) there is a sudden spike in the inflation and then a slow rise in the summertime after that again it drops and spikes again during December time.

But there are several drawbacks for seasonal decomposition models like they are slow to catch fast rises.

Newer methods for seasonal decomposition : (Links provided if you guys want to take a look )

  1. X11: https://otexts.com/fpp2/x11.html
  2. Seats: https://otexts.com/fpp2/seats.htm

B)Arima Model:

Difference between a Manuel and auto arima method.

Here we can either use the Manual method for Arima where we check for the stationarity of the series and do a dickey fuller test or proceed with the auto arima method to calculate the best model as it is a smaller data set.

The difference between a seasonal and nonseasonal Arima: Seasonal Arima models have two parameters: A regular set and a second set for the seasonal part.

Inferences :

Best Model : ARIMA(1,0,2)(0,1,1)[12]

  1. (1,0,2): For the nonseasonal part we have 1 order of autocorrelation, differencing and 2 orders of moving averages.
  2. (0,1,1) : For seasonal part, we have both 1 step of differencing and moving averages and
  3. 12– observation per interval which is a year and along with them we have the coefficients to make the forecasting.

Next, we will forecast the remaining time and we shall store the value to check it against ETS.

C)ETS Model:

As we see we are autogenerating the ETS time-series to forecast our values and we are going to then compare it with the arima model.

D)Time-Series cross-validation and error :

We have to use Error metrics to compare different models, here we use the function: ts.cv()

The drop back: for longer data it would take a lot of computational time to calculate the error rate.

Calculating the error rate we will choose the best model:

Comparing the errors between the arima and ets model

It is seen that the error for the Arima model is lower then ETS, therefore we will use the model to predict the inflation for the next year.

E) DYGraphs:

An interactive plotting tool for time series models is dygraphs: a JavaScript-based method for plotting time series data.dygraphs is also available as a package for R, so you can produce interactive output as part of your R-based data exploration workflow.

The dygraphs function in R works with time-series objects, taking a ts dataset as its first argument. For this article, we’ll create a dataset for the german inflation that we have worked with:

For complete work , kindly log in to my Github account:https://github.com/kailash14/Time-Series

References :

  1. https://www.r-graph-gallery.com/316-possible-inputs-for-the-dygraphs-library.html
  2. https://otexts.com/fpp2/
  3. http://r-tutorials.com/
  4. https://codepen.io/

--

--

Kailash Hari
Analytics Vidhya

Currently pursuing Post Graduate Program in Data Science from Praxis Business School, Bangalore