#' elbow_method_df
#'
#' @param data Dataset usado na clusterização
#' @param seed Gerador de números pseudoaleatórios
#' @param max_clusters Número máximo de clusters a serem testados
#'
#' @return retorna um dataframe com os valores de wss e distâncias, por cluster
#' @export
#'
#' @examples
#'
#' print("exemplo")
#'
elbow_method_df <- function(data,seed=123,max_clusters=15){
lista_wss <- list()
for (i in 1:max_clusters){
print(paste("########## RODANDO CLUSTER",i,"##########"))
k <- clustMixType::kproto(data,i)
lista_wss[[i]] <- k$tot.withinss
}
x <-
tibble::tibble(
x = 1:max_clusters,
y = unlist(lista_wss)
)
cluster_distances <- function(tabela_elbow){
x1 <- 2 # min possivel cluster
y1 <- tabela_elbow$y[1] # min wss
x2 <- max(tabela_elbow$x) # max k
y2 <- tabela_elbow$y[length(tabela_elbow$y)-1]
lista_distancias <- list()
for (i in 1:dim(tabela_elbow)[1]){
x0 <- i + 2
y0 <- tabela_elbow$y[i]
numerator <- abs((y2-y1)*x0 - (x2-x1)*y0 + x2*y1 - y2*x1)
denominator <- sqrt((y2 - y1)**2 + (x2 - x1)**2)
lista_distancias[[i]] <- numerator/denominator
}
return(unlist(lista_distancias))
}
x['distances'] <- cluster_distances(x)
return(x)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.