Cluster Analysis using Tableau and R – Part-2

Related article : Cluster Analysis using Tableau and R – Part-1

We have performed clustering analysis from both Tableau and R in my previous post. Tableau uses k-means algorithm for cluster analysis which partitions the data into k clusters with a center or mean value of all the points in each. Clustering is based on the distance each measure lies from the center.

Let’s look into that in detail.

First generate a cluster scatter plot in Tableau as we did in part -1 using Iris data set.

clustering_tableau_R10

Right click on the cluster that we added and choose Describe Clusters option

clustering_tableau_R11

This provides you the clustering details given below

clustering_tableau_R12

Now lets perform the k-means clustering from R and print the cluster

#copying iris to myiris variable
myiris<-iris
#Remove Species column
myiris$Species <- NULL
#clustering
kmeans.result <- kmeans(myiris,3)

#print the cluster data
kmeans.result

clustering_tableau_R13

Check the cluster means against that of Tableau cluster centers. Aren’t these comparable? However Tableau clustering analysis is limited and the default one is k-means as compared to the number of packages and functions available in R to perform various types of clustering. I will dedicate a future article to cover cluster and fpc packages in R.

In this case, what options that a Tableau user has for extended/advanced clustering? The answer is R integration by calling R packages from Tableau using similar steps that I explained in this article.

Cluster Analysis using Tableau and R – Part-1

This article introduces you to similar clustering analysis on your data using Tableau and R. Data files and source used in this post can be downloaded using the link below.

Download source files used in this article

Clustering is the assignment of a set of observations into subsets (called clusters) so that observations in the same cluster are similar in some sense. We will perform the analysis in both Tableau and R using the same data.

Clustering Analysis using Tableau

To start with, we connect Tableau to Iris data set.  You can download this from UCI Machine Learning Repository

First connect the Tableau work book to this csv data source and launch a new sheet. Drag the measures petal length, petal width to columns and sepal length, width to rows.

clustering_tableau_R

Next, disable aggregation of measures using Analysis->Aggregate Measures

clustering_tableau_R1

Alternately, to keep it simple, you can choose to analyze only 2 measures as shown below. But in this article, we go with all 4 measures as above

clustering_tableau_R2.PNG

If you observe, these scatter-plots does not identify or differentiate any groups. However in our case, the data set already has a column specifying flower species of these measures. So let us view it by dragging ‘Species’ to color which shows the distinct species groups as below:

clustering_tableau_R3

Well, imagine what if we didn’t had the ‘species’ data handy and we wanted to identify the clusters based on the measures. Lets see how it can be accomplished using Tableau Cluster Analysis.

Start with our initial plot, i.e.

clustering_tableau_R1

Go to Analytics tab, and drag ‘Clusters’ as shown in the screen capture below. Tableau automatically identifies the number of clusters.

clustering_tableau_R4

Leave the defaults

clustering_tableau_R5.PNG

clustering_tableau_R6

Note that we have got exactly same cluster grouping as we got using ‘Species’ dimensi.on data.

Tableau uses k-means algorithm for cluster analysis which partitions the data into k clusters with a center or mean value of all the points in each. Clustering is based on the distance each measure lies from the center.

Cluster Analysis using R

To start with, let us revisit Tableau plot for iris data between petal.length and petal.width with cluster analysis.

clustering_tableau_R7

Let’s go to RStudio, and plot this using ggplot (note that iris is available as part of the datasets installed with R)

library(ggplot2)
ggplot(iris, aes(Petal.Width, Petal.Length, color = Species)) + 
geom_point()

clustering_tableau_R8

Note that we get identical grouping in R plot, but we used species column data to group (color) the data.

Let us look at how to perform the cluster analysis to identify clusters in R.

First take a copy of iris dataset to another variable

#cluster analysis - Biju Paulose
#-------------------------------
#copying iris to myiris variable
myiris<-iris
#printing data 
myiris

For our analysis, we do not want to use species column/data. So lets remove that from the new dataset.

#Remove Species column
myiris$Species <- NULL
#printing data to verify
myiris

Lets use k-means function for generating 3 clusters and plot the data

kmeans.result <- kmeans(myiris,3)
# plot the clusters
plot(myiris[c("Petal.Width", "Petal.Length")],col=kmeans.result$cluster)

The result is given below.  As you can compare with the analysis performed in Tableau above, we could generate the same clustering of data from R. We will examine these more closely in my next article.

clustering_tableau_R9

Download source files used in this article

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

 

How to integrate R with Tableau

We have seen how R can be integrated to your data science project using Power BI or Visual Studio(RTVS). Now its time to look at R integration with Tableau.

Before we get started with the steps, let us discuss how this is beneficial.

Tableau is a great visualization tool which helps you to understand your data, provide interactivity and assist in making business decisions. R integration is going to bring the capabilities of to your Tableau visualizations – such as statistical functions predictive analysis. The advantage of interactive visualizations in Tableau powered by the complex statistical analysis behind the scenes using R presents a strong case for data scientists to go for this integration.

I have added a high level representation below of this implementation. You can call R functions from Tableau and it passes the result back to Tableau which can be used to generate visualizations. You can utilize all packages (difficult to accomplish using Tableau scripting alone) that are running in R Server and generate visualizations using the resultant data (complex to accomplish using R alone).

 

R_Tableau

As first step, make sure that RServe is running as a service that you can connect to. The screenshots below shows how to install RServe from RStudio.

R_Tableau_Integration

Start the service

R_Tableau_Integration_1

Now RServe is ready for connections. Go to Tableau and choose the option Help–> Settings and Performance–>Manage External Service Connection

R_Tableau_Integration_2

In this case, my RServe is running on the same PC. So I selected localhost as server. Default RServe port is 6311. Leave that as is and test your connection as below

R_Tableau_Integration_3

Above message confirms that you have established the connectivity with R service.

Next, we will look at an example calling R scripts from Tableau.

R rattle launch Error – libatk-1.0-0.dll is missing from your computer

If you encounter the below error while trying to launch rattle package in R Studio, that indicate that GTK+ is missing.

The program can’t start because libatk-1.0-0.dll is missing from your computer. Try reinstalling the program to fix this problem.

library(rattle)
Error in inDL(x, as.logical(local), as.logical(now), …) :
unable to load shared object ‘C:/Users/GEEK2/Documents/R/win-library/3.3/RGtk2/libs/x64/RGtk2.dll’:
LoadLibrary failure: The specified module could not be found.

Solution :  Re-Install package > install.packages(“rattle”)

If the popup asks for installing GTK+, select OK

R_GTKplus_istall

R_GTKplus_istall2.PNG

R Programming using RTVS – Part 4

This article assumes that you have RTVS extension added to your VS IDE.  You can refer my previous blogs on installation & configuration of RTVS.

  1. R Support in Visual Studio (RTVS)
  2. R Programming using RTVS – Part 2
  3. R Programming using RTVS – Part 3
Prerequisites:
1. Basic knowledge of R programming
2. RTVS Extension

Creating R projects using RTVS

Create a project in visual studio using R template.

RTVS_2

Open the Script.R file from solution explorer and type in your R program. In the example below, we create a simple line graph that compares sales and profit performance of a company over a period of 5 years.

#Define Data
sales <- c(2000, 3500, 3000, 4700, 6000)
profit <- c(1450, 2600, 750, 2000, 3750)
#range for axis
rnge <- range(0, sales, profit)
#Plot sales values
plot(sales, type = "o", col = "green", ylim = rnge, axes = FALSE, ann = FALSE)
#draw profit values as line
lines(profit, type = "o", pch = 22, lty = 2, col = "red")
box()
#draw X axis and y axis
axis(1, at = 1:5, lab = c("2013", "2014", "2015", "2016", "2017"))
axis(2, las = 1, at = 1000 * 0:rnge[2])
#Graph Title
title(main = "Sales by Year", col.main = "blue", font.main = 4)
#add axis labels
title(xlab = "Year", col.lab = rgb(0, 0, 1))
title(ylab = "Amount", col.lab = rgb(0, 0, 1))

Place your cursor in the first line of code and use Ctrl+Enter to step through each line to R Interactive and execute. You can see each line execution results to the R Plot window as shown below

RTVS_5_R_prioject

Here is the final plot view

RTVS_5_R_prioject_plot

Given below is an updated source which generate the output as PDF file

#Define Data
sales <- c(2000, 3500, 3000, 4700, 6000)
profit <- c(1450, 2600, 750, 2000, 3750)

#File name to save output
pdf(file = "sales_and_profit_byYr.pdf", height = 4, width = 5)

#range for axis
rnge <- range(0, sales, profit)

#Plot sales values
plot(sales, type = "o", col = "green", ylim = rnge, axes = FALSE, ann = FALSE)
#draw profit values as line
lines(profit, type = "o", pch = 22, lty = 2, col = "red")
box()

#draw X axis and y axis
axis(1, at = 1:5, lab = c("2013", "2014", "2015", "2016", "2017"))
axis(2, las = 1, at = 1000 * 0:rnge[2])

#Graph Title
title(main = "Sales by Year", col.main = "blue", font.main = 4)

#add axis labels
title(xlab = "Year", col.lab = rgb(0, 0, 1))
title(ylab = "Amount", col.lab = rgb(0, 0, 1))

#Flush the output to PDF file
dev.off()

R Programming using RTVS – Part 3

This article assumes that you have RTVS extension added to your VS IDE.  You can refer my previous blogs on installation & configuration of RTVS.

  1. R Support in Visual Studio (RTVS)
  2. R Programming using RTVS – Part 2
Prerequisites:
1. Basic knowledge of R programming
2. RTVS Extension

Key RTVS features that a developer should be aware of :

The following is just a list for you to explore rather than extensive walk through.

  1. Step through :  use Ctrl+Enter
  2. Execute a block of code in R interactive  : Select a block of code and Ctrl+ Enter (or right click and choose Execute in Interactive)
  3. Debug : Use Debug > Source startup file or F5
  4. Set Debug points : Native VS behavior (refer image below)rtvs_5_r_debug.png
  5. Reset current work space : To clear everything by using R Tools > Session > Reset
  6. Examine variables : Use ‘variable explorer’ (Ctrl + 8)
  7. IntelliSense : RTVS_5_R_IntelliSense
  8. Help : Use ? and ?? (Use ?? to search term in quotes if it include spaces)
  9. Code snippet manager :  View them at Tools > Code Snippets Manager.  RTVS_5_R_CodeSnipptType the abbreviated name and use tab to insert a snippet to your codeRTVS_5_R_CodeSnippt1
  10.  R interactive window or REPL (Read-Evaluate-Print-Loop) window : Use already illustrated in previous steps/posts
  11. Plot window : Ctrl + 6
  12. Plot history : Preserves all your plots in the session.  R Tools > Plots > Plot History Window (refer image below)RTVS_5_R_Plot_history

There are many more which an R programmer or VS developer can explore themselves. We will go through some samples using RTVS in the upcoming posts.