R/cluster_utilities.R

Defines functions .get_silinfo

# Ge the silhouette information
# cluster: the cluster assignement of observation
# diss the dissimilarity matrix
.get_silinfo <- function(cluster, diss){
  
  k <- length(unique(cluster))
  silinfo <- list()
  if(k > 1) {
    sil.obj <- cluster::silhouette(cluster, diss)
    widths <- as.data.frame(sil.obj[, 1:3])
    rownames(widths) <- colnames(as.matrix(diss))
    widths <- widths[order(widths$cluster, -widths$sil_width), ]
    widths$cluster <- as.factor(widths$cluster)
    
    # summary
    silinfo$widths <- widths
    silinfo$clus.avg.widths <- as.vector(tapply(widths$sil_width, widths$cluster, mean))
    silinfo$avg.width <- mean(widths$sil_width)
  }
  
  silinfo
}

Try the factoextra package in your browser

Any scripts or data that you put into this service are public.

factoextra documentation built on April 2, 2020, 1:09 a.m.