INTRODUCCION

El presente recurso desarrolla un modelo de analítica de datos que tiene como propósito la identificación de tendencias o relaciones entre las variables de los afiliados al Sistema de Salud en el departamento de Cundinamarca, de tal modo que permita apoyar decisiones tendientes al mejoramiento de la cobertura y acceso a los servicios de salud; Se consolidó un dataset multidimensional con 13 características de los afiliados y el número de afiliados en cada variable que atiende cada prestador de salud. Para la implementación del modelo se recurrió a los algoritmos de clustering que propone la Ciencia de Datos. Después de un proceso de evaluación de los algoritmos K-means, Clustering Jerárquico, y K-medoids, las métricas empleadas permitieron determinar que el algoritmo K-means presenta el mejor desempeño. Como resultado se obtuvieron 4 clusters estables que reflejan las relaciones o tendencias entre características de afiliados y prestadores de servicios de salud.

1.- EXPLORACION Y COMPRENSION DE LOS DATOS

1.1.- Análisis Univariado

El dataset contiene el listado de los prestadores de servicios de salud públicos y privados que operan en el departamento y la cantidad (conteo) de usuarios que atienden distribuidos en diferentes variables (grupos etarios, sexo, nivel socioeconómico y zona de residencia).

# Cargue del dataset y paquetería necesaria.

library(readxl)
require(ggplot2)
require(plotly)
require(CGPfunctions)
require(ggpubr)
library(factoextra)
library(broom)
library(pander)
library(corrplot)
library(gridExtra)
library(reshape) 
library(reshape2)
library(dplyr)
library(tidyr)
library(corrplot)
library(Hmisc)
library(tidyverse)
library(rgl)
library(igraph)
library(fpc)
library(fpc)
library(NbClust)
library(cluster)
library(amap)
library(clustertend)
library(clValid)


data_aseg <- read_excel("data_aseg_202305.xlsx")
attach(data_aseg)
summary(data_aseg)
##  nom_ips_prim         cod_ips            total_ips       Primera_infancia 
##  Length:240         Length:240         Min.   :    1.0   Min.   :   0.00  
##  Class :character   Class :character   1st Qu.:  727.5   1st Qu.:  48.75  
##  Mode  :character   Mode  :character   Median : 3734.5   Median : 271.00  
##                                        Mean   : 8477.4   Mean   : 580.92  
##                                        3rd Qu.:10355.0   3rd Qu.: 632.25  
##                                        Max.   :94785.0   Max.   :6484.00  
##     Infancia        Adolescencia       Juventud        Adultez       
##  Min.   :   0.00   Min.   :   0.0   Min.   :    0   Min.   :    0.0  
##  1st Qu.:  57.75   1st Qu.:  54.5   1st Qu.:  114   1st Qu.:  250.2  
##  Median : 302.00   Median : 351.5   Median :  610   Median : 1456.0  
##  Mean   : 698.72   Mean   : 787.1   Mean   : 1492   Mean   : 3535.8  
##  3rd Qu.: 772.75   3rd Qu.: 916.0   3rd Qu.: 1671   3rd Qu.: 4162.0  
##  Max.   :8492.00   Max.   :9576.0   Max.   :18743   Max.   :41843.0  
##   Adulto_mayor           F                 M              Nivel_1       
##  Min.   :    0.0   Min.   :    0.0   Min.   :    0.0   Min.   :    0.0  
##  1st Qu.:  127.8   1st Qu.:  357.2   1st Qu.:  333.2   1st Qu.:  227.5  
##  Median :  701.0   Median : 1903.0   Median : 1894.0   Median : 1431.0  
##  Mean   : 1382.9   Mean   : 4297.9   Mean   : 4179.5   Mean   : 2577.6  
##  3rd Qu.: 1619.5   3rd Qu.: 5212.8   3rd Qu.: 5049.5   3rd Qu.: 3068.8  
##  Max.   :10692.0   Max.   :47933.0   Max.   :46852.0   Max.   :20491.0  
##     Nivel_2            Nivel_3            Rural              Urbano     
##  Min.   :    0.00   Min.   :    0.0   Min.   :    0.00   Min.   :    0  
##  1st Qu.:   75.25   1st Qu.:   20.0   1st Qu.:   86.75   1st Qu.:  422  
##  Median :  649.00   Median :  144.0   Median :  900.00   Median : 2208  
##  Mean   : 1672.38   Mean   :  664.9   Mean   : 1632.28   Mean   : 6845  
##  3rd Qu.: 2142.50   3rd Qu.:  629.8   3rd Qu.: 2229.00   3rd Qu.: 6527  
##  Max.   :24509.00   Max.   :12125.0   Max.   :11019.00   Max.   :92322  
##      nat               cod_mun          mun            cod_ipss_prim      
##  Length:240         Min.   :  1.0   Length:240         Min.   :2.500e+11  
##  Class :character   1st Qu.:269.0   Class :character   1st Qu.:2.527e+11  
##  Mode  :character   Median :404.5   Mode  :character   Median :2.540e+11  
##                     Mean   :478.7                      Mean   :2.548e+11  
##                     3rd Qu.:754.0                      3rd Qu.:2.575e+11  
##                     Max.   :899.0                      Max.   :2.590e+11  
##       lat             long       
##  Min.   :3.919   Min.   :-74.81  
##  1st Qu.:4.559   1st Qu.:-74.46  
##  Median :4.834   Median :-74.24  
##  Mean   :4.795   Mean   :-74.22  
##  3rd Qu.:5.046   3rd Qu.:-74.03  
##  Max.   :5.620   Max.   :-73.24
head(data_aseg)
nom_ips_prim cod_ips total_ips Primera_infancia Infancia Adolescencia Juventud Adultez Adulto_mayor F M Nivel_1 Nivel_2 Nivel_3 Rural Urbano nat cod_mun mun cod_ipss_prim lat long
HOSPITAL MARIO GAITAN YANGUAS - EMPRESA SOCIAL DEL ESTADO REGION DE SALUD SOACHA e_1 49662 5413 4434 4828 8185 18610 8192 26481 23181 20491 7150 736 8435 41227 pub 754 SOACHA 2.5754e+11 4.581866 -74.24030
EMPRESA SOCIAL DEL ESTADO HOSPITAL SAN RAFAEL DE FUSAGASUGA e_2 35624 2413 2815 3201 5802 14319 7074 18511 17113 15722 6295 1002 5350 30274 pub 290 FUSAGASUGA 2.5290e+11 4.323534 -74.38859
E.S.E. HOSPITAL SAN RAFAEL DE PACHO - (255130002801) e_3 24116 1440 1851 2339 3363 9013 6110 12131 11985 12687 5134 1509 9068 15048 pub 513 PACHO 2.5513e+11 5.168368 -74.16337
EMPRESA SOCIAL DEL ESTADO HOSPITAL SAN JOSE DE GUADUAS e_4 21856 1062 1510 1987 2906 8790 5601 10686 11170 13209 3252 973 9215 12641 pub 320 GUADUAS 2.5320e+11 5.173555 -74.64015
EMPRESA SOCIAL DEL ESTADO E.S.E MUNICIPAL DE SOACHA JULIO CESAR PEñALOZA - SEDE SAN MARCOS (257540007501) e_5 17477 1782 1892 1832 2278 6245 3448 9964 7513 7918 2642 237 1821 15656 pub 754 SOACHA 2.5754e+11 4.581866 -74.24030
HOSPITAL MARIA AUXILIADORA EMPRESA SOCIAL DE ESTADO DEL MUNICIPIO DE MOSQUERA e_6 15258 1341 1096 1218 2990 6059 2554 7949 7309 4106 3401 726 773 14485 pub 473 MOSQUERA 2.5473e+11 4.672714 -74.23573
# DISTRIBUCION DE AFILIADOS POR PRESTADORES DE SERVICIOS DE SALUD EN EL DEPARTAMENTO

barplot(height=total_ips, names=cod_ips,col=c('red'),ylim=c(0,80000))

# VARIABLES DE GRUPO ETARIO

data_aseg_ge <-subset(data_aseg, select = c("Primera_infancia","Infancia","Adolescencia","Juventud","Adultez","Adulto_mayor"))

data_aseg_1 <- melt(data_aseg_ge) 
conteo_tot <- aggregate(value ~ variable, data_aseg_1, sum)
ggplot(data_aseg_1, aes(x = variable, y = value, fill=variable)) + geom_bar(stat="identity")+geom_text(data = conteo_tot, aes(x = variable, y = value, label = value, group = 1), 
            position = position_stack(vjust = 0.5), color = "white", size = 4) +
  xlab("Variable") +
  ylab("Value") +
  ggtitle("Total por cada grupo etario")

#Boxplot Variables de Grupo Etario

ggplot(data_aseg_1, aes(x = variable, y = value, color=variable)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor") +
  theme_minimal()

# VARIABLES DE GENERO

data_aseg_s <-subset(data_aseg, select = c("F","M"))

data_aseg_2 <- melt(data_aseg_s)
conteo_tot <- aggregate(value ~ variable, data_aseg_2, sum)
ggplot(data_aseg_2, aes(x = variable, y = value, fill=variable)) + geom_bar(stat="identity")+geom_text(data = conteo_tot, aes(x = variable, y = value, label = value, group = 1), 
            position = position_stack(vjust = 0.5), color = "white", size = 4) +
  xlab("Variable") +
  ylab("Value") +
  ggtitle("Total por cada Género")

#Boxplot Variables de Género

ggplot(data_aseg_2, aes(x = variable, y = value, color=variable)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor") +
  theme_minimal()

# VARIABLES DE NIVEL SOCIOECONOMICO

data_aseg_n <-subset(data_aseg, select = c("Nivel_1","Nivel_2","Nivel_3"))

data_aseg_3 <- melt(data_aseg_n) 
conteo_tot <- aggregate(value ~ variable, data_aseg_3, sum)
ggplot(data_aseg_3, aes(x = variable, y = value, fill=variable)) + geom_bar(stat="identity")+geom_text(data = conteo_tot, aes(x = variable, y = value, label = value, group = 1), 
            position = position_stack(vjust = 0.5), color = "white", size = 4) +
  xlab("Variable") +
  ylab("Value") +
  ggtitle("Total por cada Nivel Socioeconómico")

#Boxplot Variables de Grupo Etario

ggplot(data_aseg_3, aes(x = variable, y = value, color=variable)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor") +
  theme_minimal()

# VARIABLES DE ZONA

data_aseg_z <-subset(data_aseg, select = c("Rural","Urbano"))

data_aseg_4 <- melt(data_aseg_z) 
conteo_tot <- aggregate(value ~ variable, data_aseg_4, sum)
ggplot(data_aseg_4, aes(x = variable, y = value, fill=variable)) + geom_bar(stat="identity")+geom_text(data = conteo_tot, aes(x = variable, y = value, label = value, group = 1), 
            position = position_stack(vjust = 0.5), color = "white", size = 4) +
  xlab("Variable") +
  ylab("Value") +
  ggtitle("Total por Zona de residencia")

#Boxplot Variables de Zona

ggplot(data_aseg_4, aes(x = variable, y = value, color=variable)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor") +
  theme_minimal()

1.2.- Medida de Correlaciones

# Columna 2 como indice
data_aseg_c <- data_aseg %>% column_to_rownames(., var = 'cod_ips')
# remocion de la columnas/variables informativas nom_ips_prim, total_ips y nat
data_aseg_c$nom_ips_prim <- NULL
data_aseg_c$total_ips <- NULL 
data_aseg_c$nat <- NULL
data_aseg_c$cod_mun <- NULL
data_aseg_c$mun <- NULL
data_aseg_c$cod_ipss_prim <- NULL
data_aseg_c$lat <- NULL
data_aseg_c$long <- NULL


# Normalización de los datos
data_aseg_norm <- scale(data_aseg_c)

# Prueba de Boxplot luego de la estandarizacion
#data_aseg_nz <-subset(data_aseg_norm, select = c("Rural","Urbano"))
#data_aseg_12 <- melt(data_aseg_nz) 
#data_aseg_12
#conteo_tot12 <- aggregate(value ~ Var2, data_aseg_12, sum)
#ggplot(data_aseg_12, aes(x = Var2, y = value, color=Var2)) +
#  geom_boxplot() +
#  labs(x = "Variable", y = "Valor") +
#  theme_minimal()


# MEDICION DE LAS CORRELACION ENTRE LAS VARIABLES
# Matriz de Correlaciones y Estimacion del P-Valor
rcorr(as.matrix(data_aseg_norm))
##                  Primera_infancia Infancia Adolescencia Juventud Adultez
## Primera_infancia             1.00     0.99         0.98     0.97    0.97
## Infancia                     0.99     1.00         1.00     0.98    0.99
## Adolescencia                 0.98     1.00         1.00     0.97    0.99
## Juventud                     0.97     0.98         0.97     1.00    0.99
## Adultez                      0.97     0.99         0.99     0.99    1.00
## Adulto_mayor                 0.82     0.81         0.82     0.82    0.85
## F                            0.98     0.99         0.99     0.98    1.00
## M                            0.98     0.99         0.99     0.99    1.00
## Nivel_1                      0.84     0.81         0.82     0.77    0.79
## Nivel_2                      0.92     0.92         0.92     0.96    0.94
## Nivel_3                      0.76     0.79         0.79     0.85    0.84
## Rural                        0.41     0.37         0.40     0.39    0.38
## Urbano                       0.97     0.98         0.98     0.98    0.99
##                  Adulto_mayor    F    M Nivel_1 Nivel_2 Nivel_3 Rural Urbano
## Primera_infancia         0.82 0.98 0.98    0.84    0.92    0.76  0.41   0.97
## Infancia                 0.81 0.99 0.99    0.81    0.92    0.79  0.37   0.98
## Adolescencia             0.82 0.99 0.99    0.82    0.92    0.79  0.40   0.98
## Juventud                 0.82 0.98 0.99    0.77    0.96    0.85  0.39   0.98
## Adultez                  0.85 1.00 1.00    0.79    0.94    0.84  0.38   0.99
## Adulto_mayor             1.00 0.88 0.87    0.85    0.79    0.70  0.53   0.84
## F                        0.88 1.00 1.00    0.82    0.93    0.83  0.40   0.99
## M                        0.87 1.00 1.00    0.83    0.95    0.83  0.43   0.98
## Nivel_1                  0.85 0.82 0.83    1.00    0.70    0.48  0.69   0.76
## Nivel_2                  0.79 0.93 0.95    0.70    1.00    0.91  0.44   0.92
## Nivel_3                  0.70 0.83 0.83    0.48    0.91    1.00  0.28   0.83
## Rural                    0.53 0.40 0.43    0.69    0.44    0.28  1.00   0.27
## Urbano                   0.84 0.99 0.98    0.76    0.92    0.83  0.27   1.00
## 
## n= 240 
## 
## 
## P
##                  Primera_infancia Infancia Adolescencia Juventud Adultez
## Primera_infancia                   0        0            0        0     
## Infancia          0                         0            0        0     
## Adolescencia      0                0                     0        0     
## Juventud          0                0        0                     0     
## Adultez           0                0        0            0              
## Adulto_mayor      0                0        0            0        0     
## F                 0                0        0            0        0     
## M                 0                0        0            0        0     
## Nivel_1           0                0        0            0        0     
## Nivel_2           0                0        0            0        0     
## Nivel_3           0                0        0            0        0     
## Rural             0                0        0            0        0     
## Urbano            0                0        0            0        0     
##                  Adulto_mayor F  M  Nivel_1 Nivel_2 Nivel_3 Rural Urbano
## Primera_infancia  0            0  0  0       0       0       0     0    
## Infancia          0            0  0  0       0       0       0     0    
## Adolescencia      0            0  0  0       0       0       0     0    
## Juventud          0            0  0  0       0       0       0     0    
## Adultez           0            0  0  0       0       0       0     0    
## Adulto_mayor                   0  0  0       0       0       0     0    
## F                 0               0  0       0       0       0     0    
## M                 0            0     0       0       0       0     0    
## Nivel_1           0            0  0          0       0       0     0    
## Nivel_2           0            0  0  0               0       0     0    
## Nivel_3           0            0  0  0       0               0     0    
## Rural             0            0  0  0       0       0             0    
## Urbano            0            0  0  0       0       0       0
correlacion=cor(data_aseg_norm)
# Representación gráfica de la matriz de correlaciones
corrplot(correlacion, method="color")

2.- PREPARACION DE LOS DATOS

2.1.- Análisis de Componentes Principales (PCA)

#ANALISIS DE COMPONENTES Y ESTANDARIZACION DEL DATASET (media=0 y DS=1)
data_aseg_pca <- prcomp(data_aseg_c, scale = TRUE)


# VERIFICACION DE LOS COMPONENTES PRINCIPALES 
#data_aseg_pca$rotation
#head(data_aseg_pca$x)

# Representación gráfica 

#Calidad de la representación de los individuos
fviz_pca_ind(data_aseg_pca, geom = c("point", "text"),  axes=c(1, 2), pointsize=1.5, col.ind="cos2", repel=TRUE)

# Representacion de individuos, dimensiones y variables
fviz_pca_biplot(data_aseg_pca, repel=TRUE, ggtheme=theme_grey())+labs(title ="Representacion PCA-Biplot")

# Zoom sobre el sector mas denso de variables
fviz_pca_biplot(data_aseg_pca, repel=TRUE, ggtheme=theme_grey())+labs(title ="Representacion PCA-Biplot")+xlim(c(-7,0))+ylim(c(0,-2))

# Representacion 3D
plot3d(data_aseg_pca$x[,1:3],type="s", col="red", size=1)
rglwidget()
# Representacion de los individuos, variables 
fviz_pca_biplot(data_aseg_pca, repel=TRUE, ggtheme=theme_grey(),habillage=data_aseg$nat,
     addEllipses=TRUE, ellipse.level=0.995, palette = "Dark2")+labs(title ="Representacion PCA-Biplot")

# Zoom sobre el sector mas denso de individuos
fviz_pca_biplot(data_aseg_pca, repel=TRUE, ggtheme=theme_grey(),habillage=data_aseg$nat,
    palette = "Dark2")+labs(title ="Representacion PCA-Biplot")+xlim(c(0,3))+ylim(c(-2, 2))

# Varianza por cada componente principal
data_aseg_pca$sdev^2
##  [1] 1.109543e+01 1.093001e+00 4.581846e-01 2.370770e-01 3.979207e-02
##  [6] 3.725522e-02 1.814010e-02 1.606949e-02 2.966315e-03 1.077809e-03
## [11] 1.003314e-03 4.646964e-32 1.473382e-32
# Proporcion de Varianza explicada
p_varianza <- data_aseg_pca$sdev^2 / sum(data_aseg_pca$sdev^2)
p_varianza
##  [1] 8.534949e-01 8.407700e-02 3.524497e-02 1.823669e-02 3.060928e-03
##  [6] 2.865786e-03 1.395392e-03 1.236115e-03 2.281781e-04 8.290840e-05
## [11] 7.717800e-05 3.574588e-33 1.133371e-33
# Grafico de Dimensiones vs Varianza Explicada
fviz_eig(data_aseg_pca, addlabels = TRUE, ylim = c(0, 90))

#Calidad` de representación de las variables
fviz_cos2(data_aseg_pca, choice = "var", axes = 1:2)

fviz_pca_var(data_aseg_pca, col.var = "cos2",
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE
             )

3.- DESARROLLO DEL MODELO

3.1.- Verificación preliminar de existencia de agrupaciones en los datos

# Validacion de tendencia sobre el dataset con el estadistico de Hopkins

# PREVIO A PCA
library(clustertend)
set.seed(321)
hopkins(data_aseg_norm, n = nrow(data_aseg_norm) - 1)
## $H
## [1] 0.06757716
# Validacion de estructura en los datos empleando la matriz de distancias

matriz_dist_aseg <- dist(data_aseg_norm, method = "euclidean")
estructura <- fviz_dist(dist.obj = matriz_dist_aseg, show_labels = FALSE) +
      labs(title = "Datos") + theme(legend.position = "bottom")
estructura

# POSTERIOR A PCA - Dos dimensiones principales
set.seed(321)
hopkins(data_aseg_pca$x[,1:2], n = nrow(data_aseg_pca$x[,1:2]) - 1)
## $H
## [1] 0.1051677
# Validacion de estructura en los datos empleando la matriz de distancias

matriz_dist_aseg_pca <- dist(data_aseg_pca$x[,1:2], method = "euclidean")
estructura_pca <- fviz_dist(dist.obj = matriz_dist_aseg, show_labels = FALSE) +
      labs(title = "Datos") + theme(legend.position = "bottom")
estructura_pca

3.2.- Modelo basado en K-Means

3.2.1.- Determinación del número óptimo de clusters y evaluación del modelo K-means

# Prueba preliminar de determinación del número optimo de clusters mediante WSS
#set.seed(123)
#suma_cuadrados <- c()
#for (i in 1:30) {
#  k <- kmeans(data_aseg_pca$x[,1:2], centers = i)
#  suma_cuadrados[i] <- k$tot.withinss
#}
#plot(1:30, suma_cuadrados, type = "b", xlab = "Número de clusters", ylab = "Suma de las distancias al cuadrado")


#Determinacion del numero K optimo de clusters con FactoExtra y nbclust(). 

## Metodo del Codo con diferentes distancias

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "euclidean")) + labs(subtitle = "Metodo del Codo_ Dist-Euclidean") 

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "manhattan")) + labs(subtitle = "Metodo del Codo_Dist-Manhattan") 

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "pearson")) + labs(subtitle = "Metodo del Codo_Dist-Pearson") 

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "spearman")) + labs(subtitle = "Metodo del Codo_Dist-Spearman") 

## Metodo de la Silueta con diferentes distancias
fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "silhouette", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "euclidean")) + labs(subtitle = "Metodo de la Silueta-Euclidean")

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "silhouette", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "manhattan")) + labs(subtitle = "Metodo de la Silueta-Manhattan")

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "silhouette", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "pearson")) + labs(subtitle = "Metodo de la Silueta-Pearson")

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method = "silhouette", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "spearman")) + labs(subtitle = "Metodo de la Silueta-Spearman")

## Metodo Gap Statistic con diferentes distancias
fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method ="gap_stat", nstart=30, k.max=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "euclidean")) + labs(subtitle = "Metodo Gap Stat-Euclidean")

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method ="gap_stat", nstart=30, k.max=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "manhattan")) + labs(subtitle = "Metodo Gap Stat-Manhattan")

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method ="gap_stat", nstart=30, k.max=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "Pearson")) + labs(subtitle = "Metodo Gap Stat-Pearson")

fviz_nbclust(data_aseg_pca$x[,1:2], kmeans, method ="gap_stat", nstart=30, k.max=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "Spearman")) + labs(subtitle = "Metodo Gap Stat-Spearman")

#EVALUACION modelo K-means

# Ancho de la Silueta - modelo K-means

km_clusters <- eclust(x = data_aseg_pca$x[,1:2], FUNcluster = "kmeans", k = 4, seed = 123,
                      hc_metric = "euclidean", nstart=30, graph = FALSE)

fviz_silhouette(sil.obj = km_clusters, print.summary = TRUE, palette = "jco",
                ggtheme = theme_classic()) 
##   cluster size ave.sil.width
## 1       1   14          0.42
## 2       2   53          0.26
## 3       3    9          0.28
## 4       4  164          0.69

head(km_clusters$silinfo$widths)
cluster neighbor sil_width
ip_11 1 2 0.5611304
e_2 1 2 0.5593445
ip_10 1 2 0.5494646
ip_18 1 2 0.5255848
ip_17 1 2 0.5191313
ip_14 1 2 0.4477604
# Indice Dunn
km_indiceD <- cluster.stats(d = dist(data_aseg_pca$x[,1:2], method = "euclidean"), clustering=km_clusters$cluster)
km_indiceD$average.within
## [1] 1.418568
km_indiceD$average.between
## [1] 5.523847
km_indiceD$dunn
## [1] 0.01376138
# Indice de Davies Bouldin - modelo K-means
library(clusterSim)
print(index.DB(data_aseg_pca$x[,1:2], km_clusters$cluster, centrotypes="centroids"))
## $DB
## [1] 0.8510354
## 
## $r
## [1] 0.8536798 0.8483910 0.8536798 0.8483910
## 
## $R
##           [,1]      [,2]      [,3]      [,4]
## [1,]       Inf 0.8448702 0.8536798 0.4008493
## [2,] 0.8448702       Inf 0.5044677 0.8483910
## [3,] 0.8536798 0.5044677       Inf 0.3508599
## [4,] 0.4008493 0.8483910 0.3508599       Inf
## 
## $d
##          1        2         3         4
## 1 0.000000  4.50365  7.625935  7.293469
## 2 4.503650  0.00000 11.804159  2.791510
## 3 7.625935 11.80416  0.000000 14.459905
## 4 7.293469  2.79151 14.459905  0.000000
## 
## $S
## [1] 2.1801446 1.6248547 4.3299627 0.7434373
## 
## $centers
##            [,1]       [,2]
## [1,]  -5.606005  1.1771764
## [2,]  -1.166614  0.4191048
## [3,] -12.864465 -1.1615525
## [4,]   1.561554 -0.1721893
# Conectividad Modelo K-means
d = dist(data_aseg_pca$x[,1:2], method = "euclidean")
connectivity(d,km_clusters$cluster,neighbSize = 10)
## [1] 26.67817
# Estabilidad - modelo K-means 

estabilidad_k<-clValid(data_aseg_norm, 4, clMethods = "kmeans",
        validation = "stability", maxitems = 600,
        metric = "euclidean")
summary(estabilidad_k)
## 
## Clustering Methods:
##  kmeans 
## 
## Cluster sizes:
##  4 
## 
## Validation Measures:
##                  4
##                   
## kmeans APN  0.0048
##        AD   1.7953
##        ADM  0.0428
##        FOM  0.4653
## 
## Optimal Scores:
## 
##     Score  Method Clusters
## APN 0.0048 kmeans 4       
## AD  1.7953 kmeans 4       
## ADM 0.0428 kmeans 4       
## FOM 0.4653 kmeans 4
# Evaluación del modelo con los criterios jitter-boot
test.clusterboot<-clusterboot(data_aseg_pca$x[,1:2],B=70,bootmethod=c("jitter","boot"),clustermethod=kmeansCBI, krange=4, seed=123)
## jitter 1 
## jitter 2 
## jitter 3 
## jitter 4 
## jitter 5 
## jitter 6 
## jitter 7 
## jitter 8 
## jitter 9 
## jitter 10 
## jitter 11 
## jitter 12 
## jitter 13 
## jitter 14 
## jitter 15 
## jitter 16 
## jitter 17 
## jitter 18 
## jitter 19 
## jitter 20 
## jitter 21 
## jitter 22 
## jitter 23 
## jitter 24 
## jitter 25 
## jitter 26 
## jitter 27 
## jitter 28 
## jitter 29 
## jitter 30 
## jitter 31 
## jitter 32 
## jitter 33 
## jitter 34 
## jitter 35 
## jitter 36 
## jitter 37 
## jitter 38 
## jitter 39 
## jitter 40 
## jitter 41 
## jitter 42 
## jitter 43 
## jitter 44 
## jitter 45 
## jitter 46 
## jitter 47 
## jitter 48 
## jitter 49 
## jitter 50 
## jitter 51 
## jitter 52 
## jitter 53 
## jitter 54 
## jitter 55 
## jitter 56 
## jitter 57 
## jitter 58 
## jitter 59 
## jitter 60 
## jitter 61 
## jitter 62 
## jitter 63 
## jitter 64 
## jitter 65 
## jitter 66 
## jitter 67 
## jitter 68 
## jitter 69 
## jitter 70 
## boot 1 
## boot 2 
## boot 3 
## boot 4 
## boot 5 
## boot 6 
## boot 7 
## boot 8 
## boot 9 
## boot 10 
## boot 11 
## boot 12 
## boot 13 
## boot 14 
## boot 15 
## boot 16 
## boot 17 
## boot 18 
## boot 19 
## boot 20 
## boot 21 
## boot 22 
## boot 23 
## boot 24 
## boot 25 
## boot 26 
## boot 27 
## boot 28 
## boot 29 
## boot 30 
## boot 31 
## boot 32 
## boot 33 
## boot 34 
## boot 35 
## boot 36 
## boot 37 
## boot 38 
## boot 39 
## boot 40 
## boot 41 
## boot 42 
## boot 43 
## boot 44 
## boot 45 
## boot 46 
## boot 47 
## boot 48 
## boot 49 
## boot 50 
## boot 51 
## boot 52 
## boot 53 
## boot 54 
## boot 55 
## boot 56 
## boot 57 
## boot 58 
## boot 59 
## boot 60 
## boot 61 
## boot 62 
## boot 63 
## boot 64 
## boot 65 
## boot 66 
## boot 67 
## boot 68 
## boot 69 
## boot 70
test.clusterboot
## * Cluster stability assessment *
## Cluster method:  kmeans 
## Full clustering results are given as parameter result
## of the clusterboot object, which also provides further statistics
## of the resampling results.
## Number of resampling runs:  70 
## 
## Number of clusters found in data:  4 
## 
##  Clusterwise Jaccard jittering mean:
## [1] 1.0000000 0.9966028 0.9883117 0.9887218
## dissolved:
## [1] 0 0 0 0
## recovered:
## [1] 70 70 67 67
##  Clusterwise Jaccard bootstrap (omitting multiple points) mean:
## [1] 0.8349219 0.9014838 0.7454673 0.7032363
## dissolved:
## [1]  7  0 14 18
## recovered:
## [1] 48 60 37 29

3.2.2.- Ejecución del modelo K-means

# Clustering
# Clusters_pca <- Kmeans(data_aseg_pca$x[,1:2], 4, method="euclidean")
#clusters_pca <- kmeans(data_aseg_pca$x[,1:2], 4)
#clusters_pca

fviz_cluster(km_clusters,data_aseg_pca$x[,1:2]) #op2: clusters_pca

plot3d(data_aseg_pca$x[,1:3],type="s", col=km_clusters$cluster, size=1) #op2: clusters_pca$cluster
rglwidget()
# Asignación de clusters al dataset
datos_cluster_pca <- data.frame(data_aseg, cluster = as.factor(km_clusters$cluster)) #clusters_pcs$cluster
write.csv(datos_cluster_pca, "mi_KmC_4K_20230620-1700.csv")


# ANALISIS DE LOS CLUSTERS

# Tamaño de los clusters en relacion a los afiliados que atienden los prestadores
data_aseg_cl_afil <-subset(datos_cluster_pca, select = c("total_ips","cluster"))

data_aseg_cl_afil_1<- data_aseg_cl_afil %>%
  group_by(cluster) %>%
   summarise(total_afil=sum(total_ips)) %>%
   mutate(porcentaje_ta = total_afil/ sum(total_afil) * 100)
  
ggplot(data_aseg_cl_afil_1, aes(x = cluster, y = total_afil, fill=cluster)) + geom_bar(stat="identity") +  geom_text(aes(label = paste0(round(porcentaje_ta), "%\n", total_afil)), vjust = 3) 

# Distribucion Prestadores Publicos y Privados en los clusters
data_aseg_nat <-subset(datos_cluster_pca, select = c("nat","cluster"))

data_aseg_nat_porcent <- data_aseg_nat %>%
  group_by(cluster,nat) %>%
  summarise(count=n()) %>%
  mutate(porcentaje = count / sum(count) * 100)

ggplot(data_aseg_nat_porcent, aes(x = "", y = porcentaje, fill=nat)) + geom_bar(stat="identity",width = 1) +
  coord_polar("y", start = 0) +
  facet_wrap(~cluster) +
  theme_void() +
  labs(x = NULL, y = NULL, fill = "nat") +
  geom_text(aes(label = paste0(round(porcentaje), "%\n", count)), position = position_stack(vjust = 0.5))

# Grupos Etarios
data_aseg_gk <-subset(datos_cluster_pca, select = c("Primera_infancia","Infancia","Adolescencia","Juventud","Adultez","Adulto_mayor","cluster"))
data_aseg_gk_porcent <- data_aseg_gk %>%
  group_by(cluster) %>%
  summarise(across(c(Primera_infancia,Infancia,Adolescencia,Juventud,Adultez,Adulto_mayor), sum)) %>%
  mutate(across(c(Primera_infancia,Infancia,Adolescencia,Juventud,Adultez,Adulto_mayor), function(x) x / sum(x) * 100))

data_aseg_gk1 <- melt(data_aseg_gk_porcent, id.vars = "cluster")

ggplot(data_aseg_gk1, aes(x = variable, y = value, fill=cluster)) + geom_bar(stat="identity",position = "stack")       + geom_text(aes(label = paste0(round(value, 1), "%")), position = position_stack(vjust = 0.5), color = "white")

#Boxplot de Clusters versus las variables de Grupos Etarios
data_aseg_gk2 <- melt(data_aseg_gk) 
ggplot(data_aseg_gk2, aes(x = variable, y = value, fill = cluster)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor", fill = "Cluster") +
  theme_minimal()

# Sexo
data_aseg_sk <-subset(datos_cluster_pca, select = c("F","M","cluster"))

data_aseg_sk_porcent <- data_aseg_sk %>%
  group_by(cluster) %>%
  summarise(across(c(F,M,), sum)) %>%
  mutate(across(c(F,M), function(x) x / sum(x) * 100))

data_aseg_sk1 <- melt(data_aseg_sk_porcent, id.vars = "cluster")

ggplot(data_aseg_sk1, aes(x = variable, y = value, fill=cluster)) + geom_bar(stat="identity",position = "stack") + geom_text(aes(label = paste0(round(value, 1), "%")), position = position_stack(vjust = 0.5), color = "white")

#Boxplot de Clusters versus las variables de Género
data_aseg_sk2 <- melt(data_aseg_sk) 
ggplot(data_aseg_sk2, aes(x = variable, y = value, fill = cluster)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor", fill = "Cluster") +
  theme_minimal()

# Nivel Socioeconomico
data_aseg_nk <-subset(datos_cluster_pca, select = c("Nivel_1","Nivel_2","Nivel_3","cluster"))

data_aseg_nk_porcent <- data_aseg_nk %>%
  group_by(cluster) %>%
  summarise(across(c(Nivel_1,Nivel_2,Nivel_3), sum)) %>%
  mutate(across(c(Nivel_1,Nivel_2,Nivel_3), function(x) x / sum(x) * 100))

data_aseg_nk1 <- melt(data_aseg_nk_porcent, id.vars = "cluster")

ggplot(data_aseg_nk1, aes(x = variable, y = value, fill=cluster)) + geom_bar(stat="identity",position = "stack") + geom_text(aes(label = paste0(round(value, 1), "%")), position = position_stack(vjust = 0.5), color = "white")

#Boxplot de Clusters versus las variables de Nivel Socioeconomico
data_aseg_nk2 <- melt(data_aseg_nk) 
ggplot(data_aseg_nk2, aes(x = variable, y = value, fill = cluster)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor", fill = "Cluster") +
  theme_minimal()

# Zona
data_aseg_zk <-subset(datos_cluster_pca, select = c("Rural","Urbano","cluster"))

data_aseg_zk_porcent <- data_aseg_zk %>%
  group_by(cluster) %>%
  summarise(across(c(Rural,Urbano), sum)) %>%
  mutate(across(c(Rural,Urbano), function(x) x / sum(x) * 100))

data_aseg_zk1 <- melt(data_aseg_zk_porcent, id.vars="cluster")

ggplot(data_aseg_zk1, aes(x = variable, y = value, fill=cluster)) + geom_bar(stat="identity",position = "stack") + geom_text(aes(label = paste0(round(value, 1), "%")), position = position_stack(vjust = 0.5), color = "white")

#Boxplot de Clusters versus las variables de Nivel Socioeconomico
data_aseg_zk2 <- melt(data_aseg_zk) 
ggplot(data_aseg_zk2, aes(x = variable, y = value, fill = cluster)) +
  geom_boxplot() +
  labs(x = "Variable", y = "Valor", fill = "Cluster") +
  theme_minimal()

3.3.- Modelo basado en Clustering Jerárquico

3.3.1.- Determinación del número óptimo de clusters y evaluación del modelo Clustering Jerárquico

# Determinación de la mejor estructura para el Dendograma a partir de la correlación entre distancias originales y distancias del Dendograma.

matriz_dist <- dist(x = data_aseg_pca$x[,1:2], method = "euclidean") # method: euclidean, manhattan
hc_dl_ward <- hclust(d = matriz_dist, method = "single") # method: single,complete, average, ward
cor(x = matriz_dist, cophenetic(hc_dl_ward))
## [1] 0.9462142
# DETERMINACION DEL NUMERO OPTIMO DE CLUSTERS PARA EL MODELO BASADO EN CLUSTERING JERARQUICO

# Método Gráfico. Observando el punto de corte en el dendograma

hc_euclidea_single <- hclust(d = dist(x=data_aseg_pca$x[,1:2], method="euclidean"),method="single")
fviz_dend(x = hc_euclidea_single, k = 1, cex = 0.6, show_labels=FALSE) +
  geom_hline(yintercept = 3.2, linetype = "dashed") + 
  labs(title = "Clustering Jerárquico", subtitle = "Distancia Manhattan, Linkage Ward")

# Empleando la librería NbClust() y variando los hiperparámetros distancia y método

# Distancia Euclídea y varios linkage

testH.nbclust_single <- NbClust(data_aseg_pca$x[,1:2], distance="euclidean", min.nc=2, max.nc=30, method="single", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 9 proposed 2 as the best number of clusters 
## * 2 proposed 3 as the best number of clusters 
## * 6 proposed 5 as the best number of clusters 
## * 1 proposed 10 as the best number of clusters 
## * 3 proposed 11 as the best number of clusters 
## * 1 proposed 21 as the best number of clusters 
## * 1 proposed 24 as the best number of clusters 
## * 1 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_single
## $All.index
##         KL      CH Hartigan      CCC    Scott  Marriot     TrCovW    TraceW
## 2   1.2110 48.6294  30.7199 -16.6876  68.7971  2080310 2630470.33 2418.8114
## 3   1.4687 42.6332  33.4613 -19.1743 112.2717  3905176 1921788.95 2142.2946
## 4   0.7132 43.4048 133.6979 -28.3906 156.4042  5776407 1529582.40 1877.2509
## 5   2.1139 84.0625  16.8337 -23.4437 336.8583  4255349  760872.97 1198.3600
## 6   0.6854 75.1131   2.1426 -24.9786 357.7335  5617238  659894.86 1118.2560
## 7   1.6885 63.2530  13.5704 -27.1668 360.3148  7563891  645744.90 1108.1096
## 8   0.5991 59.0586   0.2943 -28.2607 376.6688  9228598  562577.57 1047.1232
## 9   0.4347 51.5555 112.5972 -30.0696 378.2332 11604057  562615.81 1045.7967
## 10  3.1212 80.3263  53.7959 -25.1674 520.1388  7931185  266992.39  703.0878
## 11  1.8199 94.1712   5.1567 -23.2732 597.4925  6952601  149776.21  569.8117
## 12  0.7867 87.6224   0.7704 -24.3566 621.6371  7482268  149273.06  557.2630
## 13  4.7095 80.3024  13.6896 -25.6432 622.9010  8735149  148372.50  555.3863
## 14  0.2200 79.2977   0.2706 -25.9475 661.9650  8608969  128692.26  523.7979
## 15  1.2614 73.4148   3.1447 -27.1054 662.5682  9857935  128269.88  523.1715
## 16  0.8229 69.3781   0.5226 -27.9923 679.6850 10444066  127946.53  515.9602
## 17  1.0027 64.9352   0.4953 -29.0068 680.9663 11727593  127642.61  514.7591
## 18  1.0012 61.0056   0.4551 -29.9714 681.8733 13098296  126863.48  513.6183
## 19  0.9832 57.4996   0.2265 -30.8929 683.0040 14525494  126600.41  512.5675
## 20  0.5729 54.2942  32.6172 -31.7897 684.1661 16016986  126615.91  512.0428
## 21  1.9943 60.5809   0.1454 -30.4675 783.4755 11674893   89301.71  445.9293
## 22  1.0404 57.4777   0.4962 -31.3031 783.7103 12800735   89200.06  445.6334
## 23  0.9644 54.7602   0.1361 -32.0826 786.0727 13853842   88770.26  444.6214
## 24 16.3929 52.1765   8.4378 -32.8603 786.8087 15038524   88668.45  444.3427
## 25  0.0452 52.0652  21.6703 -33.0163 798.6747 15530686   82166.62  427.6376
## 26  4.8994 55.6273  10.9142 -32.2383 840.1967 14129303   71795.30  388.4817
## 27  6.9942 56.3708   8.2709 -32.1723 870.6276 13422546   68603.63  369.6303
## 28  0.0492 56.4310   0.1085 -32.2713 894.4690 13070165   66156.82  355.8139
## 29  2.2175 54.1905  10.6735 -32.9448 895.2023 13977648   66161.14  355.6319
## 30  0.4743 55.0744   0.1048 -32.8321 909.2952 14105176   58900.49  338.5084
##    Friedman  Rubin Cindex     DB Silhouette     Duda Pseudot2   Beale Ratkowsky
## 2    0.2986 1.2043 0.1662 0.1428     0.8432   0.8857  30.5908  0.1285    0.2432
## 3    0.5365 1.3598 0.1578 0.2605     0.7211   0.9478  12.9338  0.0548    0.2567
## 4    0.7943 1.5518 0.1811 0.2277     0.7307   0.6381 132.7118  0.5647    0.2515
## 5    2.3146 2.4309 0.2206 0.3668     0.7585   0.9685   7.4493  0.0324    0.2825
## 6    2.5948 2.6050 0.2408 0.3736     0.6912   0.2093  11.3367  2.8342    0.2623
## 7    2.6280 2.6288 0.2408 0.2823     0.6962   0.9830   3.9514  0.0173    0.2438
## 8    2.8360 2.7819 0.2597 0.2800     0.6640 150.1282   0.0000  0.0000    0.2337
## 9    2.8503 2.7855 0.2597 0.2531     0.6678   0.6715 111.0717  0.4872    0.2208
## 10   5.7032 4.1432 0.2187 0.4012     0.6537   0.8032  53.4036  0.2439    0.2216
## 11   6.7234 5.1123 0.2580 0.4183     0.6655   0.4562   8.3432  1.0429    0.2356
## 12   7.0386 5.2274 0.2579 0.3865     0.6620  16.1084  -0.9379 -0.4690    0.2303
## 13   7.0765 5.2451 0.2579 0.3594     0.6631   0.9855   3.1535  0.0146    0.2214
## 14   7.4464 5.5614 0.3011 0.3528     0.6359  85.4573   0.0000  0.0000    0.2212
## 15   7.4535 5.5680 0.3011 0.3240     0.6416   0.3039  11.4530  1.9088    0.2138
## 16   7.6980 5.6459 0.3010 0.2973     0.6458  11.1639  -1.8209 -0.6070    0.2092
## 17   7.7331 5.6590 0.3010 0.2778     0.6337  14.8400  -0.9326 -0.4663    0.2031
## 18   7.7464 5.6716 0.3011 0.2463     0.6325  23.2289  -0.9570 -0.4785    0.1975
## 19   7.7775 5.6832 0.3011 0.2379     0.6346  44.1054  -0.9773 -0.4887    0.1922
## 20   7.7988 5.6890 0.3011 0.2213     0.6410   0.8706  31.7939  0.1479    0.1875
## 21   9.0706 6.5325 0.2846 0.2411     0.5519 254.8684   0.0000  0.0000    0.1933
## 22   9.0779 6.5368 0.2846 0.2312     0.5575  10.5721  -2.7162 -0.6791    0.1889
## 23   9.1131 6.5517 0.2846 0.2325     0.5351  78.7103   0.0000  0.0000    0.1849
## 24   9.1243 6.5558 0.2846 0.2147     0.5380   1.0047  -0.9689 -0.0046    0.1811
## 25   9.5103 6.8119 0.2803 0.2165     0.5181   0.9083  21.0043  0.1005    0.1777
## 26  10.9410 7.4985 0.2881 0.2127     0.5348   0.9539   9.9613  0.0481    0.1754
## 27  12.0537 7.8809 0.2830 0.2117     0.5434   0.9652   7.3982  0.0359    0.1730
## 28  13.0379 8.1870 0.2787 0.2133     0.5412 566.9647   0.0000  0.0000    0.1704
## 29  13.0605 8.1912 0.2787 0.2044     0.5438   0.9545   9.7250  0.0474    0.1675
## 30  13.5945 8.6055 0.3068 0.2031     0.5515 109.1975   0.0000  0.0000    0.1651
##         Ball Ptbiserial       Frey McClain   Dunn Hubert SDindex Dindex   SDbw
## 2  1209.4057     0.4644    45.1353  0.0011 0.3393  8e-04  2.8432 2.2319 0.4329
## 3   714.0982     0.5188    -1.9584  0.0047 0.1916  8e-04  1.9115 2.1032 0.2894
## 4   469.3127     0.6120     5.2770  0.0060 0.1987  8e-04  1.5333 2.0092 0.2051
## 5   239.6720     0.7752     7.6795  0.0137 0.2717  8e-04  1.4509 1.7103 0.1559
## 6   186.3760     0.7847   177.7625  0.0155 0.2116  7e-04  1.5360 1.6607 0.1196
## 7   158.3014     0.7837     9.4072  0.0156 0.1946  7e-04  1.5674 1.6433 0.0684
## 8   130.8904     0.7863  -181.0508  0.0175 0.1952  7e-04  2.0072 1.6003 0.0573
## 9   116.1996     0.7861     7.4501  0.0175 0.1882  7e-04  2.4183 1.5935 0.0671
## 10   70.3088     0.7842     2.9448  0.0357 0.1853  7e-04  2.4584 1.3402 0.0488
## 11   51.8011     0.8001    26.0111  0.0405 0.2338  7e-04  2.3945 1.2436 0.0625
## 12   46.4386     0.7991 -1067.8377  0.0407 0.1961  7e-04  2.3579 1.2246 0.0336
## 13   42.7220     0.7990     4.1130  0.0407 0.1941  7e-04  2.3822 1.2186 0.0274
## 14   37.4141     0.7996   -59.3017  0.0426 0.2118  7e-04  2.2742 1.1914 0.0250
## 15   34.8781     0.7995    65.0962  0.0426 0.1961  7e-04  3.3158 1.1867 0.0220
## 16   32.2475     0.7985   -80.4640  0.0428 0.1947  7e-04  3.3503 1.1701 0.0178
## 17   30.2799     0.7982   -87.2771  0.0429 0.1785  7e-04  3.3771 1.1658 0.0157
## 18   28.5343     0.7980   -75.3418  0.0429 0.1770  7e-04  3.4654 1.1611 0.0137
## 19   26.9772     0.7978   -43.5942  0.0429 0.1544  7e-04  3.4503 1.1571 0.0117
## 20   25.6021     0.7976     8.8152  0.0430 0.1487  7e-04  4.4551 1.1525 0.0158
## 21   21.2347     0.7631   -31.0557  0.0559 0.1362  7e-04  4.3922 1.0742 0.0103
## 22   20.2561     0.7630   -52.6468  0.0559 0.1354  7e-04  5.1219 1.0710 0.0093
## 23   19.3314     0.7626   -30.2783  0.0560 0.1324  7e-04  5.0837 1.0658 0.0084
## 24   18.5143     0.7626     5.5107  0.0560 0.1314  7e-04  5.3436 1.0627 0.0076
## 25   17.1055     0.7587     3.9569  0.0584 0.1283  7e-04  5.3896 1.0412 0.0070
## 26   14.9416     0.7552     3.6854  0.0626 0.1228  7e-04  5.4352 0.9965 0.0063
## 27   13.6900     0.7537     5.0400  0.0647 0.1226  7e-04  6.0229 0.9742 0.0058
## 28   12.7076     0.7498   -23.1838  0.0671 0.1137  7e-04  6.2553 0.9551 0.0054
## 29   12.2632     0.7497     3.6912  0.0671 0.1132  7e-04  6.6182 0.9526 0.0050
## 30   11.2836     0.7477   -22.2239  0.0692 0.1255  7e-04  6.7054 0.9315 0.0046
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.5221           216.9679       0.8794
## 3          0.5214           215.7172       0.9467
## 4          0.5211           215.0914       0.5689
## 5          0.5193           211.9589       0.9681
## 6         -0.4219           -10.1102       0.1360
## 7          0.5190           211.3316       0.9829
## 8         -1.0633             0.0000          NaN
## 9          0.5186           210.7040       0.6147
## 10         0.5153           205.0437       0.7837
## 11        -0.1409           -56.6808       0.3783
## 12        -0.7431            -2.3458       1.0000
## 13         0.5142           203.1521       0.9855
## 14        -1.0633             0.0000          NaN
## 15        -0.2510           -24.9172       0.1985
## 16        -0.5522            -5.6219       1.0000
## 17        -0.7431            -2.3458       1.0000
## 18        -0.7431            -2.3458       1.0000
## 19        -0.7431            -2.3458       1.0000
## 20         0.5138           202.5209       0.8626
## 21        -1.0633             0.0000          NaN
## 22        -0.4219           -10.1102       1.0000
## 23        -1.0633             0.0000          NaN
## 24         0.5118           199.3611       1.0000
## 25         0.5114           198.7283       0.9044
## 26         0.5106           197.4618       0.9530
## 27         0.5102           196.8282       0.9647
## 28        -1.0633             0.0000          NaN
## 29         0.5098           196.1942       0.9537
## 30        -1.0633             0.0000          NaN
## 
## $Best.nc
##                      KL      CH Hartigan      CCC    Scott Marriot   TrCovW
## Number_clusters 24.0000 11.0000   5.0000   2.0000   5.0000      21      5.0
## Value_Index     16.3929 94.1712 116.8642 -16.6876 180.4541 5467935 768709.4
##                   TraceW Friedman  Rubin Cindex     DB Silhouette   Duda
## Number_clusters   5.0000  10.0000 11.000 3.0000 2.0000     2.0000 2.0000
## Value_Index     598.7869   2.8529 -0.854 0.1578 0.1428     0.8432 0.8857
##                 PseudoT2  Beale Ratkowsky     Ball PtBiserial    Frey McClain
## Number_clusters   2.0000 2.0000    5.0000   3.0000    11.0000  2.0000  2.0000
## Value_Index      30.5908 0.1285    0.2825 495.3075     0.8001 45.1353  0.0011
##                   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 2.0000      0  5.0000      0 30.0000
## Value_Index     0.3393      0  1.4509      0  0.0046
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      1      1      1      1      1      1      1      1      1      1      1 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      1      1      1      1      1      1      1      1      1      1      1 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      1      1      1      1      1      1      1      1      1      1      1 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      1      1      1      2      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      1      1      1      1      1      1      1      1      1      1      1 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      1      1      1      1      1      1      1      1      1
testH.nbclust_complete <- NbClust(data_aseg_pca$x[,1:2], distance="euclidean", min.nc=2, max.nc=30, method="complete", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 5 proposed 2 as the best number of clusters 
## * 4 proposed 3 as the best number of clusters 
## * 5 proposed 4 as the best number of clusters 
## * 2 proposed 8 as the best number of clusters 
## * 1 proposed 11 as the best number of clusters 
## * 1 proposed 20 as the best number of clusters 
## * 2 proposed 21 as the best number of clusters 
## * 2 proposed 27 as the best number of clusters 
## * 2 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_complete
## $All.index
##           KL       CH Hartigan      CCC     Scott   Marriot      TrCovW
## 2     0.1989 274.4417  19.7750  -6.6822  239.8246 1020102.0 849235.5138
## 3     0.9426 157.8438 211.1326 -11.6034  287.6048 1880896.2 788052.4939
## 4     4.2695 268.2141  32.6857  -9.0530  503.5821 1359622.6 129459.3632
## 5     0.6644 236.1874  13.6221 -10.5361  591.8383 1470731.6 129520.0713
## 6     0.9375 201.7648   8.3957 -12.6204  657.5118 1610852.4 123994.5743
## 7     0.3194 174.8190 177.2944 -14.5835  673.2606 2053293.7 118730.2758
## 8  2487.9275 287.9514  29.0567  -7.2817  881.9286 1124184.6  42898.2387
## 9     0.0040 285.9082  12.5732  -7.3614  950.5895 1068800.2  38179.3658
## 10    0.8821 268.2042   9.1683  -8.3223  973.5467 1199138.0  33307.5680
## 11    0.1514 250.8274 161.9481  -9.3446  988.5472 1363045.1  30723.4962
## 12   22.2203 402.2417  12.8759  -2.1437 1183.3142  720517.6   6197.7760
## 13    1.3800 388.9043  21.9340  -2.6951 1208.1163  762584.6   5059.2619
## 14    0.6870 393.6216   9.8615  -2.5446 1260.7643  710211.4   4817.7498
## 15    0.2734 380.4681  40.1590  -3.1126 1287.0393  730748.5   4544.6490
## 16    3.8962 419.2894   7.7642  -1.6543 1410.9406  496156.1   4459.7082
## 17    2.6336 405.3762  15.4903  -2.2283 1443.7826  488479.9   4437.8187
## 18    0.5995 407.1103   8.4700  -2.2161 1465.1080  501076.8   3736.6632
## 19    1.0855 397.8331   8.1511  -2.6297 1483.1843  517793.2   3566.9966
## 20    0.0394 389.4542 152.5336  -3.0180 1497.0477  541529.9   3329.4556
## 21  130.7167 631.2467   9.3582   4.4336 1692.9535  263935.4    648.4558
## 22    0.8950 624.4581  11.1034   4.1992 1714.1940  265135.9    647.9933
## 23    0.3311 624.0623   5.9066   4.1226 1735.4227  265255.4    544.4748
## 24    1.0575 610.6072   5.7329   3.7148 1747.2608  274921.8    529.8921
## 25    1.8365 598.1529   6.8460   3.3248 1759.0946  283957.1    480.0016
## 26    1.1358 590.0279   6.7065   3.0421 1775.0305  287397.2    480.2233
## 27    0.0648 582.6365  34.0211   2.7754 1789.1484  292224.1    467.2758
## 28   22.6908 648.8708   8.7222   4.3808 1878.6595  216435.7    460.9246
## 29    0.2697 648.6768  11.6020   4.3035 1897.2639  214853.8    407.4621
## 30  639.2639 658.0133   6.9933   4.4528 1929.2312  201253.2    362.1275
##       TraceW Friedman   Rubin Cindex     DB Silhouette     Duda Pseudot2
## 2  1352.9393   1.5632  2.1531 0.1752 0.4914     0.7968   0.3849  11.1864
## 3  1249.1497   1.9729  2.3320 0.2207 0.5984     0.7612   0.5030 226.2487
## 4   660.6270   4.9554  4.4095 0.2119 0.7317     0.6620   0.4715  20.1744
## 5   580.2616   6.0099  5.0202 0.2613 0.8005     0.6259   0.3081  11.2279
## 6   548.4688   6.6517  5.3112 0.2632 0.6979     0.6307  12.7844   0.0000
## 7   529.4719   7.0127  5.5018 0.2855 0.5816     0.6362   0.4842 222.5968
## 8   300.6791  15.8875  9.6882 0.2439 0.6214     0.5923   0.3429  21.0806
## 9   267.2122  20.6589 10.9016 0.2611 0.6226     0.5986   0.7157   1.9864
## 10  253.4188  20.9896 11.4949 0.2902 0.5701     0.6002   0.2429   9.3502
## 11  243.7042  22.2186 11.9532 0.3109 0.5472     0.6015   0.4706 210.3940
## 12  142.7511  36.4906 20.4064 0.2360 0.6069     0.5145   0.2582  14.3638
## 13  135.1204  38.2054 21.5588 0.2666 0.6015     0.5098   0.5067  19.4684
## 14  123.2147  40.1456 23.6420 0.2700 0.6425     0.5058   0.2821  10.1782
## 15  118.0630  40.8154 24.6736 0.2796 0.6137     0.5081   0.4554  46.6299
## 16  100.1821  49.0049 29.0774 0.3132 0.5970     0.5072   0.3212   6.3387
## 17   96.8259  51.7696 30.0853 0.3292 0.5976     0.5075   0.5331  26.2700
## 18   90.5369  54.9032 32.1751 0.3373 0.6023     0.4792   0.3552  12.7057
## 19   87.2096  58.4979 33.4027 0.3505 0.5999     0.4765   0.5612   8.5992
## 20   84.1075  61.4939 34.6347 0.3562 0.5931     0.4794   0.3947 223.9187
## 21   49.6697 101.8354 58.6481 0.2123 0.6307     0.4830  41.7462   0.0000
## 22   47.6343 106.2350 61.1542 0.2236 0.5941     0.4895  16.0106  -0.9375
## 23   45.3257 113.1391 64.2690 0.2411 0.5514     0.4924  11.1639  -1.8209
## 24   44.1246 117.7337 66.0184 0.2610 0.5324     0.4948  14.8400  -0.9326
## 25   42.9838 121.3795 67.7706 0.2621 0.5003     0.4971 105.5903   0.0000
## 26   41.6574 124.9363 69.9285 0.2682 0.4756     0.5009  43.2375   0.0000
## 27   40.3915 130.9862 72.1199 0.2801 0.4491     0.5060   0.4848  46.7623
## 28   34.8286 150.2423 83.6392 0.2721 0.4505     0.5048   0.5938   6.1559
## 29   33.4523 152.7262 87.0803 0.2792 0.4831     0.5034   0.7443   8.9311
## 30   31.7088 155.6101 91.8685 0.2788 0.5435     0.4913   0.2650   8.3195
##      Beale Ratkowsky     Ball Ptbiserial    Frey McClain   Dunn Hubert SDindex
## 2   1.3983    0.3478 676.4696     0.7761 10.8815  0.0135 0.2154  7e-04  6.4732
## 3   0.9837    0.3166 416.3832     0.7763  6.9241  0.0136 0.2717  8e-04  4.3673
## 4   1.0618    0.3695 165.1568     0.7687  4.5640  0.0534 0.1003  7e-04  3.9731
## 5   1.8713    0.3596 116.0523     0.7678  3.7196  0.0543 0.1246  7e-04  3.4523
## 6   0.0000    0.3451  91.4115     0.7678  1.8488  0.0544 0.1256  7e-04  2.7340
## 7   1.0600    0.3209  75.6388     0.7678  4.4362  0.0544 0.1362  7e-04  1.6137
## 8   1.7567    0.3085  37.5849     0.6936  2.2659  0.1040 0.0910  7e-04  1.6666
## 9   0.3311    0.2927  29.6902     0.6933  2.5474  0.1043 0.0979  7e-04  1.6525
## 10  2.3375    0.2812  25.3419     0.6932  3.6607  0.1044 0.1089  7e-04  1.6552
## 11  1.1191    0.2685  22.1549     0.6931  4.1658  0.1045 0.1167  7e-04  1.5455
## 12  2.3940    0.2673  11.8959     0.5410  1.0648  0.2175 0.0632  7e-04  2.1204
## 13  0.9271    0.2581  10.3939     0.5409  2.2696  0.2175 0.0715  7e-04  2.0932
## 14  2.0356    0.2518   8.8010     0.5385  1.4936  0.2193 0.0734  7e-04  2.4576
## 15  1.1657    0.2447   7.8709     0.5384  2.2597  0.2194 0.0760  7e-04  2.4860
## 16  1.5847    0.2408   6.2614     0.5325  1.9075  0.2240 0.0879  7e-04  2.4826
## 17  0.8474    0.2344   5.6956     0.5324  3.3585  0.2240 0.0925  7e-04  2.5217
## 18  1.5882    0.2280   5.0298     0.5295  3.9383  0.2267 0.0957  7e-04  2.8176
## 19  0.7166    0.2220   4.5900     0.5290  2.7645  0.2272 0.0995  7e-04  3.1244
## 20  1.5233    0.2165   4.2054     0.5284  2.6734  0.2277 0.1014  7e-04  3.2195
## 21  0.0000    0.2132   2.3652     0.4022  0.3841  0.3492 0.0460  7e-04  3.8474
## 22 -0.4688    0.2086   2.1652     0.4022  0.4466  0.3491 0.0485  7e-04  3.8474
## 23 -0.6070    0.2041   1.9707     0.4022  0.9672  0.3489 0.0524  7e-04  3.7294
## 24 -0.4663    0.1998   1.8385     0.4021  0.9155  0.3488 0.0567  7e-04  3.8119
## 25  0.0000    0.1959   1.7194     0.4021  0.6055  0.3488 0.0570  7e-04  3.8889
## 26  0.0000    0.1922   1.6022     0.4021  0.6347  0.3487 0.0584  7e-04  3.8000
## 27  1.0392    0.1887   1.4960     0.4021  1.6437  0.3486 0.0610  7e-04  3.7989
## 28  0.6156    0.1861   1.2439     0.3929  1.7568  0.3522 0.0634  7e-04  4.3980
## 29  0.3308    0.1830   1.1535     0.3921  2.7272  0.3526 0.0654  7e-04  5.3258
## 30  2.0799    0.1802   1.0570     0.3863  1.4326  0.3594 0.0669  7e-04  7.5270
##    Dindex   SDbw
## 2  1.8034 0.8774
## 3  1.7696 0.5583
## 4  1.3615 0.5502
## 5  1.2996 0.6952
## 6  1.2660 0.4876
## 7  1.2403 0.1356
## 8  0.9620 0.1156
## 9  0.9143 0.0895
## 10 0.8971 0.0747
## 11 0.8821 0.0543
## 12 0.6777 0.0523
## 13 0.6631 0.0469
## 14 0.6366 0.0442
## 15 0.6234 0.0390
## 16 0.5781 0.0445
## 17 0.5682 0.0405
## 18 0.5491 0.0442
## 19 0.5389 0.0400
## 20 0.5315 0.0315
## 21 0.3936 0.0300
## 22 0.3851 0.0196
## 23 0.3767 0.0160
## 24 0.3725 0.0145
## 25 0.3677 0.0124
## 26 0.3609 0.0103
## 27 0.3543 0.0080
## 28 0.3304 0.0084
## 29 0.3246 0.0082
## 30 0.3165 0.0082
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2         -0.1409           -56.6808       0.2795
## 3          0.5193           211.9589       0.3747
## 4          0.1299           120.5893       0.3564
## 5         -0.2510           -24.9172       0.2040
## 6         -1.0633             0.0000          NaN
## 7          0.5118           199.3611       0.3474
## 8         -0.0027         -4016.9908       0.1960
## 9         -0.2510           -24.9172       0.7257
## 10        -0.4219           -10.1102       0.1776
## 11         0.5022           185.3717       0.3277
## 12        -0.2510           -24.9172       0.1414
## 13         0.1556           108.5678       0.4040
## 14        -0.3258           -16.2785       0.1929
## 15         0.2963            92.6281       0.3171
## 16        -0.4219           -10.1102       0.2802
## 17         0.2454            92.2265       0.4336
## 18        -0.1409           -56.6808       0.2390
## 19        -0.0027         -4016.9908       0.4995
## 20         0.4788           158.9030       0.2197
## 21        -1.0633             0.0000          NaN
## 22        -0.7431            -2.3458       1.0000
## 23        -0.5522            -5.6219       1.0000
## 24        -0.7431            -2.3458       1.0000
## 25        -1.0633             0.0000          NaN
## 26        -1.0633             0.0000          NaN
## 27         0.3178            94.4356       0.3581
## 28        -0.0624          -153.2995       0.5513
## 29         0.2153            94.7474       0.7199
## 30        -0.4219           -10.1102       0.2060
## 
## $Best.nc
##                       KL       CH Hartigan     CCC    Scott  Marriot   TrCovW
## Number_clusters    8.000  30.0000   3.0000 30.0000   4.0000      8.0      4.0
## Value_Index     2487.927 658.0133 191.3576  4.4528 215.9773 873724.8 658593.1
##                   TraceW Friedman    Rubin Cindex      DB Silhouette   Duda
## Number_clusters   4.0000  21.0000  21.0000 2.0000 27.0000     2.0000 2.0000
## Value_Index     508.1573  40.3415 -21.5073 0.1752  0.4491     0.7968 0.3849
##                 PseudoT2  Beale Ratkowsky     Ball PtBiserial    Frey McClain
## Number_clusters   4.0000 2.0000    4.0000   3.0000     3.0000 20.0000  2.0000
## Value_Index      20.1744 1.3983    0.3695 260.0864     0.7763  2.6734  0.0135
##                   Dunn Hubert SDindex Dindex   SDbw
## Number_clusters 3.0000      0 11.0000      0 27.000
## Value_Index     0.2717      0  1.5455      0  0.008
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      2      2      2      2      2      2      2      2      2      2 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      2      2      2      1      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      2      2      2      2      2      2      2      2      2      2      2 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      2      2      2      2      2      2      2      2      2
testH.nbclust_Average <- NbClust(data_aseg_pca$x[,1:2], distance="euclidean", min.nc=2, max.nc=30, method="average", index="all")
## [1] "Frey index : No clustering structure in this data set"

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 6 proposed 2 as the best number of clusters 
## * 3 proposed 3 as the best number of clusters 
## * 3 proposed 4 as the best number of clusters 
## * 2 proposed 6 as the best number of clusters 
## * 3 proposed 8 as the best number of clusters 
## * 2 proposed 18 as the best number of clusters 
## * 2 proposed 22 as the best number of clusters 
## * 1 proposed 26 as the best number of clusters 
## * 1 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_Average
## $All.index
##         KL       CH Hartigan      CCC     Scott   Marriot     TrCovW    TraceW
## 2   0.1968 274.4417  19.0073  -6.6822  239.8246 1020102.0 849235.514 1352.9393
## 3   1.0056 157.0207 201.0029 -11.6452  283.8989 1910165.5 786561.215 1252.8812
## 4   2.0903 259.3634  13.0075  -9.5315  474.6847 1533592.5 179863.113  677.9243
## 5   0.9488 207.6124   7.2030 -12.3498  520.7486 1977764.9 173780.675  642.5113
## 6   1.4956 171.8868  15.4608 -14.8420  533.0420 2705773.2 165763.068  623.4032
## 7   0.2012 154.6163 232.9312 -16.2760  597.5634 2814683.6 164248.054  584.7666
## 8   8.0446 297.0131   6.7491  -6.8138  845.9589 1305950.8  31122.577  292.4265
## 9   1.3655 267.1340  42.7586  -8.3848  857.4217 1575757.0  28343.635  284.1600
## 10  1.2458 284.9175  10.1628  -7.4088  961.2429 1262215.7  23380.849  239.7768
## 11  1.0090 267.6039   8.8570  -8.3691  977.1481 1429346.4  20922.707  229.6303
## 12  0.7323 252.3837   3.8412  -9.2779 1005.0656 1514245.7  20711.655  221.0797
## 13  0.9943 234.5363   3.2498 -10.4173 1014.0436 1711883.6  19881.410  217.4168
## 14  1.8665 218.8760  23.9272 -11.4983 1019.0729 1944208.6  19155.009  214.3481
## 15  0.6670 225.4668   3.9646 -11.1024 1105.5067 1556904.9  19026.385  193.8271
## 16  0.8837 213.4550   2.2290 -11.9789 1123.1702 1645721.3  18959.071  190.4709
## 17  0.1139 201.3418 194.3585 -12.9136 1127.5957 1823921.2  18658.022  188.5943
## 18 18.4246 364.4489   2.9612  -3.9281 1354.6107  794064.6   3569.371  100.7683
## 19  0.2232 347.3856  62.4077  -4.7243 1362.4984  856139.8   3569.046   99.4419
## 20 14.5874 423.3967  14.5089  -1.7238 1521.6369  488795.1   2758.265   77.5443
## 21  1.3338 427.5268  13.0715  -1.6344 1558.2267  462694.1   2602.950   72.7467
## 22  0.4102 430.1205   3.6840  -1.6035 1586.9721  450488.9   2390.873   68.6492
## 23  1.0963 415.7594   3.9306  -2.1941 1592.5085  481145.0   2283.375   67.5084
## 24  0.9662 403.1905   3.4783  -2.7351 1604.2470  498886.1   2253.453   66.3073
## 25  0.3707 390.9396  24.4529  -3.2793 1615.0312  517540.4   2227.964   65.2565
## 26  3.4540 417.0162   3.7609  -2.3481 1686.3967  415786.8   2000.674   58.5925
## 27  0.9753 406.2614   3.4254  -2.8217 1693.1250  435989.8   1917.301   57.5806
## 28  0.8327 395.7661   2.3694  -3.2966 1704.1847  447766.6   1874.789   56.6692
## 29  4.2064 384.1608   8.5122  -3.8276 1707.6795  473377.5   1821.063   56.0429
## 30  2.1083 384.3407   6.4186  -3.8920 1749.7744  425089.9   1807.399   53.8697
##    Friedman   Rubin Cindex     DB Silhouette     Duda Pseudot2   Beale
## 2    1.5632  2.1531 0.1752 0.4914     0.7968   1.3159  -1.6803 -0.2100
## 3    1.9460  2.3251 0.2208 0.3740     0.7779   0.5145 216.1118  0.9396
## 4    5.2071  4.2970 0.2246 0.6076     0.6917   0.4844   6.3872  0.9125
## 5    5.5470  4.5338 0.2570 0.5437     0.6928   3.2042  -2.7516 -0.5503
## 6    5.8579  4.6728 0.2586 0.4604     0.6951   0.4194  16.6144  1.2780
## 7    6.6034  4.9815 0.2581 0.5351     0.6754   0.4613 251.0484  1.1623
## 8   17.7832  9.9616 0.1814 0.6098     0.6059   3.6137  -2.8931 -0.5786
## 9   18.3330 10.2514 0.1813 0.5494     0.6033   0.4896  31.2743  1.0088
## 10  20.0502 12.1489 0.2755 0.5311     0.5962   0.2093  11.3367  2.8342
## 11  21.2256 12.6858 0.2910 0.4727     0.6012   0.4181   8.3501  1.1929
## 12  22.1735 13.1764 0.2907 0.5049     0.6020   4.9738  -3.1958 -0.6392
## 13  22.3014 13.3984 0.2906 0.4786     0.5944   9.1521  -2.6722 -0.6681
## 14  22.6819 13.5902 0.2906 0.4389     0.5989   0.4531  28.9701  1.1588
## 15  26.9072 15.0290 0.3569 0.4855     0.5611   0.3212   6.3387  1.5847
## 16  27.8593 15.2939 0.3568 0.4927     0.5615  16.1084  -0.9379 -0.4690
## 17  28.3358 15.4460 0.3568 0.4590     0.5626   0.4625 212.6540  1.1557
## 18  57.4219 28.9082 0.2501 0.5024     0.5015 105.5903   0.0000  0.0000
## 19  58.5163 29.2939 0.2501 0.4714     0.5053   0.4413  64.5735  1.2418
## 20  68.4175 37.5661 0.2493 0.4843     0.5151   0.4708  12.3641  1.0303
## 21  77.9222 40.0435 0.2479 0.5055     0.5075   0.4836  11.7466  0.9789
## 22  86.8942 42.4336 0.3449 0.5134     0.5072  14.8400  -0.9326 -0.4663
## 23  87.7972 43.1507 0.3448 0.4864     0.5088  11.1639  -1.8209 -0.6070
## 24  91.6009 43.9323 0.3466 0.4610     0.5112  23.2289  -0.9570 -0.4785
## 25  95.2597 44.6398 0.3466 0.4452     0.5133   0.3638  40.2140  1.6756
## 26 125.2129 49.7168 0.3627 0.4474     0.4613  10.5721  -2.7162 -0.6791
## 27 125.5077 50.5906 0.3625 0.4349     0.4629   2.9515  -1.9836 -0.4959
## 28 127.3175 51.4042 0.3624 0.4229     0.4629  85.4573   0.0000  0.0000
## 29 127.8024 51.9787 0.3624 0.3911     0.4686   0.2823  17.8000  2.2250
## 30 142.5682 54.0756 0.3618 0.3886     0.4677   0.3715  10.1499  1.4500
##    Ratkowsky     Ball Ptbiserial    Frey McClain   Dunn Hubert SDindex Dindex
## 2     0.3478 676.4696     0.7761  4.3720  0.0135 0.2154  7e-04  3.4730 1.8034
## 3     0.3132 417.6271     0.7769  6.6824  0.0135 0.2717  8e-04  1.4485 1.7616
## 4     0.3282 169.4811     0.8036  5.6050  0.0396 0.2027  7e-04  1.4366 1.3704
## 5     0.3250 128.5023     0.8034  7.2189  0.0397 0.2322  7e-04  1.3561 1.3386
## 6     0.2977 103.9005     0.8033 14.0111  0.0398 0.2338  7e-04  1.0926 1.3191
## 7     0.2966  83.5381     0.8010  5.0972  0.0404 0.1633  7e-04  1.2421 1.2819
## 8     0.2931  36.5533     0.6822  2.3576  0.1112 0.0624  7e-04  1.2545 0.9392
## 9     0.2778  31.5733     0.6822  2.4926  0.1113 0.0624  7e-04  1.2821 0.9256
## 10    0.2813  23.9777     0.6804  2.8911  0.1128 0.0963  7e-04  1.2663 0.8742
## 11    0.2687  20.8755     0.6803  4.8525  0.1129 0.1018  7e-04  1.2507 0.8568
## 12    0.2598  18.4233     0.6799  6.5827  0.1131 0.1018  7e-04  1.6582 0.8383
## 13    0.2506  16.7244     0.6798  7.3641  0.1132 0.1018  7e-04  1.6535 0.8283
## 14    0.2416  15.3106     0.6797  7.5383  0.1133 0.1018  7e-04  1.8121 0.8205
## 15    0.2382  12.9218     0.6741  9.6036  0.1164 0.1249  7e-04  1.9941 0.7812
## 16    0.2314  11.9044     0.6739 11.0031  0.1165 0.1249  7e-04  2.1614 0.7713
## 17    0.2246  11.0938     0.6738  4.0784  0.1165 0.1249  7e-04  2.1508 0.7653
## 18    0.2227   5.5982     0.4927  1.6983  0.2677 0.0627  7e-04  2.3433 0.5657
## 19    0.2169   5.2338     0.4927  2.0086  0.2677 0.0627  7e-04  2.3219 0.5589
## 20    0.2165   3.8772     0.4773  1.8208  0.2802 0.0682  7e-04  2.7056 0.4997
## 21    0.2114   3.4641     0.4764  2.1437  0.2808 0.0682  7e-04  2.8302 0.4871
## 22    0.2066   3.1204     0.4756  2.1401  0.2815 0.0954  7e-04  2.9526 0.4774
## 23    0.2022   2.9351     0.4755  2.2774  0.2816 0.0954  7e-04  3.0287 0.4727
## 24    0.1980   2.7628     0.4755  2.4040  0.2816 0.0959  7e-04  3.0999 0.4684
## 25    0.1940   2.6103     0.4754  2.7574  0.2817 0.0959  7e-04  3.1291 0.4644
## 26    0.1904   2.2536     0.4716  2.8990  0.2856 0.1018  7e-04  3.5287 0.4398
## 27    0.1870   2.1326     0.4715  2.9991  0.2857 0.1018  7e-04  3.5556 0.4346
## 28    0.1838   2.0239     0.4714  3.0374  0.2858 0.1018  7e-04  3.6523 0.4307
## 29    0.1807   1.9325     0.4714  3.6278  0.2859 0.1018  7e-04  3.6262 0.4260
## 30    0.1780   1.7957     0.4708  4.1667  0.2865 0.1018  7e-04  3.9493 0.4156
##      SDbw
## 2  0.8774
## 3  0.6590
## 4  0.3319
## 5  0.2129
## 6  0.1335
## 7  0.0989
## 8  0.1013
## 9  0.0819
## 10 0.0731
## 11 0.0523
## 12 0.0457
## 13 0.0446
## 14 0.0304
## 15 0.0375
## 16 0.0409
## 17 0.0348
## 18 0.0243
## 19 0.0220
## 20 0.0204
## 21 0.0191
## 22 0.0179
## 23 0.0161
## 24 0.0145
## 25 0.0094
## 26 0.0088
## 27 0.0080
## 28 0.0074
## 29 0.0064
## 30 0.0058
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2         -0.1409           -56.6808       1.0000
## 3          0.5193           211.9589       0.3915
## 4         -0.1908           -37.4469       0.4277
## 5         -0.3258           -16.2785       1.0000
## 6          0.0222           529.7343       0.2969
## 7          0.5142           203.1521       0.3138
## 8         -0.3258           -16.2785       1.0000
## 9          0.2454            92.2265       0.3707
## 10        -0.4219           -10.1102       0.1360
## 11        -0.1908           -37.4469       0.3369
## 12        -0.3258           -16.2785       1.0000
## 13        -0.4219           -10.1102       1.0000
## 14         0.1977            97.3833       0.3225
## 15        -0.4219           -10.1102       0.2802
## 16        -0.7431            -2.3458       1.0000
## 17         0.5003           182.8126       0.3160
## 18        -1.0633             0.0000          NaN
## 19         0.3427            97.8113       0.2932
## 20        -0.0027         -4016.9908       0.3735
## 21        -0.0027         -4016.9908       0.3915
## 22        -0.7431            -2.3458       1.0000
## 23        -0.5522            -5.6219       1.0000
## 24        -0.7431            -2.3458       1.0000
## 25         0.1881            99.2518       0.1984
## 26        -0.4219           -10.1102       1.0000
## 27        -0.4219           -10.1102       1.0000
## 28        -1.0633             0.0000          NaN
## 29        -0.1409           -56.6808       0.1449
## 30        -0.1908           -37.4469       0.2729
## 
## $Best.nc
##                      KL       CH Hartigan     CCC    Scott Marriot   TrCovW
## Number_clusters 18.0000  22.0000   8.0000 22.0000   8.0000       8      4.0
## Value_Index     18.4246 430.1205 226.1821 -1.6035 248.3955 1778539 606698.1
##                   TraceW Friedman    Rubin Cindex    DB Silhouette   Duda
## Number_clusters   4.0000  26.0000  18.0000 2.0000 3.000     2.0000 2.0000
## Value_Index     539.5438  29.9532 -13.0766 0.1752 0.374     0.7968 1.3159
##                 PseudoT2 Beale Ratkowsky     Ball PtBiserial Frey McClain
## Number_clusters   6.0000  2.00    2.0000   3.0000     4.0000   NA  2.0000
## Value_Index      16.6144 -0.21    0.3478 258.8426     0.8036   NA  0.0135
##                   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 3.0000      0  6.0000      0 30.0000
## Value_Index     0.2717      0  1.0926      0  0.0058
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      2      2      2      2      2      2      2      2      2      2 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      2      2      2      1      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      2      2      2      2      2      2      2      2      2      2      2 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      2      2      2      2      2      2      2      2      2
testH.nbclust_ward <- NbClust(data_aseg_pca$x[,1:2], distance="euclidean", min.nc=2, max.nc=30, method="ward.D2", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 5 proposed 2 as the best number of clusters 
## * 5 proposed 3 as the best number of clusters 
## * 5 proposed 4 as the best number of clusters 
## * 1 proposed 6 as the best number of clusters 
## * 1 proposed 9 as the best number of clusters 
## * 1 proposed 11 as the best number of clusters 
## * 1 proposed 12 as the best number of clusters 
## * 1 proposed 27 as the best number of clusters 
## * 1 proposed 29 as the best number of clusters 
## * 3 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_ward
## $All.index
##         KL       CH Hartigan     CCC     Scott    Marriot      TrCovW    TraceW
## 2   5.1126 394.2464  97.4622 -3.0642  281.8735  856157.21 325460.6253 1096.5701
## 3   0.3975 325.2050 135.5389 -4.9574  411.8507 1120816.12 206645.2458  777.9825
## 4  66.1171 384.3429  59.8005 -3.7062  593.7157  933934.17  45772.8915  494.9332
## 5   0.0308 374.6551  74.2538 -3.6856  690.8346  973629.52  36645.6607  394.8751
## 6   3.2412 407.5380  47.3904 -2.1576  849.2645  724548.86  21913.9141  300.0631
## 7   1.5894 414.5143  38.5365 -1.7654  919.7744  735138.78  15445.8636  249.5279
## 8   0.3357 417.7662  45.2860 -1.5731  985.3506  730616.20  11905.9794  214.1149
## 9   1.4389 440.6524  37.3019 -0.7072 1074.2321  638495.47   9077.9256  179.1459
## 10  0.5906 457.0987  44.0324 -0.1256 1144.6973  587708.28   5053.4029  154.2393
## 11  0.8607 492.4003  47.5260  1.0298 1247.7802  462820.26   3721.6416  129.4556
## 12  1.9453 542.4792  33.3643  2.5266 1327.1991  395618.19   2066.6447  107.2063
## 13  1.1097 570.3088  30.9811  3.2791 1384.3966  365845.90   1808.5203   93.5210
## 14  1.8100 598.0246  23.7744  3.9835 1448.3096  325097.10   1701.8688   82.2900
## 15  0.9355 612.7001  23.3674  4.3171 1507.6806  291409.72   1567.4002   74.4573
## 16  0.8209 629.9888  24.5564  4.7022 1551.0297  276769.79   1421.9014   67.4521
## 17  0.8780 653.9637  25.6803  5.2328 1597.5660  257375.02   1232.1655   60.7881
## 18  0.8115 684.8006  28.7757  5.8969 1647.6910  234158.20    842.6328   54.5107
## 19  1.3488 728.8892  24.3975  6.8141 1704.5326  205879.59    523.6842   48.2558
## 20  1.4254 764.5668  20.2494  7.4995 1750.4778  188375.94    465.3844   43.4582
## 21  0.9830 790.5950  20.1705  7.9582 1796.8272  171211.11    341.2341   39.7953
## 22  1.2739 819.4976  17.7679  8.4530 1843.8563  154467.37    327.9413   36.4392
## 23  1.3602 842.9272  15.3244  8.8253 1878.8799  145904.84    313.8831   33.6931
## 24  1.0118 859.9023  14.9367  9.0668 1909.0507  140100.83    244.7565   31.4706
## 25  0.9004 877.5994  15.3502  9.3140 1942.1836  132416.55    244.5250   29.4351
## 26  0.9296 899.0594  15.6898  9.6194 1973.1230  125898.95    221.7340   27.4736
## 27  1.2121 924.1260  14.1798  9.9760 2005.9531  118411.81    194.8258   25.5969
## 28  1.3228 945.2078  12.4028 10.2543 2035.8910  112411.08    156.3931   23.9993
## 29  0.9453 960.6637  12.4780 10.4327 2065.5055  106585.90    156.3964   22.6728
## 30  0.9179 978.1617  12.7843 10.6389 2097.7616   99718.77    156.3033   21.4069
##    Friedman    Rubin Cindex     DB Silhouette     Duda Pseudot2   Beale
## 2    2.2068   2.6565 0.0911 0.6943     0.7611   0.4248  28.4357  1.2925
## 3    4.0161   3.7443 0.1311 0.7588     0.6882   0.4784 234.3723  1.0851
## 4    8.7187   5.8857 0.0768 0.8854     0.4925   1.3159  -1.6803 -0.2100
## 5   12.8001   7.3771 0.1310 0.7391     0.4970   0.5753  61.2619  0.7293
## 6   25.3827   9.7081 0.0975 0.8880     0.4739   0.3661  45.0129  1.6671
## 7   25.9801  11.6742 0.0934 0.7500     0.4910   0.4844   6.3872  0.9125
## 8   26.6738  13.6050 0.1062 0.7118     0.4933   0.4745  13.2905  1.0223
## 9   27.8481  16.2607 0.1044 0.7138     0.4964   0.3155 282.0429  2.1530
## 10  33.9375  18.8865 0.0854 0.7445     0.4512   0.4914  56.9232  1.0165
## 11  36.0356  22.5022 0.0651 0.7232     0.4922   0.3034   9.1842  1.8368
## 12  48.6111  27.1722 0.0793 0.6961     0.4952   0.4861   7.4012  0.9251
## 13  58.8411  31.1485 0.1246 0.6839     0.4972   0.5347  17.4050  0.8288
## 14  61.8379  35.3996 0.1165 0.7300     0.4905   0.2819  12.7343  2.1224
## 15  64.2855  39.1236 0.1152 0.7057     0.4927  12.4791  -0.9199 -0.4599
## 16  74.0391  43.1868 0.1559 0.6380     0.4968   0.3638  40.2140  1.6756
## 17  87.7910  47.9212 0.1473 0.6366     0.4897   0.4275  84.3585  1.3181
## 18  93.8148  53.4397 0.1158 0.6354     0.4970   0.5357  26.0043  0.8388
## 19 108.1605  60.3665 0.0990 0.6687     0.5018   0.4708  12.3641  1.0303
## 20 126.8929  67.0308 0.0941 0.6792     0.5035   4.9738  -3.1958 -0.6392
## 21 133.2075  73.2005 0.1024 0.6559     0.5071   0.3212   6.3387  1.5847
## 22 137.7178  79.9424 0.1113 0.6537     0.5078   0.4162  30.8602  1.3417
## 23 152.0875  86.4581 0.1026 0.6514     0.5118   0.4212   9.6202  1.2025
## 24 166.0320  92.5637 0.1004 0.6433     0.5104  41.7462   0.0000  0.0000
## 25 177.4482  98.9646 0.1002 0.6096     0.5167   0.2192  21.3710  3.0530
## 26 198.6676 106.0303 0.0985 0.5889     0.5239  16.1084  -0.9379 -0.4690
## 27 225.0476 113.8041 0.1175 0.5642     0.5250   0.3715  10.1499  1.4500
## 28 231.6725 121.3802 0.1157 0.5626     0.5279 105.5903   0.0000  0.0000
## 29 245.8728 128.4814 0.1155 0.5403     0.5317   0.5470  27.3313  0.8039
## 30 262.3889 136.0795 0.1022 0.5602     0.5313   0.4722  15.6515  1.0434
##    Ratkowsky     Ball Ptbiserial    Frey McClain   Dunn Hubert SDindex Dindex
## 2     0.3214 548.2851     0.7951  1.1143  0.0390 0.0805  7e-04  9.7204 1.5709
## 3     0.3577 259.3275     0.8026  6.6768  0.0396 0.1182  7e-04  6.3281 1.4121
## 4     0.3481 123.7333     0.4935 -0.4630  0.2852 0.0126  7e-04  5.5625 1.0167
## 5     0.3252  78.9750     0.4946  0.6809  0.2836 0.0215  7e-04  3.2461 0.9750
## 6     0.3007  50.0105     0.4943  0.2784  0.2766 0.0215  7e-04  3.7235 0.8311
## 7     0.3096  35.6468     0.4960 -0.0909  0.2706 0.0215  7e-04  3.4418 0.7732
## 8     0.3063  26.7644     0.4966  0.1971  0.2691 0.0247  7e-04  3.0994 0.7414
## 9     0.3027  19.9051     0.4975  5.5429  0.2665 0.0247  7e-04  2.9770 0.7054
## 10    0.2904  15.4239     0.3603  0.5826  0.5459 0.0126  7e-04  4.6099 0.5648
## 11    0.2850  11.7687     0.3482 -0.0075  0.5049 0.0126  7e-04  4.6876 0.5036
## 12    0.2735   8.9339     0.3486  0.0539  0.4994 0.0155  7e-04  4.2723 0.4824
## 13    0.2636   7.1939     0.3489  0.3743  0.4929 0.0248  7e-04  4.3061 0.4681
## 14    0.2567   5.8779     0.3478  0.1814  0.4748 0.0248  7e-04  4.4313 0.4426
## 15    0.2500   4.9638     0.3478  0.0434  0.4711 0.0248  7e-04  4.4740 0.4242
## 16    0.2424   4.2158     0.3479  0.7300  0.4701 0.0336  7e-04  4.0885 0.4085
## 17    0.2354   3.5758     0.3447  1.6306  0.4616 0.0336  7e-04  4.3746 0.3839
## 18    0.2297   3.0284     0.3092  0.4479  0.4943 0.0336  8e-04  5.8798 0.3485
## 19    0.2241   2.5398     0.3051  0.2860  0.4495 0.0336  8e-04  6.0191 0.3309
## 20    0.2186   2.1729     0.3046  0.1253  0.4332 0.0336  8e-04  6.1843 0.3183
## 21    0.2140   1.8950     0.3046  0.1788  0.4295 0.0369  8e-04  6.2006 0.3083
## 22    0.2097   1.6563     0.3046  0.7275  0.4258 0.0405  8e-04  6.2532 0.2984
## 23    0.2053   1.4649     0.3010  0.4489  0.4087 0.0405  8e-04  6.3744 0.2849
## 24    0.2011   1.3113     0.3005  0.1295  0.4029 0.0405  8e-04  6.5510 0.2766
## 25    0.1972   1.1774     0.3005  0.4602  0.4021 0.0405  8e-04  6.5045 0.2682
## 26    0.1934   1.0567     0.3001  0.1818  0.3975 0.0405  8e-04  6.6241 0.2581
## 27    0.1899   0.9480     0.3001  0.4860  0.3962 0.0485  8e-04  6.4344 0.2520
## 28    0.1867   0.8571     0.2997  0.2017  0.3920 0.0485  8e-04  6.5617 0.2448
## 29    0.1836   0.7818     0.2997  1.5887  0.3914 0.0485  8e-04  6.3656 0.2381
## 30    0.1806   0.7136     0.2876  0.8137  0.3864 0.0485  8e-04  9.9723 0.2280
##      SDbw
## 2  1.0724
## 3  0.7017
## 4  0.5998
## 5  0.2718
## 6  0.2466
## 7  0.1942
## 8  0.1640
## 9  0.1296
## 10 0.1237
## 11 0.1293
## 12 0.1005
## 13 0.0847
## 14 0.0876
## 15 0.0742
## 16 0.0531
## 17 0.0606
## 18 0.0662
## 19 0.0560
## 20 0.0513
## 21 0.0448
## 22 0.0367
## 23 0.0356
## 24 0.0284
## 25 0.0253
## 26 0.0203
## 27 0.0204
## 28 0.0191
## 29 0.0142
## 30 0.0136
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.1671           104.6547       0.2853
## 3          0.5142           203.1521       0.3388
## 4         -0.1409           -56.6808       1.0000
## 5          0.4140           117.4720       0.4838
## 6          0.2153            94.7474       0.1987
## 7         -0.1908           -37.4469       0.4277
## 8          0.0222           529.7343       0.3749
## 9          0.4669           148.4332       0.1182
## 10         0.3548           100.0055       0.3652
## 11        -0.3258           -16.2785       0.2206
## 12        -0.1409           -56.6808       0.4194
## 13         0.1556           108.5678       0.4439
## 14        -0.2510           -24.9172       0.1705
## 15        -0.7431            -2.3458       1.0000
## 16         0.1881            99.2518       0.1984
## 17         0.3756           104.7312       0.2713
## 18         0.2454            92.2265       0.4372
## 19        -0.0027         -4016.9908       0.3735
## 20        -0.3258           -16.2785       1.0000
## 21        -0.4219           -10.1102       0.2802
## 22         0.1780           101.6241       0.2719
## 23        -0.1409           -56.6808       0.3296
## 24        -1.0633             0.0000          NaN
## 25        -0.1908           -37.4469       0.0848
## 26        -0.7431            -2.3458       1.0000
## 27        -0.1908           -37.4469       0.2729
## 28        -1.0633             0.0000          NaN
## 29         0.2646            91.7349       0.4519
## 30         0.0647           202.2234       0.3655
## 
## $Best.nc
##                      KL       CH Hartigan     CCC    Scott  Marriot   TrCovW
## Number_clusters  4.0000  30.0000   4.0000 30.0000   4.0000      6.0      4.0
## Value_Index     66.1171 978.1617  75.7384 10.6389 181.8649 259670.6 160872.4
##                   TraceW Friedman   Rubin  Cindex      DB Silhouette   Duda
## Number_clusters   4.0000    27.00 12.0000 11.0000 29.0000     2.0000 2.0000
## Value_Index     182.9912    26.38 -0.6938  0.0651  0.5403     0.7611 0.4248
##                 PseudoT2  Beale Ratkowsky     Ball PtBiserial   Frey McClain
## Number_clusters   2.0000 2.0000    3.0000   3.0000     3.0000 3.0000   2.000
## Value_Index      28.4357 1.2925    0.3577 288.9576     0.8026 6.6768   0.039
##                   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 3.0000      0   9.000      0 30.0000
## Value_Index     0.1182      0   2.977      0  0.0136
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      1      1      1      2      2      2      2      2      2      2 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      2      2      2      1      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      2      2      2      2      2      2      2      2      2      2      2 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      2      2      2      2      2      2      2      2      2
# Distancia Manhattan y varios linkage

testH.nbclust_single_m <- NbClust(data_aseg_pca$x[,1:2], distance="manhattan", min.nc=2, max.nc=30, method="single", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 9 proposed 2 as the best number of clusters 
## * 2 proposed 3 as the best number of clusters 
## * 5 proposed 5 as the best number of clusters 
## * 5 proposed 11 as the best number of clusters 
## * 1 proposed 15 as the best number of clusters 
## * 1 proposed 21 as the best number of clusters 
## * 1 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_single_m
## $All.index
##        KL      CH Hartigan      CCC    Scott  Marriot     TrCovW    TraceW
## 2  1.2110 48.6294  30.7199 -16.6876  68.7971  2080310 2630470.33 2418.8114
## 3  1.4687 42.6332  33.4613 -19.1743 112.2717  3905176 1921788.95 2142.2946
## 4  0.7132 43.4048 133.6979 -28.3906 156.4042  5776407 1529582.40 1877.2509
## 5  2.1139 84.0625  16.8337 -23.4437 336.8583  4255349  760872.97 1198.3600
## 6  0.6854 75.1131   2.1426 -24.9786 357.7335  5617238  659894.86 1118.2560
## 7  0.9542 63.2530   0.2792 -27.1668 360.3148  7563891  645744.90 1108.1096
## 8  1.9672 54.0886  13.5292 -29.1924 361.8786  9815204  645786.16 1106.7831
## 9  2.7313 51.5555  31.3605 -30.0696 378.2332 11604057  562615.81 1045.7967
## 10 0.0695 55.2926 141.6699 -29.6203 443.9265 10895543  400447.15  920.7906
## 11 7.0128 94.1712   5.1567 -23.2732 597.4925  6952601  149776.21  569.8117
## 12 0.7867 87.6224   0.7704 -24.3566 621.6371  7482268  149273.06  557.2630
## 13 0.9748 80.3024   0.2563 -25.6432 622.9010  8735149  148372.50  555.3863
## 14 6.7540 73.9017  13.6456 -26.8762 623.4187 10108878  147912.30  554.7600
## 15 0.1985 73.4148   3.1447 -27.1054 662.5682  9857935  128269.88  523.1715
## 16 0.8212 69.3781   0.4964 -27.9923 679.6850 10444066  127946.53  515.9602
## 17 0.9994 64.9260   0.4330 -29.0087 680.5920 11745898  127166.62  514.8193
## 18 1.0061 60.9763   0.4549 -29.9777 682.4360 13067620  127151.28  513.8215
## 19 0.9913 57.4719   0.3142 -30.8992 683.5683 14491378  126887.74  512.7707
## 20 0.9842 54.2942   0.1198 -31.7897 684.1661 16016986  126615.91  512.0428
## 21 7.5035 51.3790   8.9974 -32.6572 684.6560 17622716  126480.23  511.7641
## 22 0.1148 51.1366  23.0090 -32.8608 712.3318 17234499  114429.54  491.5685
## 23 0.4449 54.7577  43.0318 -32.0832 786.5739 13824944   88769.92  444.6386
## 24 3.7186 64.3365   0.3669 -30.0060 866.1681 10804373   68857.97  371.0568
## 25 0.9796 61.4898   0.1719 -30.7446 868.2853 11620531   68701.78  370.4275
## 26 0.9932 58.8095   0.1053 -31.4737 868.6110 12551719   68615.15  370.1316
## 27 3.4458 56.3151  10.2579 -32.1859 869.2832 13497949   68619.87  369.9496
## 28 0.3132 56.9523   0.3015 -32.1439 882.8648 13717646   61251.93  352.9518
## 29 0.9847 54.7477   0.1755 -32.8032 884.8873 14591488   61240.39  352.4505
## 30 4.2932 52.6591   8.6567 -33.4545 885.2095 15594201   61103.15  352.1576
##    Friedman  Rubin Cindex     DB Silhouette     Duda Pseudot2   Beale Ratkowsky
## 2    0.2986 1.2043 0.1799 0.1428     0.8402   0.8857  30.5908  0.1285    0.2432
## 3    0.5365 1.3598 0.1711 0.2605     0.7153   0.9478  12.9338  0.0548    0.2567
## 4    0.7943 1.5518 0.2011 0.2277     0.7242   0.6381 132.7118  0.5647    0.2515
## 5    2.3146 2.4309 0.2464 0.3668     0.7359   0.9685   7.4493  0.0324    0.2825
## 6    2.5948 2.6050 0.2426 0.3736     0.6658   0.2093  11.3367  2.8342    0.2623
## 7    2.6280 2.6288 0.2426 0.2823     0.6692 150.1282   0.0000  0.0000    0.2438
## 8    2.6418 2.6320 0.2426 0.2524     0.6728   0.9830   3.9514  0.0173    0.2286
## 9    2.8503 2.7855 0.2409 0.2531     0.6640   0.8802  30.9075  0.1356    0.2208
## 10   3.4100 3.1636 0.2527 0.2787     0.6589   0.6172 138.9573  0.6176    0.2350
## 11   6.7234 5.1123 0.2322 0.4183     0.6561   0.4562   8.3432  1.0429    0.2356
## 12   7.0386 5.2274 0.2321 0.3865     0.6450  16.1084  -0.9379 -0.4690    0.2303
## 13   7.0765 5.2451 0.2321 0.3594     0.6454  85.4573   0.0000  0.0000    0.2214
## 14   7.0823 5.2510 0.2321 0.3280     0.6519   0.9855   3.1535  0.0146    0.2134
## 15   7.4535 5.5680 0.2699 0.3240     0.6498   0.3039  11.4530  1.9088    0.2138
## 16   7.6980 5.6459 0.2698 0.2973     0.6388  14.8400  -0.9326 -0.4663    0.2092
## 17   7.7113 5.6584 0.2698 0.2719     0.6269  13.7872  -1.8549 -0.6183    0.2031
## 18   7.7496 5.6694 0.2698 0.2656     0.6297  23.2289  -0.9570 -0.4785    0.1975
## 19   7.7808 5.6810 0.2698 0.2549     0.6325  25.6870  -0.9611 -0.4805    0.1923
## 20   7.7988 5.6890 0.2698 0.2213     0.6273  78.7103   0.0000  0.0000    0.1875
## 21   7.8042 5.6921 0.2699 0.2031     0.6241   1.0069  -1.4752 -0.0069    0.1830
## 22   8.1177 5.9260 0.2987 0.2089     0.6100   0.9044  22.5142  0.1052    0.1822
## 23   9.1169 6.5515 0.2996 0.2229     0.5555   0.8341  41.5606  0.1979    0.1850
## 24  11.9508 7.8506 0.2908 0.2249     0.5570   1.2847  -0.4432 -0.1477    0.1832
## 25  11.9912 7.8640 0.2909 0.2316     0.5494 162.4400   0.0000  0.0000    0.1796
## 26  12.0051 7.8703 0.2909 0.2206     0.5542 566.9647   0.0000  0.0000    0.1761
## 27  12.0235 7.8741 0.2909 0.2110     0.5570   0.9565   9.3135  0.0452    0.1729
## 28  12.4989 8.2534 0.3188 0.2112     0.5605   1.8280  -0.4529 -0.2265    0.1703
## 29  12.5478 8.2651 0.3188 0.2022     0.5497  26.2760  -1.9239 -0.6413    0.1674
## 30  12.5548 8.2720 0.3188 0.2050     0.5442   0.9631   7.8098  0.0381    0.1646
##         Ball Ptbiserial      Frey McClain   Dunn Hubert SDindex Dindex   SDbw
## 2  1209.4057     0.4634   42.9060  0.0012 0.3730  7e-04  2.8306 2.2319 0.4329
## 3   714.0982     0.5245   -0.5211  0.0049 0.2058  7e-04  1.9036 2.1032 0.2894
## 4   469.3127     0.6105    6.0529  0.0063 0.2201  7e-04  1.5281 2.0092 0.2051
## 5   239.6720     0.7578    9.1267  0.0148 0.2710  6e-04  1.4469 1.7103 0.1559
## 6   186.3760     0.7635  195.2827  0.0169 0.2341  6e-04  1.5329 1.6607 0.1196
## 7   158.3014     0.7625 -246.1966  0.0169 0.1981  6e-04  1.5653 1.6433 0.0684
## 8   138.3479     0.7623    7.8480  0.0169 0.1933  6e-04  2.4444 1.6365 0.0813
## 9   116.1996     0.7683    6.8330  0.0190 0.1841  6e-04  2.4169 1.5935 0.0671
## 10   92.0791     0.7817    5.0232  0.0251 0.1892  6e-04  2.3277 1.4978 0.0644
## 11   51.8011     0.7999   25.0764  0.0435 0.1918  6e-04  2.3932 1.2436 0.0625
## 12   46.4386     0.7990 -226.7415  0.0437 0.1912  6e-04  2.3568 1.2246 0.0336
## 13   42.7220     0.7988  -67.4430  0.0437 0.1733  6e-04  2.3814 1.2186 0.0274
## 14   39.6257     0.7986    2.6097  0.0437 0.1644  6e-04  3.4833 1.2139 0.0240
## 15   34.8781     0.8037   67.8652  0.0454 0.1907  6e-04  3.3151 1.1867 0.0220
## 16   32.2475     0.8028 -108.1028  0.0456 0.1903  6e-04  3.3498 1.1701 0.0178
## 17   30.2835     0.8026  -74.2588  0.0457 0.1623  6e-04  3.4333 1.1654 0.0156
## 18   28.5456     0.8022  -68.6613  0.0457 0.1464  6e-04  3.5397 1.1620 0.0208
## 19   26.9879     0.8020  -42.6971  0.0458 0.1432  6e-04  3.5177 1.1580 0.0181
## 20   25.6021     0.8018  -36.6149  0.0458 0.1397  6e-04  4.4548 1.1525 0.0158
## 21   24.3697     0.8017    4.5555  0.0458 0.1233  6e-04  5.3981 1.1494 0.0143
## 22   22.3440     0.8008    6.9552  0.0480 0.1273  6e-04  5.3255 1.1275 0.0089
## 23   19.3321     0.7797    4.5730  0.0583 0.1337  6e-04  5.2722 1.0659 0.0085
## 24   15.4607     0.7684  -31.3861  0.0679 0.1359  6e-04  5.3168 0.9836 0.0080
## 25   14.8171     0.7682  -24.3112  0.0679 0.1350  6e-04  5.3379 0.9809 0.0073
## 26   14.2358     0.7681  -23.7413  0.0680 0.1304  6e-04  5.4003 0.9777 0.0065
## 27   13.7018     0.7680    4.1002  0.0680 0.1261  6e-04  6.5367 0.9752 0.0060
## 28   12.6054     0.7654  -28.8377  0.0704 0.1388  6e-04  6.6103 0.9541 0.0056
## 29   12.1535     0.7652  -23.6452  0.0704 0.1362  6e-04  6.6785 0.9506 0.0050
## 30   11.7386     0.7650    4.4478  0.0705 0.1249  6e-04  6.6747 0.9479 0.0046
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.5221           216.9679       0.8794
## 3          0.5214           215.7172       0.9467
## 4          0.5211           215.0914       0.5689
## 5          0.5193           211.9589       0.9681
## 6         -0.4219           -10.1102       0.1360
## 7         -1.0633             0.0000          NaN
## 8          0.5190           211.3316       0.9829
## 9          0.5186           210.7040       0.8733
## 10         0.5175           208.8197       0.5397
## 11        -0.1409           -56.6808       0.3783
## 12        -0.7431            -2.3458       1.0000
## 13        -1.0633             0.0000          NaN
## 14         0.5142           203.1521       0.9855
## 15        -0.2510           -24.9172       0.1985
## 16        -0.7431            -2.3458       1.0000
## 17        -0.5522            -5.6219       1.0000
## 18        -0.7431            -2.3458       1.0000
## 19        -0.7431            -2.3458       1.0000
## 20        -1.0633             0.0000          NaN
## 21         0.5138           202.5209       1.0000
## 22         0.5134           201.8895       0.9002
## 23         0.5118           199.3611       0.8205
## 24        -0.5522            -5.6219       1.0000
## 25        -1.0633             0.0000          NaN
## 26        -1.0633             0.0000          NaN
## 27         0.5102           196.8282       0.9558
## 28        -0.7431            -2.3458       1.0000
## 29        -0.5522            -5.6219       1.0000
## 30         0.5098           196.1942       0.9626
## 
## $Best.nc
##                      KL      CH Hartigan      CCC    Scott Marriot   TrCovW
## Number_clusters 21.0000 11.0000  11.0000   2.0000   5.0000      11      5.0
## Value_Index      7.5035 94.1712 136.5132 -16.6876 180.4541 4472610 768709.4
##                   TraceW Friedman   Rubin Cindex     DB Silhouette   Duda
## Number_clusters   5.0000  11.0000 11.0000 3.0000 2.0000     2.0000 2.0000
## Value_Index     598.7869   3.3133 -1.8335 0.1711 0.1428     0.8402 0.8857
##                 PseudoT2  Beale Ratkowsky     Ball PtBiserial   Frey McClain
## Number_clusters   2.0000 2.0000    5.0000   3.0000    15.0000  2.000  2.0000
## Value_Index      30.5908 0.1285    0.2825 495.3075     0.8037 42.906  0.0012
##                  Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 2.000      0  5.0000      0 30.0000
## Value_Index     0.373      0  1.4469      0  0.0046
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      1      1      1      1      1      1      1      1      1      1      1 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      1      1      1      1      1      1      1      1      1      1      1 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      1      1      1      1      1      1      1      1      1      1      1 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      1      1      1      1      1      1      1      1      1      1      1 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      1      1      1      2      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      1      1      1      1      1      1      1      1      1      1      1 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      1      1      1      1      1      1      1      1      1
testH.nbclust_complete_m <- NbClust(data_aseg_pca$x[,1:2], distance="manhattan", min.nc=2, max.nc=30, method="complete", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 6 proposed 2 as the best number of clusters 
## * 8 proposed 3 as the best number of clusters 
## * 1 proposed 5 as the best number of clusters 
## * 1 proposed 6 as the best number of clusters 
## * 1 proposed 8 as the best number of clusters 
## * 2 proposed 9 as the best number of clusters 
## * 3 proposed 18 as the best number of clusters 
## * 1 proposed 26 as the best number of clusters 
## * 1 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  3 
##  
##  
## *******************************************************************
testH.nbclust_complete_m
## $All.index
##         KL       CH Hartigan      CCC     Scott   Marriot       TrCovW
## 2   1.0429  48.6294 339.1812 -16.6876   68.7971 2080310.0 2630470.3313
## 3   3.1869 227.5966  27.7615  -8.4444  358.0676 1402355.7  501929.3637
## 4   0.9737 178.0040 117.9434 -14.6297  453.7646 1673270.4  476450.8236
## 5   2.0664 228.7350  22.1363 -10.9916  627.1591 1269458.1  163880.2810
## 6   0.7412 203.7813   9.9775 -12.4803  677.4846 1482223.8  127640.7561
## 7   0.2968 177.9578 181.9329 -14.3350  689.5667 1918421.9  115771.0401
## 8  46.7755 296.3520  25.9219  -6.8475  898.4002 1049617.7   40620.4474
## 9   0.0540 290.2648  97.4192  -7.1327  945.1801 1093163.6   34264.5569
## 10 21.7103 376.0141  19.7836  -3.1617 1069.6227  803551.7   12992.1960
## 11  0.2755 367.8934   9.7999  -3.5057 1102.9188  846345.7   10323.9131
## 12  9.7117 348.1251  17.8833  -4.3765 1125.8418  915470.5   10031.4481
## 13  0.1152 344.1190   8.2288  -4.5836 1172.9567  882899.6    9619.1495
## 14  1.2072 328.3433   8.7969  -5.3415 1197.0427  926180.6    9417.8115
## 15  0.7788 315.9818   6.0013  -5.9730 1222.8750  954722.3    9068.0616
## 16  0.5289 301.8351  32.1583  -6.7223 1235.7640 1029463.6    8502.6126
## 17  0.2130 324.1510 144.9046  -5.6792 1354.1678  709594.3    7917.7694
## 18 21.4356 509.5539   6.3866   1.2728 1522.2114  394977.5    2028.2005
## 19  3.9160 493.2131  10.1337   0.7071 1540.6600  407521.5    2027.5124
## 20  0.3447 486.9997   6.7641   0.4499 1560.2985  416069.9    1865.0929
## 21  1.0381 475.0434   6.4462   0.0017 1573.2168  434678.7    1705.0803
## 22  0.2193 463.9181  29.4971  -0.4298 1586.5012  451373.8    1624.3021
## 23  4.4917 501.7780  13.8520   0.7245 1671.7599  345832.5    1406.7350
## 24  1.2592 508.8460   5.5674   0.8756 1701.8645  332167.1    1294.9003
## 25  0.4213 498.1282  17.0022   0.4771 1722.6175  330567.6    1295.4788
## 26  2.4119 514.2962   4.8215   0.9047 1753.3028  314630.0    1107.7929
## 27  2.3840 503.4789   6.4766   0.5047 1767.6183  319651.0    1089.9952
## 28  0.5185 497.4669   4.8968   0.2476 1779.6299  326985.9    1005.5009
## 29  0.5049 488.6395  12.6724  -0.1017 1788.0275  338698.2     942.5363
## 30  0.0622 498.1895 149.9712   0.1265 1824.3425  311562.6     896.5542
##       TraceW Friedman   Rubin Cindex     DB Silhouette     Duda Pseudot2
## 2  2418.8114   0.2986  1.2043 0.1799 0.1428     0.8402   0.4123 337.7560
## 3   997.3941   3.0192  2.9206 0.1874 0.4695     0.7355   0.5118  13.3558
## 4   892.8128   4.3767  3.2628 0.2187 0.6847     0.6847   0.6201 135.3768
## 5   595.3037   6.2569  4.8934 0.1999 0.7630     0.6428   0.6434   4.4336
## 6   544.0553   6.8928  5.3543 0.2948 0.6876     0.6405   0.3034   9.1842
## 7   521.8060   7.1978  5.5826 0.3003 0.6137     0.6390   0.4842 222.5968
## 8   293.0132  16.0955  9.9417 0.2712 0.6612     0.5686   0.3000  23.3370
## 9   263.5645  19.3898 11.0525 0.2921 0.6092     0.5722   0.5900 129.9550
## 10  185.3831  28.1535 15.7136 0.2295 0.7140     0.4870   0.1863   8.7348
## 11  170.7002  31.5790 17.0652 0.2519 0.6962     0.4900  12.4791  -0.9199
## 12  163.6950  34.3513 17.7955 0.2883 0.6092     0.4951   0.5067  19.4684
## 13  151.7893  36.7001 19.1913 0.3062 0.6430     0.4906   0.3673   6.8894
## 14  146.4794  37.9868 19.8870 0.3131 0.6310     0.4890   0.2746  10.5660
## 15  140.9914  39.1253 20.6611 0.3241 0.6296     0.4904   4.9738  -3.1958
## 16  137.3285  39.3862 21.2122 0.3355 0.6035     0.4915   0.3006  55.8339
## 17  120.0882  47.8714 24.2575 0.3569 0.5679     0.4625   0.4616 187.7929
## 18   72.7897  72.7681 40.0199 0.2537 0.6293     0.4591  41.7462   0.0000
## 19   70.7542  76.1858 41.1712 0.2616 0.5798     0.4669   0.5612   8.5992
## 20   67.6521  82.0677 43.0591 0.2731 0.5749     0.4632   0.2698   5.4126
## 21   65.6341  82.8726 44.3830 0.2967 0.5635     0.4648  16.1084  -0.9379
## 22   63.7574  87.1728 45.6894 0.3044 0.5343     0.4651   0.5027  38.5849
## 23   56.1587  94.4369 51.8715 0.2972 0.5584     0.4307   0.3470  13.1726
## 24   52.7890 105.5190 55.1827 0.3003 0.5575     0.4325 105.5903   0.0000
## 25   51.4625 111.2365 56.6050 0.3309 0.5320     0.4365   0.3636  26.2499
## 26   47.6911 124.1455 61.0813 0.3414 0.5418     0.4338  23.2289  -0.9570
## 27   46.6403 130.6979 62.4575 0.3518 0.5276     0.4365   0.5938   6.1559
## 28   45.2640 131.5491 64.3566 0.3510 0.5585     0.4332   0.2650   8.3195
## 29   44.2420 132.5159 65.8432 0.3779 0.5606     0.4346   0.4334  26.1470
## 30   41.7355 151.3932 69.7976 0.3875 0.5644     0.4273   0.3119 264.6791
##      Beale Ratkowsky      Ball Ptbiserial    Frey McClain   Dunn Hubert SDindex
## 2   1.4191    0.2432 1209.4057     0.4634 12.1881  0.0012 0.3730  7e-04  2.7265
## 3   0.8904    0.3267  332.4647     0.7609  6.4633  0.0317 0.0859  6e-04  2.8044
## 4   0.6098    0.3194  223.2032     0.7609  3.8653  0.0323 0.1007  6e-04  2.4468
## 5   0.4926    0.3704  119.0607     0.7844  2.9835  0.0567 0.0888  6e-04  2.3370
## 6   1.8368    0.3489   90.6759     0.7846  8.4291  0.0569 0.1312  6e-04  2.0682
## 7   1.0600    0.3240   74.5437     0.7843  4.7533  0.0570 0.1337  6e-04  1.7940
## 8   2.1215    0.3114   36.6266     0.7005  2.3491  0.1131 0.1096  6e-04  1.7820
## 9   0.6912    0.2946   29.2849     0.7002  3.8561  0.1135 0.1185  6e-04  1.6952
## 10  2.9116    0.2861   18.5383     0.6011  0.3800  0.1911 0.0516  6e-04  2.3432
## 11 -0.4599    0.2738   15.5182     0.6012  0.7213  0.1910 0.0567  6e-04  2.1785
## 12  0.9271    0.2625   13.6412     0.6012  3.3828  0.1910 0.0649  6e-04  1.9684
## 13  1.3779    0.2556   11.6761     0.5984  2.4950  0.1935 0.0695  6e-04  2.4060
## 14  2.1132    0.2478   10.4628     0.5983  2.3584  0.1936 0.0711  6e-04  2.4450
## 15 -0.6392    0.2409    9.3994     0.5981  2.0042  0.1938 0.0737  6e-04  2.4791
## 16  2.2334    0.2341    8.5830     0.5980  3.2133  0.1939 0.0763  6e-04  2.4541
## 17  1.1592    0.2313    7.0640     0.5944  2.8819  0.1971 0.0821  6e-04  2.4453
## 18  0.0000    0.2277    4.0439     0.4672  0.4705  0.3280 0.0507  6e-04  3.0565
## 19  0.7166    0.2219    3.7239     0.4672  1.1765  0.3279 0.0524  6e-04  3.0807
## 20  1.8042    0.2163    3.3826     0.4668  1.0891  0.3280 0.0549  6e-04  3.1691
## 21 -0.4690    0.2115    3.1254     0.4667  1.0061  0.3279 0.0597  6e-04  3.2486
## 22  0.9646    0.2066    2.8981     0.4667  3.0524  0.3279 0.0612  6e-04  3.1897
## 23  1.6466    0.2037    2.4417     0.4542  1.4347  0.3444 0.0621  6e-04  4.3341
## 24  0.0000    0.1995    2.1995     0.4538  0.6873  0.3446 0.0629  6e-04  4.4119
## 25  1.6406    0.1956    2.0585     0.4538  2.0176  0.3446 0.0694  6e-04  4.3169
## 26 -0.4785    0.1919    1.8343     0.4524  1.5285  0.3460 0.0721  6e-04  5.3605
## 27  0.6156    0.1883    1.7274     0.4524  3.4549  0.3460 0.0743  6e-04  5.4041
## 28  2.0799    0.1851    1.6166     0.4515  2.3633  0.3473 0.0743  6e-04  5.5066
## 29  1.2451    0.1820    1.5256     0.4513  6.1030  0.3475 0.0800  6e-04  5.5671
## 30  2.1874    0.1790    1.3912     0.4473  1.6010  0.3543 0.0825  6e-04  6.4458
##    Dindex   SDbw
## 2  2.2319 0.4329
## 3  1.5958 0.7475
## 4  1.5212 0.3123
## 5  1.2984 0.2867
## 6  1.2570 0.2298
## 7  1.2358 0.1695
## 8  0.9574 0.1449
## 9  0.9134 0.1163
## 10 0.7724 0.1531
## 11 0.7527 0.1317
## 12 0.7370 0.0994
## 13 0.7106 0.0633
## 14 0.6986 0.0442
## 15 0.6839 0.0379
## 16 0.6739 0.0331
## 17 0.6280 0.0369
## 18 0.4879 0.0355
## 19 0.4795 0.0289
## 20 0.4722 0.0275
## 21 0.4666 0.0202
## 22 0.4606 0.0171
## 23 0.4314 0.0232
## 24 0.4207 0.0247
## 25 0.4139 0.0216
## 26 0.3987 0.0169
## 27 0.3947 0.0151
## 28 0.3889 0.0146
## 29 0.3842 0.0137
## 30 0.3737 0.0119
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.5221           216.9679       0.2429
## 3          0.0647           202.2234       0.4218
## 4          0.5164           206.9329       0.5439
## 5         -0.0987           -89.0644       0.6200
## 6         -0.3258           -16.2785       0.2206
## 7          0.5118           199.3611       0.3474
## 8         -0.0307          -335.8023       0.1460
## 9          0.5022           185.3717       0.5016
## 10        -0.5522            -5.6219       0.1658
## 11        -0.7431            -2.3458       1.0000
## 12         0.1556           108.5678       0.4040
## 13        -0.3258           -16.2785       0.3061
## 14        -0.3258           -16.2785       0.1833
## 15        -0.3258           -16.2785       1.0000
## 16         0.1977            97.3833       0.1182
## 17         0.4884           168.6482       0.3150
## 18        -1.0633             0.0000          NaN
## 19        -0.0027         -4016.9908       0.4995
## 20        -0.5522            -5.6219       0.2764
## 21        -0.7431            -2.3458       1.0000
## 22         0.2963            92.6281       0.3856
## 23        -0.1409           -56.6808       0.2279
## 24        -1.0633             0.0000          NaN
## 25         0.0832           165.3574       0.2108
## 26        -0.7431            -2.3458       1.0000
## 27        -0.0624          -153.2995       0.5513
## 28        -0.4219           -10.1102       0.2060
## 29         0.1556           108.5678       0.2988
## 30         0.4583           141.8551       0.1144
## 
## $Best.nc
##                      KL       CH Hartigan     CCC    Scott  Marriot  TrCovW
## Number_clusters  8.0000  26.0000   3.0000 18.0000   3.0000      3.0       3
## Value_Index     46.7755 514.2962 311.4197  1.2728 289.2705 948869.1 2128541
##                   TraceW Friedman    Rubin Cindex     DB Silhouette   Duda
## Number_clusters    3.000  18.0000  18.0000 2.0000 2.0000     2.0000 3.0000
## Value_Index     1316.836  24.8967 -14.6111 0.1799 0.1428     0.8402 0.5118
##                 PseudoT2  Beale Ratkowsky    Ball PtBiserial   Frey McClain
## Number_clusters   3.0000 2.0000    5.0000   3.000     6.0000 9.0000  2.0000
## Value_Index      13.3558 1.4191    0.3704 876.941     0.7846 3.8561  0.0012
##                  Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 2.000      0  9.0000      0 30.0000
## Value_Index     0.373      0  1.6952      0  0.0119
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      1      2      2      2      2      2      2      2      2      2 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      2      2      2      3      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      1      1      1      1      1      1      1      2      2      2      2 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      2      2      2      2      2      2      2      2      2      2      2 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      2      2      2      2      2      2      2      2      2
testH.nbclust_Average_m <- NbClust(data_aseg_pca$x[,1:2], distance="manhattan", min.nc=2, max.nc=30, method="average", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 5 proposed 2 as the best number of clusters 
## * 3 proposed 3 as the best number of clusters 
## * 3 proposed 4 as the best number of clusters 
## * 2 proposed 5 as the best number of clusters 
## * 1 proposed 7 as the best number of clusters 
## * 1 proposed 9 as the best number of clusters 
## * 2 proposed 19 as the best number of clusters 
## * 4 proposed 20 as the best number of clusters 
## * 2 proposed 29 as the best number of clusters 
## * 1 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_Average_m
## $All.index
##         KL       CH Hartigan      CCC     Scott   Marriot      TrCovW    TraceW
## 2   0.1968 274.4417  19.0073  -6.6822  239.8246 1020102.0 849235.5138 1352.9393
## 3   0.9469 157.0207 211.7353 -11.6452  283.8989 1910165.5 786561.2153 1252.8812
## 4   2.2941 267.6462  13.3443  -9.0833  485.3988 1466635.5 144564.9250  661.7104
## 5   1.7325 214.5081  23.8748 -11.8950  532.8083 1880839.8 140147.2487  626.2974
## 6   0.6434 192.9911   8.1381 -13.2438  627.3663 1826442.1 141724.4623  568.5369
## 7   1.0245 167.0585   6.8216 -15.2139  640.2403 2356152.0 133986.5879  549.4288
## 8   0.2270 147.7230 232.1784 -16.9322  657.0259 2869542.4 130892.0093  533.8006
## 9   9.8157 286.3972   6.8068  -7.3356  895.9090 1342283.2  26472.7982  266.7977
## 10  1.2906 261.6953   9.3717  -8.6922  907.0658 1581868.1  24161.6499  259.1611
## 11  1.1310 244.9899  33.3116  -9.6981  923.1039 1790332.5  21727.6912  249.0147
## 12  0.9830 257.0170   7.0423  -9.0036 1016.5733 1443352.3  21077.8516  217.3917
## 13  0.9720 242.3949   5.8635  -9.9215 1041.2497 1528421.4  20916.9583  210.8782
## 14  0.8810 228.9666   3.7693 -10.8219 1063.0482 1618701.6  20693.8588  205.5683
## 15  0.5141 215.4695  41.4896 -11.7837 1069.2011 1811170.0  19967.0830  202.1960
## 16  2.0347 239.8833   1.7541 -10.2194 1177.5591 1312007.4  17035.6513  170.7162
## 17  2.1784 225.7489  18.8297 -11.1923 1185.8329 1430942.5  17036.5948  169.3898
## 18  0.5643 230.4795   2.6997 -10.9371 1285.4184 1059408.5  16746.0797  156.2005
## 19  0.0800 219.4791 257.5850 -11.7374 1294.1870 1138041.9  16485.1397  154.3238
## 20 52.1676 461.7343   6.4114  -0.3784 1542.3298  448416.7   2286.7795   71.2633
## 21  0.9702 449.6981   5.8256  -0.8500 1553.0799  472723.7   2104.4898   69.2454
## 22  1.4642 437.9451  13.0753  -1.3240 1563.6670  496428.0   1962.1127   67.4511
## 23  1.2276 441.6707  12.0265  -1.2571 1594.4692  477230.4   1778.5352   63.6344
## 24  0.4868 444.3473   3.8314  -1.2293 1616.0238  474996.7   1544.0502   60.2929
## 25  0.7920 431.5387   2.2974  -1.7498 1627.3460  491654.4   1527.9590   59.2420
## 26  1.1049 416.8481   2.7027  -2.3544 1631.2794  523129.0   1479.7228   58.6157
## 27  0.9576 404.0843   2.2963  -2.9048 1636.4714  552070.3   1433.8924   57.8847
## 28  0.0921 391.5512  90.8560  -3.4621 1643.7528  575979.2   1431.5687   57.2673
## 29 28.8059 540.0650   4.4390   1.4523 1793.2992  331339.7    460.4302   40.0872
## 30  0.9169 530.0413   3.9670   1.0888 1805.0215  337681.9    448.8534   39.2613
##    Friedman   Rubin Cindex     DB Silhouette     Duda Pseudot2   Beale
## 2    1.5632  2.1531 0.1576 0.4914     0.7866   1.3159  -1.6803 -0.2100
## 3    1.9460  2.3251 0.2466 0.3740     0.7676   0.5008 228.2815  0.9925
## 4    5.0874  4.4023 0.1944 0.6207     0.6861   0.4844   6.3872  0.9125
## 5    5.3994  4.6512 0.2044 0.5589     0.6838   0.4343  18.2324  1.2155
## 6    6.4446  5.1237 0.2991 0.6272     0.6644   3.2042  -2.7516 -0.5503
## 7    6.7795  5.3019 0.2990 0.5493     0.6667   0.4482   8.6177  1.0772
## 8    7.1666  5.4572 0.2988 0.5406     0.6252   0.4561 253.9865  1.1869
## 9   18.3181 10.9185 0.2409 0.5962     0.5788   3.1934  -3.4343 -0.5724
## 10  18.9063 11.2402 0.2407 0.5449     0.5778   0.2093  11.3367  2.8342
## 11  20.0795 11.6983 0.2406 0.4892     0.5813   0.4774  30.6508  1.0569
## 12  23.4624 13.3999 0.2938 0.5228     0.5539   0.4556   5.9757  0.9960
## 13  24.5174 13.8138 0.3284 0.5627     0.5565   0.3673   6.8894  1.3779
## 14  25.4271 14.1706 0.3283 0.5697     0.5572  10.4573  -1.8087 -0.6029
## 15  26.0090 14.4070 0.3283 0.4978     0.5628   0.8074  43.6672  0.2373
## 16  39.4014 17.0636 0.3044 0.5167     0.4280 150.1282   0.0000  0.0000
## 17  40.4081 17.1972 0.3044 0.4874     0.4320   0.4126  25.6227  1.3486
## 18  52.8533 18.6493 0.3031 0.5123     0.4394  16.1084  -0.9379 -0.4690
## 19  54.7352 18.8761 0.3031 0.4805     0.4398   0.3632 305.0321  1.7430
## 20  90.0544 40.8771 0.2071 0.5028     0.5209   0.2698   5.4126  1.8042
## 21  90.7475 42.0683 0.2069 0.4907     0.5216   2.0677  -5.1638 -0.4694
## 22  93.7773 43.1874 0.2064 0.4775     0.5173   0.4067  11.6687  1.2965
## 23 105.7526 45.7777 0.2055 0.4832     0.5192   0.1815  27.0508  3.8644
## 24 106.5701 48.3148 0.2050 0.4802     0.5072  23.2289  -0.9570 -0.4785
## 25 111.1454 49.1718 0.2049 0.4702     0.5085  85.4573   0.0000  0.0000
## 26 111.9794 49.6972 0.2049 0.4377     0.5152  52.0690   0.0000  0.0000
## 27 112.1719 50.3248 0.2048 0.4130     0.5207  71.2689   0.0000  0.0000
## 28 114.0355 50.8674 0.2048 0.3798     0.5260   0.4980  69.5640  0.9938
## 29 131.1427 72.6674 0.2678 0.4059     0.5095   3.8916  -1.4861 -0.4954
## 30 132.0288 74.1962 0.2677 0.3953     0.5112  25.6870  -0.9611 -0.4805
##    Ratkowsky     Ball Ptbiserial    Frey McClain   Dunn Hubert SDindex Dindex
## 2     0.3478 676.4696     0.7583  2.8041  0.0146 0.1731  6e-04  3.4366 1.8034
## 3     0.3132 417.6271     0.7593  5.8314  0.0146 0.2710  6e-04  1.4350 1.7616
## 4     0.3482 165.4276     0.8070  3.9233  0.0461 0.0823  6e-04  1.5008 1.3562
## 5     0.3388 125.2595     0.8070  7.2888  0.0462 0.0866  6e-04  1.3975 1.3244
## 6     0.3364  94.7561     0.8055  7.2553  0.0469 0.1273  6e-04  1.4093 1.2775
## 7     0.3123  78.4898     0.8053 12.7777  0.0470 0.1273  6e-04  1.1689 1.2580
## 8     0.2937  66.7251     0.8047  4.8852  0.0471 0.1273  6e-04  1.2207 1.2381
## 9     0.2876  29.6442     0.6928  2.0771  0.1203 0.0909  6e-04  1.2186 0.9129
## 10    0.2737  25.9161     0.6928  3.0799  0.1203 0.0909  6e-04  1.2886 0.9002
## 11    0.2615  22.6377     0.6927  4.4030  0.1204 0.0909  6e-04  1.2606 0.8829
## 12    0.2596  18.1160     0.6878  4.8527  0.1237 0.1125  6e-04  1.6044 0.8364
## 13    0.2512  16.2214     0.6875  6.8980  0.1239 0.1258  6e-04  1.8144 0.8209
## 14    0.2435  14.6835     0.6872  7.5328  0.1241 0.1258  6e-04  1.8614 0.8089
## 15    0.2354  13.4797     0.6871  6.0459  0.1241 0.1258  6e-04  1.9258 0.8011
## 16    0.2291  10.6698     0.6396  5.8046  0.1573 0.1212  6e-04  1.9663 0.7352
## 17    0.2225   9.9641     0.6396  6.0591  0.1573 0.1212  6e-04  2.1986 0.7284
## 18    0.2191   8.6778     0.6367  8.0527  0.1596 0.1212  6e-04  2.3624 0.7009
## 19    0.2133   8.1223     0.6366  2.9206  0.1596 0.1212  6e-04  2.3509 0.6948
## 20    0.2150   3.5632     0.4551  0.9904  0.3459 0.0426  6e-04  2.5285 0.4630
## 21    0.2101   3.2974     0.4550  1.1313  0.3458 0.0426  6e-04  2.5997 0.4574
## 22    0.2054   3.0660     0.4548  1.2866  0.3458 0.0426  6e-04  2.6537 0.4517
## 23    0.2009   2.7667     0.4543  1.3633  0.3460 0.0426  6e-04  2.9843 0.4396
## 24    0.1973   2.5122     0.4540  1.5775  0.3462 0.0426  6e-04  2.9995 0.4261
## 25    0.1934   2.3697     0.4539  1.5834  0.3462 0.0426  6e-04  3.0387 0.4222
## 26    0.1897   2.2544     0.4539  1.6770  0.3462 0.0426  6e-04  3.3926 0.4175
## 27    0.1863   2.1439     0.4539  1.6847  0.3462 0.0426  6e-04  3.3972 0.4124
## 28    0.1830   2.0453     0.4538  1.4984  0.3463 0.0426  6e-04  3.4967 0.4078
## 29    0.1821   1.3823     0.4242  1.8024  0.3615 0.0691  6e-04  3.8374 0.3504
## 30    0.1792   1.3087     0.4241  1.9087  0.3615 0.0691  6e-04  3.9050 0.3469
##      SDbw
## 2  0.8774
## 3  0.6590
## 4  0.3541
## 5  0.2266
## 6  0.1755
## 7  0.1121
## 8  0.0887
## 9  0.0857
## 10 0.0730
## 11 0.0512
## 12 0.0734
## 13 0.0666
## 14 0.0587
## 15 0.0441
## 16 0.0342
## 17 0.0293
## 18 0.0305
## 19 0.0298
## 20 0.0231
## 21 0.0209
## 22 0.0149
## 23 0.0132
## 24 0.0117
## 25 0.0102
## 26 0.0091
## 27 0.0076
## 28 0.0066
## 29 0.0063
## 30 0.0056
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2         -0.1409           -56.6808       1.0000
## 3          0.5193           211.9589       0.3714
## 4         -0.1908           -37.4469       0.4277
## 5          0.0647           202.2234       0.3117
## 6         -0.3258           -16.2785       1.0000
## 7         -0.1409           -56.6808       0.3672
## 8          0.5134           201.8895       0.3062
## 9         -0.2510           -24.9172       1.0000
## 10        -0.4219           -10.1102       0.1360
## 11         0.2311            93.1391       0.3544
## 12        -0.2510           -24.9172       0.4032
## 13        -0.3258           -16.2785       0.3061
## 14        -0.5522            -5.6219       1.0000
## 15         0.5003           182.8126       0.7889
## 16        -1.0633             0.0000          NaN
## 17         0.1299           120.5893       0.2724
## 18        -0.7431            -2.3458       1.0000
## 19         0.4957           177.0365       0.1765
## 20        -0.5522            -5.6219       0.2764
## 21        -0.0307          -335.8023       1.0000
## 22        -0.0987           -89.0644       0.3007
## 23        -0.1908           -37.4469       0.0506
## 24        -0.7431            -2.3458       1.0000
## 25        -1.0633             0.0000          NaN
## 26        -1.0633             0.0000          NaN
## 27        -1.0633             0.0000          NaN
## 28         0.3888           108.4601       0.3728
## 29        -0.5522            -5.6219       1.0000
## 30        -0.7431            -2.3458       1.0000
## 
## $Best.nc
##                      KL      CH Hartigan     CCC    Scott Marriot   TrCovW
## Number_clusters 20.0000  29.000  19.0000 29.0000  20.0000       9      4.0
## Value_Index     52.1676 540.065 254.8853  1.4523 248.1427 1766844 641996.3
##                   TraceW Friedman    Rubin Cindex    DB Silhouette   Duda
## Number_clusters   4.0000  20.0000  20.0000 2.0000 3.000     2.0000 2.0000
## Value_Index     555.7577  35.3193 -20.8097 0.1576 0.374     0.7866 1.3159
##                 PseudoT2 Beale Ratkowsky     Ball PtBiserial    Frey McClain
## Number_clusters   5.0000  2.00    4.0000   3.0000      5.000 19.0000  2.0000
## Value_Index      18.2324 -0.21    0.3482 258.8426      0.807  2.9206  0.0146
##                  Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 3.000      0  7.0000      0 30.0000
## Value_Index     0.271      0  1.1689      0  0.0056
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      2      2      2      2      2      2      2      2      2      2 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      2      2      2      1      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      2      2      2      2      2      2      2      2      2      2      2 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      2      2      2      2      2      2      2      2      2
#testH.nbclust<-NbClust(data_aseg_pca$x[,1:2], distance="manhattan", min.nc=2, max.nc=30, method="ward.D", index="all")
testH.nbclust_ward_m <- NbClust(data_aseg_pca$x[,1:2], distance="euclidean", min.nc=2, max.nc=30, method="ward.D2", index="all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 5 proposed 2 as the best number of clusters 
## * 5 proposed 3 as the best number of clusters 
## * 5 proposed 4 as the best number of clusters 
## * 1 proposed 6 as the best number of clusters 
## * 1 proposed 9 as the best number of clusters 
## * 1 proposed 11 as the best number of clusters 
## * 1 proposed 12 as the best number of clusters 
## * 1 proposed 27 as the best number of clusters 
## * 1 proposed 29 as the best number of clusters 
## * 3 proposed 30 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
testH.nbclust_ward_m
## $All.index
##         KL       CH Hartigan     CCC     Scott    Marriot      TrCovW    TraceW
## 2   5.1126 394.2464  97.4622 -3.0642  281.8735  856157.21 325460.6253 1096.5701
## 3   0.3975 325.2050 135.5389 -4.9574  411.8507 1120816.12 206645.2458  777.9825
## 4  66.1171 384.3429  59.8005 -3.7062  593.7157  933934.17  45772.8915  494.9332
## 5   0.0308 374.6551  74.2538 -3.6856  690.8346  973629.52  36645.6607  394.8751
## 6   3.2412 407.5380  47.3904 -2.1576  849.2645  724548.86  21913.9141  300.0631
## 7   1.5894 414.5143  38.5365 -1.7654  919.7744  735138.78  15445.8636  249.5279
## 8   0.3357 417.7662  45.2860 -1.5731  985.3506  730616.20  11905.9794  214.1149
## 9   1.4389 440.6524  37.3019 -0.7072 1074.2321  638495.47   9077.9256  179.1459
## 10  0.5906 457.0987  44.0324 -0.1256 1144.6973  587708.28   5053.4029  154.2393
## 11  0.8607 492.4003  47.5260  1.0298 1247.7802  462820.26   3721.6416  129.4556
## 12  1.9453 542.4792  33.3643  2.5266 1327.1991  395618.19   2066.6447  107.2063
## 13  1.1097 570.3088  30.9811  3.2791 1384.3966  365845.90   1808.5203   93.5210
## 14  1.8100 598.0246  23.7744  3.9835 1448.3096  325097.10   1701.8688   82.2900
## 15  0.9355 612.7001  23.3674  4.3171 1507.6806  291409.72   1567.4002   74.4573
## 16  0.8209 629.9888  24.5564  4.7022 1551.0297  276769.79   1421.9014   67.4521
## 17  0.8780 653.9637  25.6803  5.2328 1597.5660  257375.02   1232.1655   60.7881
## 18  0.8115 684.8006  28.7757  5.8969 1647.6910  234158.20    842.6328   54.5107
## 19  1.3488 728.8892  24.3975  6.8141 1704.5326  205879.59    523.6842   48.2558
## 20  1.4254 764.5668  20.2494  7.4995 1750.4778  188375.94    465.3844   43.4582
## 21  0.9830 790.5950  20.1705  7.9582 1796.8272  171211.11    341.2341   39.7953
## 22  1.2739 819.4976  17.7679  8.4530 1843.8563  154467.37    327.9413   36.4392
## 23  1.3602 842.9272  15.3244  8.8253 1878.8799  145904.84    313.8831   33.6931
## 24  1.0118 859.9023  14.9367  9.0668 1909.0507  140100.83    244.7565   31.4706
## 25  0.9004 877.5994  15.3502  9.3140 1942.1836  132416.55    244.5250   29.4351
## 26  0.9296 899.0594  15.6898  9.6194 1973.1230  125898.95    221.7340   27.4736
## 27  1.2121 924.1260  14.1798  9.9760 2005.9531  118411.81    194.8258   25.5969
## 28  1.3228 945.2078  12.4028 10.2543 2035.8910  112411.08    156.3931   23.9993
## 29  0.9453 960.6637  12.4780 10.4327 2065.5055  106585.90    156.3964   22.6728
## 30  0.9179 978.1617  12.7843 10.6389 2097.7616   99718.77    156.3033   21.4069
##    Friedman    Rubin Cindex     DB Silhouette     Duda Pseudot2   Beale
## 2    2.2068   2.6565 0.0911 0.6943     0.7611   0.4248  28.4357  1.2925
## 3    4.0161   3.7443 0.1311 0.7588     0.6882   0.4784 234.3723  1.0851
## 4    8.7187   5.8857 0.0768 0.8854     0.4925   1.3159  -1.6803 -0.2100
## 5   12.8001   7.3771 0.1310 0.7391     0.4970   0.5753  61.2619  0.7293
## 6   25.3827   9.7081 0.0975 0.8880     0.4739   0.3661  45.0129  1.6671
## 7   25.9801  11.6742 0.0934 0.7500     0.4910   0.4844   6.3872  0.9125
## 8   26.6738  13.6050 0.1062 0.7118     0.4933   0.4745  13.2905  1.0223
## 9   27.8481  16.2607 0.1044 0.7138     0.4964   0.3155 282.0429  2.1530
## 10  33.9375  18.8865 0.0854 0.7445     0.4512   0.4914  56.9232  1.0165
## 11  36.0356  22.5022 0.0651 0.7232     0.4922   0.3034   9.1842  1.8368
## 12  48.6111  27.1722 0.0793 0.6961     0.4952   0.4861   7.4012  0.9251
## 13  58.8411  31.1485 0.1246 0.6839     0.4972   0.5347  17.4050  0.8288
## 14  61.8379  35.3996 0.1165 0.7300     0.4905   0.2819  12.7343  2.1224
## 15  64.2855  39.1236 0.1152 0.7057     0.4927  12.4791  -0.9199 -0.4599
## 16  74.0391  43.1868 0.1559 0.6380     0.4968   0.3638  40.2140  1.6756
## 17  87.7910  47.9212 0.1473 0.6366     0.4897   0.4275  84.3585  1.3181
## 18  93.8148  53.4397 0.1158 0.6354     0.4970   0.5357  26.0043  0.8388
## 19 108.1605  60.3665 0.0990 0.6687     0.5018   0.4708  12.3641  1.0303
## 20 126.8929  67.0308 0.0941 0.6792     0.5035   4.9738  -3.1958 -0.6392
## 21 133.2075  73.2005 0.1024 0.6559     0.5071   0.3212   6.3387  1.5847
## 22 137.7178  79.9424 0.1113 0.6537     0.5078   0.4162  30.8602  1.3417
## 23 152.0875  86.4581 0.1026 0.6514     0.5118   0.4212   9.6202  1.2025
## 24 166.0320  92.5637 0.1004 0.6433     0.5104  41.7462   0.0000  0.0000
## 25 177.4482  98.9646 0.1002 0.6096     0.5167   0.2192  21.3710  3.0530
## 26 198.6676 106.0303 0.0985 0.5889     0.5239  16.1084  -0.9379 -0.4690
## 27 225.0476 113.8041 0.1175 0.5642     0.5250   0.3715  10.1499  1.4500
## 28 231.6725 121.3802 0.1157 0.5626     0.5279 105.5903   0.0000  0.0000
## 29 245.8728 128.4814 0.1155 0.5403     0.5317   0.5470  27.3313  0.8039
## 30 262.3889 136.0795 0.1022 0.5602     0.5313   0.4722  15.6515  1.0434
##    Ratkowsky     Ball Ptbiserial    Frey McClain   Dunn Hubert SDindex Dindex
## 2     0.3214 548.2851     0.7951  1.1143  0.0390 0.0805  7e-04  9.7204 1.5709
## 3     0.3577 259.3275     0.8026  6.6768  0.0396 0.1182  7e-04  6.3281 1.4121
## 4     0.3481 123.7333     0.4935 -0.4630  0.2852 0.0126  7e-04  5.5625 1.0167
## 5     0.3252  78.9750     0.4946  0.6809  0.2836 0.0215  7e-04  3.2461 0.9750
## 6     0.3007  50.0105     0.4943  0.2784  0.2766 0.0215  7e-04  3.7235 0.8311
## 7     0.3096  35.6468     0.4960 -0.0909  0.2706 0.0215  7e-04  3.4418 0.7732
## 8     0.3063  26.7644     0.4966  0.1971  0.2691 0.0247  7e-04  3.0994 0.7414
## 9     0.3027  19.9051     0.4975  5.5429  0.2665 0.0247  7e-04  2.9770 0.7054
## 10    0.2904  15.4239     0.3603  0.5826  0.5459 0.0126  7e-04  4.6099 0.5648
## 11    0.2850  11.7687     0.3482 -0.0075  0.5049 0.0126  7e-04  4.6876 0.5036
## 12    0.2735   8.9339     0.3486  0.0539  0.4994 0.0155  7e-04  4.2723 0.4824
## 13    0.2636   7.1939     0.3489  0.3743  0.4929 0.0248  7e-04  4.3061 0.4681
## 14    0.2567   5.8779     0.3478  0.1814  0.4748 0.0248  7e-04  4.4313 0.4426
## 15    0.2500   4.9638     0.3478  0.0434  0.4711 0.0248  7e-04  4.4740 0.4242
## 16    0.2424   4.2158     0.3479  0.7300  0.4701 0.0336  7e-04  4.0885 0.4085
## 17    0.2354   3.5758     0.3447  1.6306  0.4616 0.0336  7e-04  4.3746 0.3839
## 18    0.2297   3.0284     0.3092  0.4479  0.4943 0.0336  8e-04  5.8798 0.3485
## 19    0.2241   2.5398     0.3051  0.2860  0.4495 0.0336  8e-04  6.0191 0.3309
## 20    0.2186   2.1729     0.3046  0.1253  0.4332 0.0336  8e-04  6.1843 0.3183
## 21    0.2140   1.8950     0.3046  0.1788  0.4295 0.0369  8e-04  6.2006 0.3083
## 22    0.2097   1.6563     0.3046  0.7275  0.4258 0.0405  8e-04  6.2532 0.2984
## 23    0.2053   1.4649     0.3010  0.4489  0.4087 0.0405  8e-04  6.3744 0.2849
## 24    0.2011   1.3113     0.3005  0.1295  0.4029 0.0405  8e-04  6.5510 0.2766
## 25    0.1972   1.1774     0.3005  0.4602  0.4021 0.0405  8e-04  6.5045 0.2682
## 26    0.1934   1.0567     0.3001  0.1818  0.3975 0.0405  8e-04  6.6241 0.2581
## 27    0.1899   0.9480     0.3001  0.4860  0.3962 0.0485  8e-04  6.4344 0.2520
## 28    0.1867   0.8571     0.2997  0.2017  0.3920 0.0485  8e-04  6.5617 0.2448
## 29    0.1836   0.7818     0.2997  1.5887  0.3914 0.0485  8e-04  6.3656 0.2381
## 30    0.1806   0.7136     0.2876  0.8137  0.3864 0.0485  8e-04  9.9723 0.2280
##      SDbw
## 2  1.0724
## 3  0.7017
## 4  0.5998
## 5  0.2718
## 6  0.2466
## 7  0.1942
## 8  0.1640
## 9  0.1296
## 10 0.1237
## 11 0.1293
## 12 0.1005
## 13 0.0847
## 14 0.0876
## 15 0.0742
## 16 0.0531
## 17 0.0606
## 18 0.0662
## 19 0.0560
## 20 0.0513
## 21 0.0448
## 22 0.0367
## 23 0.0356
## 24 0.0284
## 25 0.0253
## 26 0.0203
## 27 0.0204
## 28 0.0191
## 29 0.0142
## 30 0.0136
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.1671           104.6547       0.2853
## 3          0.5142           203.1521       0.3388
## 4         -0.1409           -56.6808       1.0000
## 5          0.4140           117.4720       0.4838
## 6          0.2153            94.7474       0.1987
## 7         -0.1908           -37.4469       0.4277
## 8          0.0222           529.7343       0.3749
## 9          0.4669           148.4332       0.1182
## 10         0.3548           100.0055       0.3652
## 11        -0.3258           -16.2785       0.2206
## 12        -0.1409           -56.6808       0.4194
## 13         0.1556           108.5678       0.4439
## 14        -0.2510           -24.9172       0.1705
## 15        -0.7431            -2.3458       1.0000
## 16         0.1881            99.2518       0.1984
## 17         0.3756           104.7312       0.2713
## 18         0.2454            92.2265       0.4372
## 19        -0.0027         -4016.9908       0.3735
## 20        -0.3258           -16.2785       1.0000
## 21        -0.4219           -10.1102       0.2802
## 22         0.1780           101.6241       0.2719
## 23        -0.1409           -56.6808       0.3296
## 24        -1.0633             0.0000          NaN
## 25        -0.1908           -37.4469       0.0848
## 26        -0.7431            -2.3458       1.0000
## 27        -0.1908           -37.4469       0.2729
## 28        -1.0633             0.0000          NaN
## 29         0.2646            91.7349       0.4519
## 30         0.0647           202.2234       0.3655
## 
## $Best.nc
##                      KL       CH Hartigan     CCC    Scott  Marriot   TrCovW
## Number_clusters  4.0000  30.0000   4.0000 30.0000   4.0000      6.0      4.0
## Value_Index     66.1171 978.1617  75.7384 10.6389 181.8649 259670.6 160872.4
##                   TraceW Friedman   Rubin  Cindex      DB Silhouette   Duda
## Number_clusters   4.0000    27.00 12.0000 11.0000 29.0000     2.0000 2.0000
## Value_Index     182.9912    26.38 -0.6938  0.0651  0.5403     0.7611 0.4248
##                 PseudoT2  Beale Ratkowsky     Ball PtBiserial   Frey McClain
## Number_clusters   2.0000 2.0000    3.0000   3.0000     3.0000 3.0000   2.000
## Value_Index      28.4357 1.2925    0.3577 288.9576     0.8026 6.6768   0.039
##                   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters 3.0000      0   9.000      0 30.0000
## Value_Index     0.1182      0   2.977      0  0.0136
## 
## $Best.partition
##    e_1    e_2    e_3    e_4    e_5    e_6    e_7    e_8    e_9   e_10   e_11 
##      1      1      1      1      2      2      2      2      2      2      2 
##   e_12   e_13   e_14   e_15   e_16   e_17   e_18   e_19   e_20   e_21   e_22 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_23   e_24   e_25   e_26   e_27   e_28   e_29   e_30   e_31   e_32   e_33 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_34   e_35   e_36   e_37   e_38   e_39   e_40   e_41   e_42   e_43   e_44 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_45   e_46   e_47   e_48   e_49   e_50   e_51   e_52   e_53   e_54   e_55 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_56   e_57   e_58   e_59   e_60   e_61   e_62   e_63   e_64   e_65   e_66 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_67   e_68   e_69   e_70   e_71   e_72   e_73   e_74   e_75   e_76   e_77 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_78   e_79   e_80   e_81   e_82   e_83   e_84   e_85   e_86   e_87   e_88 
##      2      2      2      2      2      2      2      2      2      2      2 
##   e_89   e_90   e_91   e_92   e_93   e_94   e_95   e_96   e_97   e_98   e_99 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_100  e_101  e_102  e_103  e_104  e_105  e_106  e_107  e_108  e_109  e_110 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_111  e_112  e_113  e_114  e_115  e_116  e_117  e_118  e_119  e_120  e_121 
##      2      2      2      2      2      2      2      2      2      2      2 
##  e_122  e_123  e_124   ip_1   ip_2   ip_3   ip_4   ip_5   ip_6   ip_7   ip_8 
##      2      2      2      1      1      1      1      1      1      1      1 
##   ip_9  ip_10  ip_11  ip_12  ip_13  ip_14  ip_15  ip_16  ip_17  ip_18  ip_19 
##      1      1      1      1      1      1      1      1      1      1      1 
##  ip_20  ip_21  ip_22  ip_23  ip_24  ip_25  ip_26  ip_27  ip_28  ip_29  ip_30 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_31  ip_32  ip_33  ip_34  ip_35  ip_36  ip_37  ip_38  ip_39  ip_40  ip_41 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_42  ip_43  ip_44  ip_45  ip_46  ip_47  ip_48  ip_49  ip_50  ip_51  ip_52 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_53  ip_54  ip_55  ip_56  ip_57  ip_58  ip_59  ip_60  ip_61  ip_62  ip_63 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_64  ip_65  ip_66  ip_67  ip_68  ip_69  ip_70  ip_71  ip_72  ip_73  ip_74 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_75  ip_76  ip_77  ip_78  ip_79  ip_80  ip_81  ip_82  ip_83  ip_84  ip_85 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_86  ip_87  ip_88  ip_89  ip_90  ip_91  ip_92  ip_93  ip_94  ip_95  ip_96 
##      2      2      2      2      2      2      2      2      2      2      2 
##  ip_97  ip_98  ip_99 ip_100 ip_101 ip_102 ip_103 ip_104 ip_105 ip_106 ip_107 
##      2      2      2      2      2      2      2      2      2      2      2 
## ip_108 ip_109 ip_110 ip_111 ip_112 ip_113 ip_114 ip_115 ip_116 
##      2      2      2      2      2      2      2      2      2
# EVALUACION - Clustering Jerarquico

# Ancho de la Silueta
hclust_clusters <- eclust(x = data_aseg_pca$x[,1:2], FUNcluster = "hclust", k = 4, seed = 123,
                      hc_metric = "manhattan", nstart = 30, graph = FALSE, method="ward.D2")
fviz_silhouette(sil.obj = hclust_clusters, print.summary = TRUE, palette = "jco",
                ggtheme = theme_classic()) 
##   cluster size ave.sil.width
## 1       1    9          0.40
## 2       2   20          0.12
## 3       3   35          0.40
## 4       4  176          0.63

#Evaluacion Indice Dunn

hc_indiceD <- cluster.stats(d = dist(data_aseg_pca$x[,1:2], method = "manhattan"),clustering=hclust_clusters$cluster)
hc_indiceD$average.within
## [1] 1.914112
hc_indiceD$average.between
## [1] 7.125497
hc_indiceD$dunn
## [1] 0.02816942
# Indice de Davies Bouldin
print(index.DB(data_aseg_pca$x[,1:2], hclust_clusters$cluster, centrotypes="centroids"))
## $DB
## [1] 0.9581487
## 
## $r
## [1] 0.7934449 1.1629173 1.1629173 0.7133152
## 
## $R
##           [,1]      [,2]      [,3]      [,4]
## [1,]       Inf 0.7934449 0.4903633 0.3614094
## [2,] 0.7934449       Inf 1.1629173 0.5891873
## [3,] 0.4903633 1.1629173       Inf 0.7133152
## [4,] 0.3614094 0.5891873 0.7133152       Inf
## 
## $d
##           1        2         3         4
## 1  0.000000 8.932438 11.396632 14.362820
## 2  8.932438 0.000000  3.453352  6.141223
## 3 11.396632 3.453352  0.000000  2.971229
## 4 14.362820 6.141223  2.971229  0.000000
## 
## $S
## [1] 4.3299627 2.7574352 1.2585274 0.8608956
## 
## $centers
##            [,1]       [,2]
## [1,] -12.864465 -1.1615525
## [2,]  -4.407428  1.7136853
## [3,]  -1.511288 -0.1672741
## [4,]   1.459226 -0.1020747
# Conectividad Modelo Clustering Jerarquico
d = dist(data_aseg_pca$x[,1:2], method = "manhattan")
connectivity(d,hclust_clusters$cluster,neighbSize = 10)
## [1] 23.86429
# Estabilidad modelo Clustering Jerárquico con clValid()
estabilidad_h<-clValid(data_aseg_norm, 4, clMethods = "hierarchical",
        validation = "stability", maxitems=600,
        metric = "manhattan", method='ward')
summary(estabilidad_h)
## 
## Clustering Methods:
##  hierarchical 
## 
## Cluster sizes:
##  4 
## 
## Validation Measures:
##                        4
##                         
## hierarchical APN  0.1116
##              AD   4.9158
##              ADM  0.3843
##              FOM  0.5342
## 
## Optimal Scores:
## 
##     Score  Method       Clusters
## APN 0.1116 hierarchical 4       
## AD  4.9158 hierarchical 4       
## ADM 0.3843 hierarchical 4       
## FOM 0.5342 hierarchical 4
# Evaluacion exahustiva con clusterboot()
testH.clusterboot<-clusterboot(data_aseg_pca$x[,1:2],B=70,bootmethod=c("jitter","boot"),clustermethod=hclustCBI, method="ward.D", k=4, seed=123)
## jitter 1 
## jitter 2 
## jitter 3 
## jitter 4 
## jitter 5 
## jitter 6 
## jitter 7 
## jitter 8 
## jitter 9 
## jitter 10 
## jitter 11 
## jitter 12 
## jitter 13 
## jitter 14 
## jitter 15 
## jitter 16 
## jitter 17 
## jitter 18 
## jitter 19 
## jitter 20 
## jitter 21 
## jitter 22 
## jitter 23 
## jitter 24 
## jitter 25 
## jitter 26 
## jitter 27 
## jitter 28 
## jitter 29 
## jitter 30 
## jitter 31 
## jitter 32 
## jitter 33 
## jitter 34 
## jitter 35 
## jitter 36 
## jitter 37 
## jitter 38 
## jitter 39 
## jitter 40 
## jitter 41 
## jitter 42 
## jitter 43 
## jitter 44 
## jitter 45 
## jitter 46 
## jitter 47 
## jitter 48 
## jitter 49 
## jitter 50 
## jitter 51 
## jitter 52 
## jitter 53 
## jitter 54 
## jitter 55 
## jitter 56 
## jitter 57 
## jitter 58 
## jitter 59 
## jitter 60 
## jitter 61 
## jitter 62 
## jitter 63 
## jitter 64 
## jitter 65 
## jitter 66 
## jitter 67 
## jitter 68 
## jitter 69 
## jitter 70 
## boot 1 
## boot 2 
## boot 3 
## boot 4 
## boot 5 
## boot 6 
## boot 7 
## boot 8 
## boot 9 
## boot 10 
## boot 11 
## boot 12 
## boot 13 
## boot 14 
## boot 15 
## boot 16 
## boot 17 
## boot 18 
## boot 19 
## boot 20 
## boot 21 
## boot 22 
## boot 23 
## boot 24 
## boot 25 
## boot 26 
## boot 27 
## boot 28 
## boot 29 
## boot 30 
## boot 31 
## boot 32 
## boot 33 
## boot 34 
## boot 35 
## boot 36 
## boot 37 
## boot 38 
## boot 39 
## boot 40 
## boot 41 
## boot 42 
## boot 43 
## boot 44 
## boot 45 
## boot 46 
## boot 47 
## boot 48 
## boot 49 
## boot 50 
## boot 51 
## boot 52 
## boot 53 
## boot 54 
## boot 55 
## boot 56 
## boot 57 
## boot 58 
## boot 59 
## boot 60 
## boot 61 
## boot 62 
## boot 63 
## boot 64 
## boot 65 
## boot 66 
## boot 67 
## boot 68 
## boot 69 
## boot 70
testH.clusterboot
## * Cluster stability assessment *
## Cluster method:  hclust/cutree 
## Full clustering results are given as parameter result
## of the clusterboot object, which also provides further statistics
## of the resampling results.
## Number of resampling runs:  70 
## 
## Number of clusters found in data:  4 
## 
##  Clusterwise Jaccard jittering mean:
## [1] 1 1 1 1
## dissolved:
## [1] 0 0 0 0
## recovered:
## [1] 70 70 70 70
##  Clusterwise Jaccard bootstrap (omitting multiple points) mean:
## [1] 0.8204666 0.7250066 0.7617709 0.6511095
## dissolved:
## [1]  7  6  0 31
## recovered:
## [1] 53 34 42 36

3.3.2.- Ejecución del modelo Clustering Jerárquico

# EJECUCION DEL CLUSTERING JERARQUICO UTILIZANDO LA METRICA MANHATTAN



hc_euclidea_single <- hclust(d = dist(x=data_aseg_pca$x[,1:2], method="manhattan"),method="ward.D2")
fviz_dend(x = hc_euclidea_single, k = 4, cex = 0.6, show_labels=FALSE) +
  geom_hline(yintercept = 20, linetype = "dashed") +
  labs(title = "Clustering Jerárquico", subtitle = "Distancia Manhattan, Linkage Ward")

# Grafico Arbol Filogenetico
Phylo = fviz_dend(hclust_clusters, cex = 0.8, lwd = 0.8, k = 4,
                  rect = TRUE,
                  k_colors = "jco",
                  rect_border = "jco",
                  rect_fill = TRUE,
                  type = "phylogenic")
Phylo

# Clusters modelo Clustering Jerárquico en las dos primeras componentes principales
fviz_cluster(hclust_clusters,data_aseg_pca$x[,1:2])

# Clusters - modelo Clustering Jerárquico - en las tres primeras componentes principales
plot3d(data_aseg_pca$x[,1:3],type="s", col=hclust_clusters$cluster, size=1)
rglwidget()
# Asignación de clusters al dataset
#datos_cluster_h <- data.frame(data_aseg, cluster = as.factor(hclust_clusters$cluster))
#datos_cluster_h
#write.csv(datos_cluster_h, "mi_HC_nK_20230610-1651.csv")

3.4.- Modelo basado en K-medoids

3.4.1.- Determinación del número óptimo de clusters y evaluación del modelo K-medoids

# MODELO BASADO EN K-MEDOIDS

# Determinacion del numero optimo de clusters

## Metodo del Codo con diferentes distancias


fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "euclidean")) + labs(subtitle = "Metodo del Codo_ Dist-Euclidean_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "manhattan")) + labs(subtitle = "Metodo del Codo_Dist-Manhattan_K-medoids") 

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "pearson")) + labs(subtitle = "Metodo del Codo_Dist-Pearson_K-medoids") 

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "wss", k.max=30, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "spearman")) + labs(subtitle = "Metodo del Codo_Dist-Spearman_K-medoids") 

## Metodo de la Silueta con diferentes distancias
fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "silhouette", k.max=10, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "euclidean")) + labs(subtitle = "Metodo de la Silueta-Euclidean_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "silhouette", k.max=10, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "manhattan")) + labs(subtitle = "Metodo de la Silueta-Manhattan_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "silhouette", k.max=10, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "pearson")) + labs(subtitle = "Metodo de la Silueta-Pearson_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method = "silhouette", k.max=10, nstart=30, diss = get_dist(data_aseg_pca$x[,1:2], method = "spearman")) + labs(subtitle = "Metodo de la Silueta-Spearman_K-medoids")

## Metodo Gap Statistic con diferentes distancias
fviz_nbclust(data_aseg_pca$x[,1:2], pam, method ="gap_stat", nstart=1, k.max=10, diss = get_dist(data_aseg_pca$x[,1:2], method = "euclidean")) + labs(subtitle = "Metodo Gap Stat-Euclidean_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method ="gap_stat", nstart=1, k.max=10, diss = get_dist(data_aseg_pca$x[,1:2], method = "manhattan")) + labs(subtitle = "Metodo Gap Stat-Manhattan_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method ="gap_stat", nstart=1, k.max=10, diss = get_dist(data_aseg_pca$x[,1:2], method = "Pearson")) + labs(subtitle = "Metodo Gap Stat-Pearson_K-medoids")

fviz_nbclust(data_aseg_pca$x[,1:2], pam, method ="gap_stat", nstart=1, k.max=10, diss = get_dist(data_aseg_pca$x[,1:2], method = "Spearman")) + labs(subtitle = "Metodo Gap Stat-Spearman_K-medoids")

# Clustering con PAM
pam_clusters <- eclust(x = data_aseg_pca$x[,1:2], FUNcluster = "pam", k = 4, seed = 123,
                      hc_metric = "manhattan", nstart=30, graph = FALSE)
fviz_cluster(pam_clusters,data_aseg_pca$x[,1:2])

#EVALUACION modelo K-medoids

# Ancho de la Silueta

fviz_silhouette(sil.obj = pam_clusters, print.summary = TRUE, palette = "jco",
                ggtheme = theme_classic()) 
##   cluster size ave.sil.width
## 1       1   10          0.42
## 2       2   44          0.17
## 3       3   95          0.27
## 4       4   91          0.81

head(pam_clusters$silinfo$widths)
cluster neighbor sil_width
ip_3 1 2 0.6095839
ip_5 1 2 0.6075852
ip_2 1 2 0.5654247
ip_4 1 2 0.5352343
ip_6 1 2 0.4500389
ip_1 1 2 0.4291083
# Indice Dunn
pam_indiceD <- cluster.stats(d = dist(data_aseg_pca$x[,1:2], method = "manhattan"), clustering=pam_clusters$cluster)
pam_indiceD$average.within
## [1] 1.668909
pam_indiceD$average.between
## [1] 5.23319
pam_indiceD$dunn
## [1] 0.005867191
# indice Davies-Bouldin DB
d<-dist(data_aseg_pca$x[,1:2], method = "manhattan")
print(index.DB(data_aseg_pca$x[,1:2], pam_clusters$cluster,d, centrotypes="medoids"))
## $DB
## [1] 0.9759078
## 
## $r
## [1] 0.7724723 1.1488748 1.1488748 0.8334091
## 
## $R
##           [,1]      [,2]      [,3]      [,4]
## [1,]       Inf 0.7724723 0.4532964 0.3753697
## [2,] 0.7724723       Inf 1.1488748 0.6728783
## [3,] 0.4532964 1.1488748       Inf 0.8334091
## [4,] 0.3753697 0.6728783 0.8334091       Inf
## 
## $d
##           1        2         3         4
## 1  0.000000 9.472437 12.309124 13.358501
## 2  9.472437 0.000000  3.006989  4.294014
## 3 12.309124 3.006989  0.000000  1.382077
## 4 13.358501 4.294014  1.382077  0.000000
## 
## $S
## [1] 4.7211115 2.5960835 0.8585702 0.2932657
## 
## $centers
##             [,1]       [,2]
## [1,] -11.1129384 -2.2030014
## [2,]  -2.0329367  0.4952620
## [3,]   0.9594637  0.1994216
## [4,]   2.1391919 -0.5205640
# Conectividad Modelo K-medoids
connectivity(d,pam_clusters$cluster,neighbSize = 10)
## [1] 28.36984
# Medidas de estabilidad - K-medoids
estabilidad_pam<-clValid(data_aseg_norm, 4, clMethods = "pam",
        validation = "stability", maxitems = 600,
        metric = "manhattan")
summary(estabilidad_pam)
## 
## Clustering Methods:
##  pam 
## 
## Cluster sizes:
##  4 
## 
## Validation Measures:
##               4
##                
## pam APN  0.0436
##     AD   4.6022
##     ADM  0.1715
##     FOM  0.5015
## 
## Optimal Scores:
## 
##     Score  Method Clusters
## APN 0.0436 pam    4       
## AD  4.6022 pam    4       
## ADM 0.1715 pam    4       
## FOM 0.5015 pam    4