CalcOptimalNumClustersForKMeans: Generate plots to help decide optimal number of clusters for...

Description Usage Arguments Details Value See Also Examples

View source: R/clustering.R

Description

Multiple methods are included for assessing optimal number of clusters.

Usage

1
CalcOptimalNumClustersForKMeans(inputted.data, clustering.columns)

Arguments

inputted.data

A dataframe.

clustering.columns

Name of columns with data to use for kmeans clustering.

Details

Function generates plots that allow you to evaluate optimal number of clusters using 3 different methods:

  1. Elbow method: https://www.datanovia.com/en/lessons/determining-the-optimal-number-of-clusters-3-must-know-methods/. Plot intra-cluster variation. Pick the K (cluster) corresponding to a sharp decrease (the elbow).

  2. Calinski-Harabasz Index (ch) method: https://www.datasciencecentral.com/profiles/blogs/machine-learning-unsupervised-k-means-clustering-and and https://medium.com/@haataa/how-to-measure-clustering-performances-when-there-are-no-ground-truth-db027e9a871c

  3. Average Silhouette Width (asw) method: https://www.datasciencecentral.com/profiles/blogs/machine-learning-unsupervised-k-means-clustering-and and https://medium.com/@haataa/how-to-measure-clustering-performances-when-there-are-no-ground-truth-db027e9a871c

ch and asw both produce a score. Higher score = better defined cluster. Maximize between cluster dispersion and minimize within cluster dispersion. Can plot the score versus the number of clusters.

Value

A list is returned with two elements:

  1. ggplot for elbow plot.

  2. ggplot for ch and asw plot.

See Also

Other Clustering functions: GenerateParcoordForClusters(), HierarchicalClustering(), generate.2D.clustering.with.labeled.subgroup(), generate.3D.clustering.with.labeled.subgroup(), generate.plots.comparing.clusters()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
example.data <- data.frame(x = c(18, 21, 22, 24, 26, 26, 27, 30, 31,
                                 35, 39, 40, 41, 42, 44, 46, 47, 48, 49, 54, 35, 30),
                          y = c(10, 11, 22, 15, 12, 13, 14, 33, 39, 37, 44,
                                27, 29, 20, 28, 21, 30, 31, 23, 24, 40, 45))

#dev.new()
plot(example.data$x, example.data$y)

#Results should say that 3 clusters is optimal
output <- CalcOptimalNumClustersForKMeans(example.data, c("x", "y"))

elbow.plot <- output[[1]]

ch.and.asw.plot <- output[[2]]

#dev.new()
elbow.plot

#dev.new()
ch.and.asw.plot

yhhc2/machinelearnr documentation built on Dec. 23, 2021, 7:19 p.m.