Using R forecast from Tableau

Prerequisites:

  • RStudio (with forecast package installed)
  • Tableau Desktop (with connectivity established to RServe service. For details of R integration with Tableau, please refer my previous post here)
  • R Programming knowledge
Download source files used for this article

Forecasting allows business to arrive at more realistic estimates and targets for future. Tableau analytics provide the option of generating forecasts which many of you must be familiar with. In this article, we will look at how to make use of the R forecast package from Tableau. This will be a reference for the capabilities that you can bring in from R.

For a simple demonstration of R forecast, we can use the R air passenger time series data set for 2 year forecasting as below

myts1 <- ets(AirPassengers)
library(datasets)
library(forecast)
plot(forecast.ets(myts1, h=24))

tableau_r_forecast2

Tableau’s native forecasting has similar capability – an example shown below using the superstore dataset

tableau_r_forecast3.PNG

For the R integration, start a new sheet connecting to globalstore dataset and generate timeseries graph for sales (orderdate by months and Sum[Sales])

tableau_r_forecast4

To generate a forecast using R package, create a calculated field with the script as shown below

tableau_r_forecast5

SCRIPT_REAL("library(forecast);
myts <- ts(.arg1,start=c(2011,1), frequency=12);
myforecast <- forecast(myts, h=.arg2[1]);
monthsts<-length(.arg1); 
append(.arg1[(.arg2[1]+1):monthsts],myforecast$mean,after= monthsts
)",
SUM([Sales]),[Forecast Months])

The scripts creates timeseries for sales starting Jan 2011, generates forecast and appends starting x months (specified by parameter ‘Forecast months’) before the last month in the series.

You can view the forecast series by adding calculated field (SalesForecast) to the row. To make it intuitive, create the formula isForecast as below and drag to color.

tableau_r_forecast6

tableau_r_forecast7.PNG

Forecast vs Actual

To view forecast vs actual side by side, you can add sales to row

tableau_r_forecast8.PNG

But this does not give you a clear understanding or limits your ability to compare. The solution is to bring them together (dual-axis) and then synchronize both axis. The result is shown below

tableau_r_forecast9

Download source files used for this article

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s