library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.2.1 ✔ dplyr 1.1.0
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.4 ✔ forcats 1.0.0
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.2.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.2.3
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.2.3
##
## Attaching package: 'ggpubr'
##
## The following object is masked from 'package:cowplot':
##
## get_legend
library(cluster)
library(purrr)
library(dplyr)
library(readr)
cereal <- read_csv("C:/Users/Leslie/Downloads/cereal.csv")
## Rows: 77 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): name, mfr, type
## dbl (13): calories, protein, fat, sodium, fiber, carbo, sugars, potass, vita...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(cereal)
summary(cereal)
## name mfr type calories
## Length:77 Length:77 Length:77 Min. : 50.0
## Class :character Class :character Class :character 1st Qu.:100.0
## Mode :character Mode :character Mode :character Median :110.0
## Mean :106.9
## 3rd Qu.:110.0
## Max. :160.0
## protein fat sodium fiber
## Min. :1.000 Min. :0.000 Min. : 0.0 Min. : 0.000
## 1st Qu.:2.000 1st Qu.:0.000 1st Qu.:130.0 1st Qu.: 1.000
## Median :3.000 Median :1.000 Median :180.0 Median : 2.000
## Mean :2.545 Mean :1.013 Mean :159.7 Mean : 2.152
## 3rd Qu.:3.000 3rd Qu.:2.000 3rd Qu.:210.0 3rd Qu.: 3.000
## Max. :6.000 Max. :5.000 Max. :320.0 Max. :14.000
## carbo sugars potass vitamins
## Min. :-1.0 Min. :-1.000 Min. : -1.00 Min. : 0.00
## 1st Qu.:12.0 1st Qu.: 3.000 1st Qu.: 40.00 1st Qu.: 25.00
## Median :14.0 Median : 7.000 Median : 90.00 Median : 25.00
## Mean :14.6 Mean : 6.922 Mean : 96.08 Mean : 28.25
## 3rd Qu.:17.0 3rd Qu.:11.000 3rd Qu.:120.00 3rd Qu.: 25.00
## Max. :23.0 Max. :15.000 Max. :330.00 Max. :100.00
## shelf weight cups rating
## Min. :1.000 Min. :0.50 Min. :0.250 Min. :18.04
## 1st Qu.:1.000 1st Qu.:1.00 1st Qu.:0.670 1st Qu.:33.17
## Median :2.000 Median :1.00 Median :0.750 Median :40.40
## Mean :2.208 Mean :1.03 Mean :0.821 Mean :42.67
## 3rd Qu.:3.000 3rd Qu.:1.00 3rd Qu.:1.000 3rd Qu.:50.83
## Max. :3.000 Max. :1.50 Max. :1.500 Max. :93.70
cereales <- select(cereal, -(name:type))
modelo = scale(cereales, center = TRUE, scale = TRUE)
summary(modelo)
## calories protein fat sodium
## Min. :-2.9195 Min. :-1.4116 Min. :-1.0065 Min. :-1.9047
## 1st Qu.:-0.3533 1st Qu.:-0.4982 1st Qu.:-1.0065 1st Qu.:-0.3540
## Median : 0.1600 Median : 0.4152 Median :-0.0129 Median : 0.2424
## Mean : 0.0000 Mean : 0.0000 Mean : 0.0000 Mean : 0.0000
## 3rd Qu.: 0.1600 3rd Qu.: 0.4152 3rd Qu.: 0.9807 3rd Qu.: 0.6003
## Max. : 2.7262 Max. : 3.1554 Max. : 3.9614 Max. : 1.9124
## fiber carbo sugars potass
## Min. :-0.90290 Min. :-3.6451 Min. :-1.78229 Min. :-1.36179
## 1st Qu.:-0.48333 1st Qu.:-0.6070 1st Qu.:-0.88238 1st Qu.:-0.78665
## Median :-0.06375 Median :-0.1396 Median : 0.01753 Median :-0.08526
## Mean : 0.00000 Mean : 0.0000 Mean : 0.00000 Mean : 0.00000
## 3rd Qu.: 0.35582 3rd Qu.: 0.5615 3rd Qu.: 0.91744 3rd Qu.: 0.33558
## Max. : 4.97115 Max. : 1.9637 Max. : 1.81735 Max. : 3.28142
## vitamins shelf weight cups
## Min. :-1.2643 Min. :-1.4508 Min. :-3.5195 Min. :-2.4538
## 1st Qu.:-0.1453 1st Qu.:-1.4508 1st Qu.:-0.1968 1st Qu.:-0.6490
## Median :-0.1453 Median :-0.2496 Median :-0.1968 Median :-0.3053
## Mean : 0.0000 Mean : 0.0000 Mean : 0.0000 Mean : 0.0000
## 3rd Qu.:-0.1453 3rd Qu.: 0.9516 3rd Qu.:-0.1968 3rd Qu.: 0.7690
## Max. : 3.2115 Max. : 0.9516 Max. : 3.1260 Max. : 2.9176
## rating
## Min. :-1.7529
## 1st Qu.:-0.6757
## Median :-0.1613
## Mean : 0.0000
## 3rd Qu.: 0.5811
## Max. : 3.6334
modelo = as.data.frame(modelo)
cereales=rownames(modelo)
kmcluster = kmeans(modelo,centers=4,nstart = 50)
kmcluster
## K-means clustering with 4 clusters of sizes 3, 22, 35, 17
##
## Cluster means:
## calories protein fat sodium fiber carbo
## 1 -2.2351425 1.3286071 -0.3440932 0.20268253 3.7124216 -1.85342766
## 2 -0.7265324 0.5812656 -0.4645258 -0.70099755 0.1212408 0.08346495
## 3 0.1159786 -0.7331064 -0.1832296 0.43670986 -0.6271829 0.14750532
## 4 1.0958759 0.5226506 1.0391108 -0.02770273 0.4792258 -0.08462542
## sugars potass vitamins shelf weight cups rating
## 1 -0.7323949 3.0008647 -0.1453172 0.9515734 -0.1967771 -1.86653275 2.2195570
## 2 -0.9130588 -0.0125704 -0.5013444 -0.3041915 -0.5501992 -0.03766921 1.0080334
## 3 0.3324995 -0.6788381 0.2383202 -0.3182311 -0.1967771 0.43260998 -0.6385744
## 4 0.6262939 0.8843112 0.1837835 0.8809166 1.1518771 -0.51253109 -0.3814882
##
## Clustering vector:
## [1] 1 4 1 1 3 3 3 4 2 2 3 2 3 4 3 3 3 3 3 4 2 3 3 3 3 3 2 4 4 3 3 3 2 2 4 3 3 3
## [39] 3 4 3 2 3 2 4 4 4 3 3 4 2 4 4 3 2 2 2 2 4 4 2 3 3 2 2 2 3 2 2 3 4 3 3 3 2 2
## [77] 3
##
## Within cluster sum of squares by cluster:
## [1] 10.44922 204.08176 221.18154 133.03825
## (between_SS / total_SS = 42.4 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
##Buscar el numero optimo de Clusters
# creamos una funcion que nos retorne la var.within para cada k
total_within = function(n_clusters, data, iter.max=1000, nstart=50){
cluster_means = kmeans(data,centers = n_clusters,
iter.max = iter.max,
nstart = nstart)
return(cluster_means$tot.withinss)
}
# Se aplica esta función con para diferentes valores de k
total_withinss <- map_dbl(.x = 1:15,
.f = total_within,
data = modelo)
total_withinss
## [1] 988.0000 803.9890 663.9710 568.7508 483.8744 424.9849 376.0781 339.6164
## [9] 310.7750 285.7197 265.1067 239.5962 221.7312 204.1183 188.2521
#graficamos la varianza total
data.frame(n_clusters = 1:15, suma_cuadrados_internos = total_withinss) %>%
ggplot(aes(x = n_clusters, y = suma_cuadrados_internos)) +
geom_line() +
geom_point() +
scale_x_continuous(breaks = 1:15) +
labs(title = "Suma total de cuadrados intra-cluster") +
theme_bw()
modelo = modelo %>% mutate(cluster = kmcluster$cluster)
(g1=ggplot(modelo, aes(x = calories, y = protein)) +
geom_point(aes(color=as.factor(cluster)), size=10)+
geom_text(aes(label = cluster), size = 5) +
theme_bw() +
theme(legend.position = "none")+
labs(title = "Kmenas con k=4")
)
graficamos sus 2 primeras componentes
fviz_cluster(kmcluster, modelo)+
theme_minimal()
Adicionamos la etiqueta de las ciudades
rownames(modelo)=cereales
fviz_cluster(kmcluster, modelo, show.clust.cent = T,
ellipse.type = "euclid", star.plot = T, repel = T) +
labs(title = "Resultados clustering K-means") +
theme_bw()
## Too few points to calculate an ellipse