Timeseries Forecasting with the forecast R package and Shiny

Timeseries Forecasting with the forecast R package and ShinyAneesha BakhariaBlockedUnblockFollowFollowingFeb 6, 2016I’m more of a Python person — I love Python’s syntax, the tons of Machine Learning libraries in Python and Django (for web dev).

 …but I swap to R when necessity calls, and necessity usually takes the form of a useful library for which there is no Python alternative.

Sometimes its just a waste of time to port to another language and easier just to learn to load data and use the library.

This pretty much sums up my relationship with R.

I’ve mainly used Machine Learning algorithms (i.


, classification and clustering) and not done much with Timeseries Forecasting.

I did once investigate what was possible with Exponential Smoothing (ETS), ARIMA models and Timeseries decomposition.

I looked for a good implementation of these algorithms and found the wonderful Forecast R package written by Rob J Hyndman.

Around the same time I heard about Shiny and thought I’d see if I could learn to use both at the same time.

In under an hour I had a simple prototype UI that allowed a dataset to be selected, enter the months to forecast ahead and produced 3 graphs which included the timeseries decomposition, ARIMA forecast and ETS forecast.

I put the code up on Github as ShinyTimeseriesForecasting in 2013 and never thought much about it again.

This week (Feb 1, 2016) I came across an article about using Timeseries Forecasting on IoT datasets on Data Science Central.

It occurred to me that Timeseries Forecasting would be useful to identify trends and forecast into the future for IoT data.

This prompted me to resurrect the ShinyTimeseriesForecasting app and deploy it to shinyapps.

io — you can have a play here: https://aneesha.


io/ShinyTimeseriesForecasting/The Shiny UI to display ETS, ARIMA and Timeseries Decomposition using the Forecast R packageIt’s very easy to build a UI to explore data with using Shiny!Timeseries DecompositionOnce a data is loaded as a timeseries object, the decompose method returns the trend, seasonal, random and observed components for the dataset which can easily be plotted using plot().

library(datasets)library(forecast)ds_ts <- ts(AirPassengers, frequency=12)f <- decompose(ds_ts)plot(f)Timeseries DecompositionARIMA ForecastingThe power of the Forecast package can be illustrated by what it is able to do in 2 lines of code.


arima() finds the best ARIMA model and forecast method uses the model to forecast out to the specified time period.

library(datasets)library(forecast)fit <- auto.

arima(AirPassengers)plot(forecast(fit, h=12))ARIMA Forecast (12 months ahead)Exponential Smoothing (ETS) ForecastingThe Forecast package also includes ETS models which once fitted can also be forecast out to a specified time period and plotted.

library(datasets)library(forecast)fit <- ets(AirPassengers)plot(forecast(fit, h=12))ETS Forecast (12 months ahead).

. More details

Leave a Reply