#Clusters
# 1. Crear base de datos
df <- data.frame(x=c(2,2,8,5,7,6,1,4), y=c(10,5,4,8,5,4,2,9))
# 2. Determinar número de grupos
grupos <- 3
# 3. Realizar la clasificación
segmentos <- kmeans(df,grupos)
segmentos## K-means clustering with 3 clusters of sizes 3, 2, 3
##
## Cluster means:
## x y
## 1 7.000000 4.333333
## 2 1.500000 3.500000
## 3 3.666667 9.000000
##
## Clustering vector:
## [1] 3 2 1 3 1 1 2 3
##
## Within cluster sum of squares by cluster:
## [1] 2.666667 5.000000 6.666667
## (between_SS / total_SS = 85.8 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
# 4. Revisar la asignación de grupos
asignación <- cbind(df, cluster=segmentos$cluster)
asignación## x y cluster
## 1 2 10 3
## 2 2 5 2
## 3 8 4 1
## 4 5 8 3
## 5 7 5 1
## 6 6 4 1
## 7 1 2 2
## 8 4 9 3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_cluster(segmentos, data = df, palette = c("red","blue","green"), ellipse.type = "euclid", star.plot = T, repel = T, ggtheme = theme())## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
# 6. Optimizar cantidad de grupos
library(cluster)
library(data.table)
set.seed(123)
optimización <- clusGap(df, FUN = kmeans, nstart = 1, K.max = 7)
plot(optimización, xlab = "Número de clusters K")