Este documento presenta un análisis de clústeres realizado con R. El objetivo es explorar y agrupar los datos en clústeres basados en similitudes inherentes.
Es fundamental preparar el entorno de R cargando las librerías necesarias y configurando los parámetros iniciales.
# Step 1 Install and call libraries
# install.packages("cluster")
# install.packages("ggplot2")
# install.packages("data.table")
# install.packages("factoextra")
library(ggplot2)
library(data.table)
library(cluster)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
Los datos para este análisis consisten en coordenadas (x, y) que representan puntos en un espacio bidimensional.
# Step 2 Obtain data
df <- data.frame (x=c(2,2,8,5,7,6,1,4),
y=c(10,5,4,8,5,4,2,9))
# Step 3 Number of Clusters
groups <- 4
A continuación, realizaremos el análisis de clústeres para agrupar los datos basados en sus características.
# Step 4 Generate Clusters
segments <- kmeans(df,groups)
segments
## K-means clustering with 4 clusters of sizes 3, 1, 2, 2
##
## Cluster means:
## x y
## 1 3.666667 9.0
## 2 8.000000 4.0
## 3 1.500000 3.5
## 4 6.500000 4.5
##
## Clustering vector:
## [1] 1 3 2 1 4 4 3 1
##
## Within cluster sum of squares by cluster:
## [1] 6.666667 0.000000 5.000000 1.000000
## (between_SS / total_SS = 87.4 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
# Step 5 Assign data to Clusters
assignation <- cbind(df, cluster = segments$cluster)
assignation
## x y cluster
## 1 2 10 1
## 2 2 5 3
## 3 8 4 2
## 4 5 8 1
## 5 7 5 4
## 6 6 4 4
## 7 1 2 3
## 8 4 9 1
# Step 6 Graph Clusters
fviz_cluster(segments, data = df)
# Step 7 Optimize number of clusters
set.seed(123)
optimization = clusGap(df, FUN=kmeans, nstart=1, K.max = 7)
plot(optimization, xlab="Number of k clusters: ")
La visualización es clave para entender los grupos formados.
# ------------ RUN IT BACK WITH OPTIMIZED CLUSTER NUMBER -----------------
# Step 3 Number of Clusters
groups <- 3 #Changed to optimized number of clusters
# Step 4 Generate Clusters
segments <- kmeans(df,groups)
segments
## K-means clustering with 3 clusters of sizes 1, 5, 2
##
## Cluster means:
## x y
## 1 2.0 10.0
## 2 4.8 4.0
## 3 4.5 8.5
##
## Clustering vector:
## [1] 1 2 2 3 2 2 2 3
##
## Within cluster sum of squares by cluster:
## [1] 0.0 44.8 1.0
## (between_SS / total_SS = 54.5 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
# Step 5 Assign data to Clusters
assignation <- cbind(df, cluster = segments$cluster)
assignation
## x y cluster
## 1 2 10 1
## 2 2 5 2
## 3 8 4 2
## 4 5 8 3
## 5 7 5 2
## 6 6 4 2
## 7 1 2 2
## 8 4 9 3
# Step 6 Graph Clusters
fviz_cluster(segments, data = df)
Este análisis ofrece una visión general sobre la agrupación de datos mediante técnicas de clúster. Los resultados permiten identificar patrones y agrupaciones significativas dentro del conjunto de datos analizado.