Skip to content

Instantly share code, notes, and snippets.

@espio999
Last active September 12, 2021 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espio999/da7e7b08088875b17dd1cdbdb09c4a8a to your computer and use it in GitHub Desktop.
Save espio999/da7e7b08088875b17dd1cdbdb09c4a8a to your computer and use it in GitHub Desktop.
Clustering - identify number of clusters, analysis, and plot
library(NbClust)
library(factoextra)
library(Rmisc)
mydata = scale(LifeCycleSavings)
myAHCnum = NbClust(mydata, method = "ward.D", index = "all")
myNHCnum = NbClust(mydata, method = "kmeans", index = "alllong")
fig1 = fviz_nbclust(myAHCnum, method = "silhouette")
fig2 = fviz_nbclust(myNHCnum, method = "gap_stat", nboot = 100)
multiplot(fig1, fig2)
fig3 = fviz_nbclust(mydata, FUNcluster = hcut, method = "silhouette", verbose = FALSE)
fig4 = fviz_nbclust(mydata, FUNcluster = hcut, method = "wss", verbose = FALSE)
fig5 = fviz_nbclust(mydata, FUNcluster = kmeans, method = "gap_stat", nboot = 100, verbose = FALSE)
multiplot(fig3, fig4, fig5)
mykmeans = function(c, d){
myAHC = hcut(d, k = c, stand = TRUE, graph = FALSE)
myNHC = kmeans(scale(d), c, iter.max = 100, nstart = nrow(d))
fig_a = fviz_dend(myAHC, rect = TRUE)
fig_b = fviz_silhouette(myAHC, label = TRUE, rotate = TRUE, print.summary = FALSE)
fig_c = fviz_cluster(myNHC, data = d)
multiplot(fig_a)
multiplot(fig_b)
multiplot(fig_c)
}
for (i in c(2, 6, 15)){
mykmeans(i, mydata)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment