PRÁCTICA EN CLASE

library(ggfortify)
## Loading required package: ggplot2
library(see)
library(patchwork)
library(performance)
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ✔ purrr   0.3.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(rio)
library(cluster)
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.2.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(dplyr)
library(readxl)
IDH_2019 <- read_excel("IDH 2019.xlsx", sheet = "dist")

##. PASO 1

q10 <- IDH_2019 %>% select(3:8)

##. PASO 2

factoextra::fviz_nbclust(q10, kmeans, method = "silhouette") #silhouette

agrup<-kmeans(q10,centers=2)
cluster.km<-agrup$cluster 
clusplot(q10, 
         agrup$cluster, 
         color=TRUE, 
         shade=TRUE, 
         labels=2, 
         lines=0)

##. CLUSTER JERARQUICO

matriz.dist<-dist(q10)
clust_01 <- hclust(matriz.dist , method = "complete") 
clust_02 <- hclust(matriz.dist , method = "average") 
clust_03 <- hclust(matriz.dist , method = "single")
plot(clust_01) 

plot(clust_02) 

plot(clust_02) 

clust_01.1<-cutree(clust_01,h=4000)

meter los cluster en la data

lalal<-dplyr::mutate(q10,Clusterito.jerarquico=clust_01.1)
IDH<-dplyr::mutate(lalal,Clusterito.kmean=cluster.km )