Clustering predictions using R language analysis
This applies to: Managed Dashboards, Managed Reports
This article shows an example of using R for analysis, creating clusters using a K-means model. K-means clustering aims to partition a number (n) of observations into a number (k) of clusters, in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. Clustering can help identify different groups in your data that should receive special treatment based on their characteristics (for example, a defined custom marketing campaign for a certain cluster).
Prepare the data
The following example uses the Iris data set. This data set contains sepal and petal measurements, which are used to predict the species of the iris plant by classifying the data points into different clusters.
If necessary, first install and configure R and Rserve.
Create the data cube using Rserve
Create a data cube that retrieves your data from any source, such as a database, Excel file, or a flat file such as the iris data file provided in CSV format. The following shows the iris data in our example with column names assigned:
Using the Insert Other option in the toolbar, add an R Language Analysis transform after the Select transform.
Configure the R Language Analysis transform and add the following script:
This script is using the K-means clustering algorithm, which requires multiple arguments and defines the number of clusters to generate out of those variables.
In the same configuration panel, click Define Element Placeholders and add the placeholders for all the arguments defined in the script. Make sure the column names match with something that the R script recognizes.
Open the data preview. You can see that the new result.cluster column, which is classifying each data point into clusters 1, 2, or 3.
Using SQL Server R Services
Alternatively, if you are using a database such as SQL Server or Azure SQL with R Services or Machine Learning Services, you may be able to run R script as part of the SQL query in a Manual Select transform.
Create a Manual Select data cube using the following script:
This script specifies the language (i.e., R) and executes the K-means algorithm as a standard stored procedure. It also includes the Select statements for retrieving the data points from the database.
The same result is obtained as with using Rserve:
Use the results
From the data cube created above, select the two measures, and add Sample ID to Rows in the Data Analysis Panel of a metric set. Add the Predicted Cluster dimension to Columns, so that each Sample ID is grouped by the cluster.
Revisualize to a Scatter Plot. You will see that all the samples are grouped by cluster.
See also
R resources:
- The R Project for Statistical Computing
- The Comprehensive R Archive Network
- An Introduction to R
- The R Datasets Package
Comments
0 comments
Please sign in to leave a comment.