This is going to serve as a sort of primer for my r packge {healthyR.ai}. The goal of this package is to help with producing uniform machine learning/ai models either from scratch or by way of one of the boilerplate functions.
This particular article is going to focus on k-means clustering with umap projection and visualization.
First things first, lets load in the library:
library(healthyR.ai)
== Welcome to healthyR.ai ===========================================================================
If you find this package useful, please leave a star:
https://github.com/spsanderson/healthyR.ai'
If you encounter a bug or want to request an enhancement please file an issue at:
https://github.com/spsanderson/healthyR.ai/issues
Thank you for using healthyR.ai
Information
K-Means is a partition algorithm initially designed for signal processing. The goal is to partition n observations into k clusters where each n is in k. The unsupervised k-means algorithm has a loose relationship to the k-nearest neighbor classifier, a popular supervised machine learning technique for classification that is often confused with k-means due to the name. Applying the 1-nearest neighbor classifier to the cluster centers obtained by k-means classifies new data into the existing clusters.
The aim of this post is to showcase the use of the healthyR.ai wrapper for the kmeans function along with the wrapper and plot for the uwot::umap projection function. We will go through the entire workflow from getting the data to getting the final UMAP plot.
Now that we have our data we need to generate what is called a user item table. To do this we use the function hai_kmeans_user_item_tbl which takes in just a few arguments. The purpose of the user item table is to aggregate and normalize the data between the users and the items.
The data that we have generated is going to look for clustering amongst the service_lines (the user) and the payer_grouping (item) columns.
The table is aggregated by item for the various users to which the algorithm will be applied.
Now that we have this data we need to find what will be out optimal k (clusters). To do this we need to generate a table of data that will have a column of k and for that k apply the k-means function to the data with that k and return the total within sum of squares.
To do this there is a convienent function called hai_kmeans_mapped_tbl that takes as its sole argument the output from the hai_kmeans_user_item_tbl. There is an argument .centers where the default is set to 15.
As we see there are three columns, centers, k_means and glance. The k_means column is the k_means list object and glance is the tibble returned by the broom::glance function.
As stated we use the tot.withinss to decide what will become our k, an easy way to do this is to visualize the Scree Plot, also known as the elbow plot. This is done by ploting the x-axis as the centers and the y-axis as the tot.withinss.
Scree Plot and Data
hai_kmeans_scree_plt(.data = kmm_tbl)
If we want to see the scree plot data that creates the plot then we can use another function hai_kmeans_scree_data_tbl.
With the above pieces of information we can decide upon a value for k, in this instance we are going to use 3. Now that we have that we can go ahead with creating the umap list object where we can take a look at a great many things associated with the data.
UMAP List Object
Now lets go ahead and create our UMAP list object.