library(readxl)
Democracia_IDH_AL <- read_excel("Democracia_IDH_AL.xlsx")
View(Democracia_IDH_AL)

#Observado el tipo de variables de la data del IDH y democracia en AL

str(Democracia_IDH_AL)
## tibble [20 × 8] (S3: tbl_df/tbl/data.frame)
##  $ País                       : chr [1:20] "Argentina" "Bolivia" "Brasil" "Chile" ...
##  $ Puntuación                 : num [1:20] 7.02 5.7 6.97 7.97 6.96 8.07 3 6.27 5.96 5.6 ...
##  $ Procesoelectoralypluralismo: num [1:20] 9.17 7.5 9.58 9.58 9.17 9.58 1.08 8.75 9.17 7.92 ...
##  $ Funcionamientodelgobierno  : num [1:20] 5.36 4.64 5.36 8.57 6.79 7.5 3.57 5.36 4.29 5.36 ...
##  $ Participaciónpolítica      : num [1:20] 6.11 5.56 6.67 4.44 5 6.67 3.33 6.11 5.56 3.89 ...
##  $ Culturapolítica            : num [1:20] 6.25 3.75 5 8.13 5.63 7.5 4.38 4.38 3.75 4.38 ...
##  $ Derechosciviles            : num [1:20] 8.24 7.06 8.24 9.12 8.24 9.12 2.65 7.65 7.06 6.47 ...
##  $ IDH                        : chr [1:20] "0,841" "0,718" "0,758" "0,849" ...

#índice de democracia en AL

str(Democracia_IDH_AL$IDH)
##  chr [1:20] "0,841" "0,718" "0,758" "0,849" "0,756" "0,811" "0,759" "0,734" ...
library(DescTools)
library(moments)
Democracia_IDH_AL$IDH <- as.numeric(gsub(",", ".", Democracia_IDH_AL$IDH))
print(Democracia_IDH_AL)
## # A tibble: 20 × 8
##    País          Puntuación Procesoelectoralypluralismo Funcionamientodelgobie…¹
##    <chr>              <dbl>                       <dbl>                    <dbl>
##  1 Argentina           7.02                        9.17                     5.36
##  2 Bolivia             5.7                         7.5                      4.64
##  3 Brasil              6.97                        9.58                     5.36
##  4 Chile               7.97                        9.58                     8.57
##  5 Colombia            6.96                        9.17                     6.79
##  6 Costa Rica          8.07                        9.58                     7.5 
##  7 Cuba                3                           1.08                     3.57
##  8 Ecuador             6.27                        8.75                     5.36
##  9 El Salvador         5.96                        9.17                     4.29
## 10 Guatemala           5.6                         7.92                     5.36
## 11 Haití               4.91                        5.58                     2.93
## 12 Honduras            5.63                        8.5                      4.64
## 13 México              6.19                        8.33                     6.07
## 14 Nicaragua           3.63                        2.67                     1.86
## 15 Panamá              7.05                        9.58                     6.07
## 16 Paraguay            6.24                        8.75                     5.71
## 17 Perú                6.6                         9.17                     5   
## 18 R. Dominicana       6.54                        9.17                     5.36
## 19 Uruguay             8.38                       10                        8.57
## 20 Venezuela           3.16                        1.67                     1.79
## # ℹ abbreviated name: ¹​Funcionamientodelgobierno
## # ℹ 4 more variables: Participaciónpolítica <dbl>, Culturapolítica <dbl>,
## #   Derechosciviles <dbl>, IDH <dbl>
str(Democracia_IDH_AL$IDH)
##  num [1:20] 0.841 0.718 0.758 0.849 0.756 0.811 0.759 0.734 0.676 0.645 ...
head(Democracia_IDH_AL$IDH)
## [1] 0.841 0.718 0.758 0.849 0.756 0.811
Democracia_IDH_AL$IDH <- gsub("[^0-9.]", "", Democracia_IDH_AL$IDH)
Democracia_IDH_AL$IDH <- as.numeric(Democracia_IDH_AL$IDH)

summary(Democracia_IDH_AL$IDH)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.5590  0.6873  0.7570  0.7381  0.7880  0.8490
allStats=c(summary(Democracia_IDH_AL$IDH),
  sd=sd(Democracia_IDH_AL$IDH),
  skew=Skew(Democracia_IDH_AL$IDH),
  kurt=Kurt(Democracia_IDH_AL$IDH),
  cv=CoefVar(Democracia_IDH_AL$IDH))
allStats
##        Min.     1st Qu.      Median        Mean     3rd Qu.        Max. 
##  0.55900000  0.68725000  0.75700000  0.73810000  0.78800000  0.84900000 
##          sd        skew        kurt          cv 
##  0.07579467 -0.54948487 -0.47384885  0.10268889

Resultado: se observan los puntajes del índice de desarrollo humano, el mayor puntajes es 0.849 (Chile), mientras que el mínimo es 0.559(Haití)

library(ggplot2)

base=ggplot(data=Democracia_IDH_AL,
            aes(x=IDH))
histogram= base + geom_histogram(aes(y = after_stat(density)),
                 colour = 1, fill = "skyblue",bins=10) +  
    stat_function(fun = dnorm,
                  args = list(mean = allStats['Mean'],
                              sd = allStats['sd']),col='pink')
    
histogram

RESULTADO: Se observa que la densidad del puntaje del IDH se encuentra entre 700 y 800 de puntaje, el promedio de todo el puntaje del IDH en América Latina es 738.1

#Análisis bivariado

str(Democracia_IDH_AL$Puntuación)
##  num [1:20] 7.02 5.7 6.97 7.97 6.96 8.07 3 6.27 5.96 5.6 ...

#Análisis de correlación entre el IDH la Puntuación de democracia y participación política

library(ggplot2)
library(ggrepel)
names(Democracia_IDH_AL) #verifico los nombres
## [1] "País"                        "Puntuación"                 
## [3] "Procesoelectoralypluralismo" "Funcionamientodelgobierno"  
## [5] "Participaciónpolítica"       "Culturapolítica"            
## [7] "Derechosciviles"             "IDH"
ggplot(Democracia_IDH_AL, aes(x = Puntuación, y = IDH, label = País)) +
  geom_point() +
  geom_text_repel(size = 3) +
  labs(x = "Puntuación", y = "IDH", title = "Gráfico de dispersión democracia e IDH en AL")

#Índice de correlación con matriz

f1 <- Democracia_IDH_AL[, c("IDH", "Puntuación", "Participaciónpolítica", "Culturapolítica")]
correlation_matrix <- cor(f1)
correlation_matrix
##                             IDH Puntuación Participaciónpolítica
## IDH                   1.0000000  0.6249085            0.57783965
## Puntuación            0.6249085  1.0000000            0.66445396
## Participaciónpolítica 0.5778396  0.6644540            1.00000000
## Culturapolítica       0.4007599  0.5238965           -0.01544892
##                       Culturapolítica
## IDH                        0.40075990
## Puntuación                 0.52389652
## Participaciónpolítica     -0.01544892
## Culturapolítica            1.00000000

RESULTADO: En cuanto al IDH y la Puntuación de democracia se observa que hay una correlación positiva. Puede indicar que los países con puntuaciones de democracia más altos influyen en el IDH y por ende esta variable es más alta. Asimismo, el cuando al IDH y la Participación Política, se observa que también hay una correlación positiva moderada. Por ende, se puede proponer que los países con un mayor puntaje en participación política tienen un IDH más alto. Finalmente, se observa que en cuanto al IDH y la Cultura política, hay una baja correlación entre ambas variables; sin embargo, aporta a la correlación.

#REGRESIÓN #modelo1

f1modelo <- formula(IDH ~ Puntuación + Participaciónpolítica)

modelo1 <- lm(f1, data = Democracia_IDH_AL)
summary(modelo1)
## 
## Call:
## lm(formula = f1, data = Democracia_IDH_AL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.14427 -0.02483  0.00174  0.02006  0.12191 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           0.416513   0.088304   4.717 0.000233 ***
## Puntuación            0.004084   0.017033   0.240 0.813570    
## Participaciónpolítica 0.035460   0.019218   1.845 0.083606 .  
## Culturapolítica       0.020603   0.014158   1.455 0.164944    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0582 on 16 degrees of freedom
## Multiple R-squared:  0.5036, Adjusted R-squared:  0.4105 
## F-statistic:  5.41 on 3 and 16 DF,  p-value: 0.009197

#modelo2

f1modelo2 <- formula(IDH ~ Puntuación + Participaciónpolítica + Culturapolítica) #incluyendo otra variable

modelo2 <- lm(f1, data = Democracia_IDH_AL)
summary(modelo2)
## 
## Call:
## lm(formula = f1, data = Democracia_IDH_AL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.14427 -0.02483  0.00174  0.02006  0.12191 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           0.416513   0.088304   4.717 0.000233 ***
## Puntuación            0.004084   0.017033   0.240 0.813570    
## Participaciónpolítica 0.035460   0.019218   1.845 0.083606 .  
## Culturapolítica       0.020603   0.014158   1.455 0.164944    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0582 on 16 degrees of freedom
## Multiple R-squared:  0.5036, Adjusted R-squared:  0.4105 
## F-statistic:  5.41 on 3 and 16 DF,  p-value: 0.009197

RESULTADO:

#Haciendo un modelo alternativo con el uso de otras variables

f1modeloalt <- formula(IDH ~ Puntuación + Participaciónpolítica + Culturapolítica + Derechosciviles + Funcionamientodelgobierno)

modeloalt <- lm(f1modeloalt, data = Democracia_IDH_AL)
summary(modeloalt)
## 
## Call:
## lm(formula = f1modeloalt, data = Democracia_IDH_AL)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.085684 -0.029336  0.003183  0.025101  0.086983 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                0.437040   0.070214   6.224 2.22e-05 ***
## Puntuación                -0.081243   0.070813  -1.147  0.27049    
## Participaciónpolítica      0.058024   0.018815   3.084  0.00809 ** 
## Culturapolítica            0.027010   0.011593   2.330  0.03530 *  
## Derechosciviles            0.009498   0.039006   0.243  0.81116    
## Funcionamientodelgobierno  0.053304   0.019031   2.801  0.01415 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04592 on 14 degrees of freedom
## Multiple R-squared:  0.7295, Adjusted R-squared:  0.6329 
## F-statistic: 7.551 on 5 and 14 DF,  p-value: 0.001259

RESULTADO: Dado que la variable Puntuación, Participaciónpolítica y Culturapolítica no aportan en los primeros dos modelos de regresión, se crea un tercer modelo alternativo que conecta a todas las variables independientes con la variable dependiente. De ese modo se observa que hay significancia de la variable Participaciónpolítica, Culturapolítica y Funcionamientodelgobierno en el Índice de desarrollo humano a nivel de América Latina ya que, son menores a 0.05. Además, el efecto es directo dado que en la columna Estimate las variables son positivas.

#Vemos la gráfica de correlación entre las variables

library(ggcorrplot)
colNums=names(Democracia_IDH_AL)[c(4,5,6,8)]
numXs=Democracia_IDH_AL[,colNums]
ggcorrplot(cor(numXs),lab = T,show.diag = F)

library(modelsummary)
## `modelsummary` 2.0.0 now uses `tinytable` as its default table-drawing
##   backend. Learn more at: https://vincentarelbundock.github.io/tinytable/
## 
## Revert to `kableExtra` for one session:
## 
##   options(modelsummary_factory_default = 'kableExtra')
##   options(modelsummary_factory_latex = 'kableExtra')
##   options(modelsummary_factory_html = 'kableExtra')
## 
## Silence this message forever:
## 
##   config_modelsummary(startup_message = FALSE)
## 
## Attaching package: 'modelsummary'
## The following objects are masked from 'package:DescTools':
## 
##     Format, Mean, Median, N, SD, Var
modeloalt=list('modeloalt'=modeloalt)
modelsummary(modeloalt, title = "Regresion: modelo alternativo",
            stars = TRUE,
            output = "kableExtra")
Regresion: modelo alternativo
modeloalt
(Intercept) 0.437***
(0.070)
Puntuación -0.081
(0.071)
Participaciónpolítica 0.058**
(0.019)
Culturapolítica 0.027*
(0.012)
Derechosciviles 0.009
(0.039)
Funcionamientodelgobierno 0.053*
(0.019)
Num.Obs. 20
R2 0.730
R2 Adj. 0.633
AIC -59.6
BIC -52.6
Log.Lik. 36.804
RMSE 0.04
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

#Análisis Factorial Exploratorio (EFA)

dontselect=c("País")
select=setdiff(names(Democracia_IDH_AL),dontselect) 
theData=Democracia_IDH_AL[,select]

library(magrittr)
head(theData,8)%>%
    rmarkdown::paged_table()
library(polycor)
correlation_matrix <- cor(theData)
round(correlation_matrix,2)
##                             Puntuación Procesoelectoralypluralismo
## Puntuación                        1.00                        0.93
## Procesoelectoralypluralismo       0.93                        1.00
## Funcionamientodelgobierno         0.91                        0.78
## Participaciónpolítica             0.66                        0.67
## Culturapolítica                   0.52                        0.24
## Derechosciviles                   0.98                        0.93
## IDH                               0.62                        0.43
##                             Funcionamientodelgobierno Participaciónpolítica
## Puntuación                                       0.91                  0.66
## Procesoelectoralypluralismo                      0.78                  0.67
## Funcionamientodelgobierno                        1.00                  0.50
## Participaciónpolítica                            0.50                  1.00
## Culturapolítica                                  0.51                 -0.02
## Derechosciviles                                  0.85                  0.62
## IDH                                              0.71                  0.58
##                             Culturapolítica Derechosciviles  IDH
## Puntuación                             0.52            0.98 0.62
## Procesoelectoralypluralismo            0.24            0.93 0.43
## Funcionamientodelgobierno              0.51            0.85 0.71
## Participaciónpolítica                 -0.02            0.62 0.58
## Culturapolítica                        1.00            0.52 0.40
## Derechosciviles                        0.52            1.00 0.55
## IDH                                    0.40            0.55 1.00

#Gráfico con todas las variables

ggcorrplot(correlation_matrix)

#viendo que datos permiten factorizar

library(psych)
## 
## Attaching package: 'psych'
## The following object is masked from 'package:polycor':
## 
##     polyserial
## The following object is masked from 'package:modelsummary':
## 
##     SD
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following objects are masked from 'package:DescTools':
## 
##     AUC, ICC, SD
psych::KMO(correlation_matrix) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = correlation_matrix)
## Overall MSA =  0.43
## MSA for each item = 
##                  Puntuación Procesoelectoralypluralismo 
##                        0.46                        0.43 
##   Funcionamientodelgobierno       Participaciónpolítica 
##                        0.43                        0.31 
##             Culturapolítica             Derechosciviles 
##                        0.20                        0.54 
##                         IDH 
##                        0.97

#viendo si la matriz de correlación es adecuada

cortest.bartlett(correlation_matrix,n=nrow(theData))$p.value>0.05
## [1] FALSE

Resultado: es una matriz singular

fa.parallel(theData, fa = 'fa',correct = T,plot = F)
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA

RESULTADO: Dado que las variables son menor a 0.6, no es posible hacer el Análisis Factorial Exploratorio, varimix ni oblimin pueden ser empleadas

#Análisis de conglomerados

Distribución de componentes de la democracia

boxplot(Democracia_IDH_AL[,c(2:7)],horizontal = F,las=2,cex.axis = 0.5)

library(BBmisc)
## 
## Attaching package: 'BBmisc'
## The following object is masked from 'package:DescTools':
## 
##     %nin%
## The following object is masked from 'package:base':
## 
##     isFALSE
boxplot(normalize(Democracia_IDH_AL[,c(2:7)],method='range',range=c(0,10)))

#Correlación

cor(Democracia_IDH_AL[,c(2:8)])
##                             Puntuación Procesoelectoralypluralismo
## Puntuación                   1.0000000                   0.9302970
## Procesoelectoralypluralismo  0.9302970                   1.0000000
## Funcionamientodelgobierno    0.9088101                   0.7838096
## Participaciónpolítica        0.6644540                   0.6705509
## Culturapolítica              0.5238965                   0.2434508
## Derechosciviles              0.9810601                   0.9265352
## IDH                          0.6249085                   0.4285165
##                             Funcionamientodelgobierno Participaciónpolítica
## Puntuación                                  0.9088101            0.66445396
## Procesoelectoralypluralismo                 0.7838096            0.67055088
## Funcionamientodelgobierno                   1.0000000            0.49625015
## Participaciónpolítica                       0.4962501            1.00000000
## Culturapolítica                             0.5051464           -0.01544892
## Derechosciviles                             0.8527415            0.61987591
## IDH                                         0.7051060            0.57783965
##                             Culturapolítica Derechosciviles       IDH
## Puntuación                       0.52389652       0.9810601 0.6249085
## Procesoelectoralypluralismo      0.24345075       0.9265352 0.4285165
## Funcionamientodelgobierno        0.50514637       0.8527415 0.7051060
## Participaciónpolítica           -0.01544892       0.6198759 0.5778396
## Culturapolítica                  1.00000000       0.5236906 0.4007599
## Derechosciviles                  0.52369061       1.0000000 0.5483545
## IDH                              0.40075990       0.5483545 1.0000000

#Clusterización

dataClus=Democracia_IDH_AL[,c(2:8)]
row.names(dataClus)=Democracia_IDH_AL$País
## Warning: Setting row names on a tibble is deprecated.
library(cluster)
g.dist = daisy(dataClus, metric="gower")

#viendo cantidad de clusters

library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_nbclust(dataClus, pam,diss=g.dist,method = "gap_stat",k.max = 8,verbose = F)

RESULTADO: 3 Clusters

#1. PAM

library(kableExtra)
set.seed(123)
res.pam=pam(g.dist,3,cluster.only = F)

Democracia_IDH_AL$pam=res.pam$cluster
head(Democracia_IDH_AL,15)%>%kbl()%>%kable_styling()
País Puntuación Procesoelectoralypluralismo Funcionamientodelgobierno Participaciónpolítica Culturapolítica Derechosciviles IDH pam
Argentina 7.02 9.17 5.36 6.11 6.25 8.24 0.841 1
Bolivia 5.70 7.50 4.64 5.56 3.75 7.06 0.718 1
Brasil 6.97 9.58 5.36 6.67 5.00 8.24 0.758 1
Chile 7.97 9.58 8.57 4.44 8.13 9.12 0.849 2
Colombia 6.96 9.17 6.79 5.00 5.63 8.24 0.756 1
Costa Rica 8.07 9.58 7.50 6.67 7.50 9.12 0.811 2
Cuba 3.00 1.08 3.57 3.33 4.38 2.65 0.759 3
Ecuador 6.27 8.75 5.36 6.11 4.38 7.65 0.734 1
El Salvador 5.96 9.17 4.29 5.56 3.75 7.06 0.676 1
Guatemala 5.60 7.92 5.36 3.89 4.38 6.47 0.645 1
Haití 4.91 5.58 2.93 3.89 6.25 5.88 0.559 3
Honduras 5.63 8.50 4.64 4.44 4.38 6.18 0.629 1
México 6.19 8.33 6.07 7.22 3.13 6.18 0.781 1
Nicaragua 3.63 2.67 1.86 3.89 5.63 4.12 0.661 3
Panamá 7.05 9.58 6.07 6.67 5.00 7.94 0.809 1
fviz_silhouette(res.pam,print.summary = F)

silPAM=data.frame(res.pam$silinfo$widths)
silPAM$País=row.names(silPAM)
poorPAM=silPAM[silPAM$sil_width<0,'País']%>%sort()
poorPAM
## character(0)
#aggregate(.~ pam, data=dataClus,mean)
Democracia_IDH_AL$pamIDHpoor=Democracia_IDH_AL$País%in%poorPAM
Democracia_IDH_AL$pamIDH=as.ordered(Democracia_IDH_AL$pam)
Democracia_IDH_AL$pam=NULL

#2. AGNES

fviz_nbclust(dataClus, hcut,diss=g.dist,method = "gap_stat",k.max = 8,verbose = F,hc_func = "agnes")

set.seed(123)
library(factoextra)

res.agnes<- hcut(g.dist, k = 3,hc_func='agnes',hc_method = "ward.D")

Democracia_IDH_AL$agnes=res.agnes$cluster

# ver

head(dataClus,20)%>%kbl()%>%kable_styling()
Puntuación Procesoelectoralypluralismo Funcionamientodelgobierno Participaciónpolítica Culturapolítica Derechosciviles IDH
7.02 9.17 5.36 6.11 6.25 8.24 0.841
5.70 7.50 4.64 5.56 3.75 7.06 0.718
6.97 9.58 5.36 6.67 5.00 8.24 0.758
7.97 9.58 8.57 4.44 8.13 9.12 0.849
6.96 9.17 6.79 5.00 5.63 8.24 0.756
8.07 9.58 7.50 6.67 7.50 9.12 0.811
3.00 1.08 3.57 3.33 4.38 2.65 0.759
6.27 8.75 5.36 6.11 4.38 7.65 0.734
5.96 9.17 4.29 5.56 3.75 7.06 0.676
5.60 7.92 5.36 3.89 4.38 6.47 0.645
4.91 5.58 2.93 3.89 6.25 5.88 0.559
5.63 8.50 4.64 4.44 4.38 6.18 0.629
6.19 8.33 6.07 7.22 3.13 6.18 0.781
3.63 2.67 1.86 3.89 5.63 4.12 0.661
7.05 9.58 6.07 6.67 5.00 7.94 0.809
6.24 8.75 5.71 5.00 4.38 7.35 0.742
6.60 9.17 5.00 5.56 5.63 7.65 0.758
6.54 9.17 5.36 6.11 5.00 7.06 0.765
8.38 10.00 8.57 6.11 7.50 9.71 0.820
3.16 1.67 1.79 4.44 4.38 3.53 0.691

#Aglomeración Agnes

fviz_dend(res.agnes, cex = 0.7, horiz = T,main = "")
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

fviz_silhouette(res.agnes,print.summary = F)

silAGNES=data.frame(res.agnes$silinfo$widths)
silAGNES$country=row.names(silAGNES)
poorAGNES=silAGNES[silAGNES$sil_width<0,'country']%>%sort()
poorAGNES
## [1] "Argentina" "Haití"

RESULTADO: Argentina y Haití mal clusterizados

#aggregate(.~ agnes, data=Democracia_IDH_AL,mean)
Democracia_IDH_AL$agnesIDHpoor=Democracia_IDH_AL$País%in%poorAGNES
Democracia_IDH_AL$agnesIDH=as.ordered(Democracia_IDH_AL$agnes)
Democracia_IDH_AL$agnes=NULL

#3. GAP

fviz_nbclust(dataClus, hcut,diss=g.dist,method = "gap_stat",k.max = 8,verbose = F,hc_func = "diana")

#Clusterización con DIANA

set.seed(123)
res.diana <- hcut(g.dist, k = 3,hc_func='diana')
dataClus$diana=res.diana$cluster
head(dataClus,15)%>%kbl%>%kable_styling()
Puntuación Procesoelectoralypluralismo Funcionamientodelgobierno Participaciónpolítica Culturapolítica Derechosciviles IDH diana
7.02 9.17 5.36 6.11 6.25 8.24 0.841 1
5.70 7.50 4.64 5.56 3.75 7.06 0.718 1
6.97 9.58 5.36 6.67 5.00 8.24 0.758 1
7.97 9.58 8.57 4.44 8.13 9.12 0.849 2
6.96 9.17 6.79 5.00 5.63 8.24 0.756 1
8.07 9.58 7.50 6.67 7.50 9.12 0.811 2
3.00 1.08 3.57 3.33 4.38 2.65 0.759 3
6.27 8.75 5.36 6.11 4.38 7.65 0.734 1
5.96 9.17 4.29 5.56 3.75 7.06 0.676 1
5.60 7.92 5.36 3.89 4.38 6.47 0.645 1
4.91 5.58 2.93 3.89 6.25 5.88 0.559 3
5.63 8.50 4.64 4.44 4.38 6.18 0.629 1
6.19 8.33 6.07 7.22 3.13 6.18 0.781 1
3.63 2.67 1.86 3.89 5.63 4.12 0.661 3
7.05 9.58 6.07 6.67 5.00 7.94 0.809 1
fviz_dend(res.diana, cex = 0.7, horiz = T, main = "")

fviz_silhouette(res.diana,print.summary = F)

silDIANA=data.frame(res.diana$silinfo$widths)
silDIANA$Country=row.names(silDIANA)
poorDIANA=silDIANA[silDIANA$sil_width<0,'Country']%>%sort()
poorDIANA
## character(0)
dataClus$diana=dplyr::recode(dataClus$diana, `1` = 1, `4`=2,`2`=3,`3`=4)

#Visualización comparativa

proyeccion = cmdscale(g.dist, k=2,add = T) 
head(proyeccion$points,20)
##                      [,1]        [,2]
## Argentina      0.21775459 -0.02900172
## Bolivia       -0.06829822  0.15457134
## Brasil         0.16616471  0.09121768
## Chile          0.32173007 -0.31295474
## Colombia       0.13540714 -0.05122613
## Costa Rica     0.39006049 -0.12364613
## Cuba          -0.48017816 -0.06990417
## Ecuador        0.05864136  0.14306340
## El Salvador   -0.04836731  0.15369033
## Guatemala     -0.16120426  0.04966532
## Haití         -0.30449662 -0.16889724
## Honduras      -0.15800121  0.06428661
## México         0.07845949  0.23152622
## Nicaragua     -0.46870234 -0.18901788
## Panamá         0.21610114  0.07233221
## Paraguay       0.01287981  0.08841961
## Perú           0.08433386  0.02616666
## R. Dominicana  0.09357619  0.09949757
## Uruguay        0.41997293 -0.17444272
## Venezuela     -0.50583364 -0.05534619
Democracia_IDH_AL$dim1 <- proyeccion$points[,1]
Democracia_IDH_AL$dim2 <- proyeccion$points[,2]