- Utilizando el porcentaje de viviendas que tiene agua de red publica
dentro de la vivienda, la razón de votacion de keiko entre castillo, y
la tasa fallecidos por cada 1000 contagiados, Ud se propone agrupar a
las provincias del Peru (sin la provincia de Lima) siguiendo diversas
estrategias (no corrija correlacion negativa si la hubiera, pero siempre
normalice); y en ese proceso Ud. encuentra…
setwd("C:/Users/moren/OneDrive/2024-2/ESTADÍSTICA 2/EXAMEN FINAL")
library(rio)
## Warning: package 'rio' was built under R version 4.3.3
dataok=import("dataOK_all.xlsx")
## New names:
## • `` -> `...1`
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(cluster)
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.3
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
# Filtrar provincias (excluyendo Lima) y seleccionar variables relevantes
data_filtered <- dataok %>%
filter(key != "Lima") %>%
select(agua2_Red_fueraVivienda, Keiko, Castillo, covidFallecidos)
# Normalizar los datos
data_normalized <- scale(data_filtered)
res.agnes <- agnes(data_normalized, # Usa los datos normalizados
method = "ward")
# Visualizar dendrograma jerárquico aglomerativo
fviz_dend(res.agnes,
cex = 0.7, # Tamaño del texto de las etiquetas
horiz = TRUE, # Orientación horizontal
main = "Dendrograma Aglomerativo", # Título personalizado
rect = TRUE, # Dibujar rectángulos para los clusters
k = 4, # Número de clusters
rect_fill = TRUE) # Rellenar los rectángulos
## 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.

# Clustering jerárquico divisivo
divisive_clustering <- diana(dist(data_normalized))
# Visualizar el dendrograma
fviz_dend(divisive_clustering, k = 5) # Ajusta 'k' al número de grupos deseado

# Clustering K-means
set.seed(123) # Para reproducibilidad
kmeans_result <- kmeans(data_normalized, centers = 4) # Ajusta 'centers' al número de grupos deseado
# Visualizar los clusters
fviz_cluster(kmeans_result, data = data_normalized)
