1

Estoy intentando correr un análisis de K medoids a partir de la siguiente base de datos (representación de la misma).

#### Estructura de la base de datos

df <- structure(list(Sexo = c(2, 1, 1, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 
                              2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 
                              1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2), Escolaridad = c(1, 
                                                                                               3, 1, 1, 2, 2, 3, 7, 3, 3, 1, 4, 2, 2, 7, 1, 2, 7, 2, 2, 2, 4, 
                                                                                               2, 3, 1, 3, 2, 1, 1, 1, 1, 4, 3, 2, 2, 3, 4, 2, 2, 1, 1, 1, 1, 
                                                                                               2, 3, 3, 2, 2, 3, 3), edad_intervalo = c(4, 1, 6, 6, 4, 4, 2, 
                                                                                                                                        2, 4, 1, 1, 3, 3, 4, 6, 4, 4, 6, 2, 4, 5, 3, 5, 6, 6, 2, 6, 6, 
                                                                                                                                        5, 5, 4, 6, 3, 6, 4, 3, 2, 5, 5, 3, 2, 2, 5, 6, 2, 2, 3, 3, 1, 
                                                                                                                                        1), seguro_votar = c(1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 
                                                                                                                                                             2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 
                                                                                                                                                             2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 3, 2, 2), principal_eleme = c(1, 
                                                                                                                                                                                                                               1, 3, 1, 5, 1, 1, 2, 1, 4, 1, 1, 1, 1, 88, 3, 1, 3, 1, 1, 1, 
                                                                                                                                                                                                                               1, 1, 1, 1, 3, 1, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 
                                                                                                                                                                                                                               2, 2, 1, 1, 1, 1, 1, 1), partido_votaste = c(88, 5, 2, 88, 7, 
                                                                                                                                                                                                                                                                            1, 66, 5, 7, 7, 6, 3, 2, 66, 1, 1, 7, 4, 88, 2, 7, 2, 7, 88, 
                                                                                                                                                                                                                                                                            1, 5, 88, 2, 7, 2, 5, 8, 7, 7, 88, 66, 7, 7, 5, 1, 66, 2, 66, 
                                                                                                                                                                                                                                                                            7, 5, 5, 88, 2, 66, 66), votarias_mismo = c(2, 2, 2, 88, 1, 88, 
                                                                                                                                                                                                                                                                                                                        2, 88, 1, 1, 2, 2, 1, 2, 1, 2, 2, 1, 88, 1, 1, 1, 1, 1, 1, 1, 
                                                                                                                                                                                                                                                                                                                        88, 2, 1, 2, 2, 2, 1, 1, 88, 88, 88, 1, 2, 2, 2, 1, 2, 2, 2, 
                                                                                                                                                                                                                                                                                                                        1, 88, 2, 88, 88), arrepientes = c(1, 2, 1, 2, 2, 2, 88, 88, 
                                                                                                                                                                                                                                                                                                                                                           2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 88, 1, 
                                                                                                                                                                                                                                                                                                                                                           2, 2, 1, 2, 2, 2, 2, 88, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 2, 1, 
                                                                                                                                                                                                                                                                                                                                                           88, 88), dispuesto_cambiar = c(1, 2, 1, 4, 4, 3, 2, 3, 4, 3, 
                                                                                                                                                                                                                                                                                                                                                                                          1, 1, 4, 3, 2, 1, 1, 2, 2, 3, 4, 4, 3, 88, 2, 4, 1, 1, 4, 1, 
                                                                                                                                                                                                                                                                                                                                                                                          1, 2, 3, 4, 88, 3, 2, 4, 2, 1, 1, 2, 4, 1, 1, 2, 3, 1, 4, 88), 
                     voto_partido = c(5, 8, 7, 77, 77, 77, 77, 77, 7, 7, 6, 4, 
                                      2, 2, 2, 5, 1, 4, 7, 2, 7, 2, 7, 77, 88, 5, 77, 7, 5, 5, 
                                      7, 7, 7, 7, 99, 5, 77, 7, 13, 5, 12, 2, 2, 9, 7, 77, 88, 
                                      11, 5, 7), voto_partido_segundo = c(1, 4, 7, 88, 7, 88, 88, 
                                                                          77, 7, 7, 4, 99, 7, 3, 4, 5, 2, 4, 5, 2, 7, 2, 7, 77, 88, 
                                                                          99, 77, 7, 1, 5, 5, 13, 99, 99, 88, 7, 77, 99, 11, 5, 2, 
                                                                          7, 99, 9, 8, 7, 99, 5, 4, 1), voto_partido_nunca = c(99, 
                                                                                                                               2, 2, 88, 2, 88, 99, 99, 7, 3, 1, 11, 1, 1, 2, 2, 7, 1, 88, 
                                                                                                                               4, 7, 5, 1, 2, 88, 5, 88, 2, 2, 2, 12, 1, 2, 99, 99, 1, 2, 

Copio el código para el cálculo de las distancias entre las observaciones y la determinación y visualización del número óptimo de clusters. En este último paso, obtengo de vuelta el error "Error in if (class(best_nc) == "numeric") print(best_nc) else if (class(best_nc) == :the condition has length > 1" ¿Cómo podría resolverlo para visualizar, en el gráfico de barras que muestra la función fviz_nbclust, el número óptimo de clusters?

#### Calculando y visualizando las distancias entre las observaciones.

library(factoextra)
#> Loading required package: ggplot2
#> Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(NbClust)


m.distance <- get_dist(df, method = "kendall") #el método aceptado también puede ser: "maximum", "manhattan", "canberra", "binary", "minkowski", "pearson", "spearman" o "kendall"
fviz_dist(m.distance, gradient = list(low = "blue", mid = "white", high = "red"))



#### Determinando el número óptimo de clusters.
resnumclust<-NbClust(df, distance = "euclidean", min.nc=2, max.nc=10, method = "kmean", index = "all")
#> Warning in pf(beale, pp, df2): NaNs produced

#> *** : 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 
#> * 1 proposed 3 as the best number of clusters 
#> * 3 proposed 4 as the best number of clusters 
#> * 3 proposed 5 as the best number of clusters 
#> * 1 proposed 6 as the best number of clusters 
#> * 3 proposed 7 as the best number of clusters 
#> * 1 proposed 8 as the best number of clusters 
#> * 2 proposed 9 as the best number of clusters 
#> * 4 proposed 10 as the best number of clusters 
#> 
#>                    ***** Conclusion *****                            
#>  
#> * According to the majority rule, the best number of clusters is  2 
#>  
#>  
#> *******************************************************************
fviz_nbclust(resnumclust)
#> Error in if (class(best_nc) == "numeric") print(best_nc) else if (class(best_nc) == : the condition has length > 1

Created on 2022-08-14 by the reprex package (v2.0.1.9000)

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.