Hi,

If you also would like to enhance SAP Predictive Analysis with further algorithms based on R this example might be something for you.

Do yourself a favor and test that your R function is working before adding it to SAP PA.

1) Example of using k-means with the famous iris data:

Run this in your R editor.

## Data frames are created by the data.frame function:
## dataFrame<-iris stores the iris data in the dataFrame
## Data frames are created by the data.frame function:

dataFrame<-iris
attach(dataFrame)
set.seed(4321)
str(dataFrame)
kmeans_model<-kmeans(data.frame(Sepal.Length,Sepal.Width,Petal.Length,
+Petal.Width), centers=3,iter.max=100,nstart=1,algorithm=”Hartigan-Wong”)
kmeans_model$cluster
detach(dataFrame)

This gives you a fine sets of clusters:

Rstudio.jpg

2) If SAP Predictive Analysis is to use this R script it needs to be converted into a function:

## Transforming this R script into a Function.
## For SAP Predictive Analysis
kmeansfunction<-function(dataFrame,ind,ClusterNumber,Iterations)
  {
  attach(dataFrame)
  dataFrame
  dataFrame<-data.frame(dataFrame,check.names = FALSE)
  set.seed(4321)
  kmeans_model<-kmeans(data.frame(dataFrame[,ind]),centers=ClusterNumber,iter.max=Iterations,nstart=1)
  output<- cbind(dataFrame,kmeans_model$cluster);

  return (list(out=output,modelkmeans=kmeans_model));
}

Guide: SAP Predictive Analysis  – Adding further data mining algorithms & visualizations.

3) Testing. Do yourself a favor and test the R function before trying to load it into PA.

## Make sure you have data in your dataFrame

str(dataFrame)

STR.png

## Testing the new R function before embedding in Predictive Analysis

kmeansfunction(dataFrame,c(“Sepal.Length”,”Sepal.Width”,”Petal.Length”,”Petal.Width”), ClusterNumber=3, Iterations=100)

Calling_Function.png

## OR just the short version
ind <- c(“Sepal.Length”,”Sepal.Width”,”Petal.Length”,”Petal.Width”)
kmeansfunction(dataFrame,ind,”Petal.Width”, ClusterNumber=3, Iterations=100)

Your R Function is now ready to be used in SAP Predictive Analysis.

For a video on how do use custom R scripts and enhance SAP PA even further please look at this: http://scn.sap.com/docs/DOC-46202

Best regards,

Kurt Holst

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply