使用 facet_wrap 进行多 Kmeans 聚类和绘图

Multiple Kmeans Clustering and plotting using facetwrap

我想问一下如何为同一数据集自动执行多个 K 均值聚类 - 我想在聚类数量发生变化时创建多个 kmean 聚类,然后使用 facet_wrap[=12= 绘制结果]

因此,人们可以目测一下什么簇数似乎最合适。

我能够这样做,因为代码是非常手工的——它能以某种方式自动化吗:


library(tidyverse)
Y <- mtcars %>% select(hp, disp)

kme1 <- kmeans(Y, 3)
kme2 <- kmeans(Y, 4)
kme3 <- kmeans(Y, 5)
kme4 <- kmeans(Y, 6)

A <- broom::augment(kme1, Y) %>% 
  mutate(num_clust = 3)
B <- broom::augment(kme2, Y) %>% 
  mutate(num_clust = 4)
C <- broom::augment(kme3, Y) %>% 
  mutate(num_clust = 5)
D <- broom::augment(kme4, Y) %>% 
  mutate(num_clust = 6)

rbind(A, B, C, D) %>% 
  ggplot(aes(hp, disp)) + 
  geom_point(aes(color = .cluster)) + 
  stat_ellipse(aes(x=hp,y=disp,fill=factor(.cluster)),
               geom="polygon", level=0.95, alpha=0.2) + 
  facet_wrap(~num_clust)

您可以使用 purrr::map 和变体:

library(tidyverse)
Y <- mtcars %>% select(hp, disp)

map(set_names(3:6), ~kmeans(Y, .x)) %>% 
  map(broom::augment, Y) %>% 
  imap(~mutate(.x, num_clust = .y)) %>% 
  bind_rows() %>% 
  ggplot(aes(hp, disp)) + 
  geom_point(aes(color = .cluster)) + 
  stat_ellipse(aes(x=hp,y=disp,fill=factor(.cluster)),
               geom="polygon", level=0.95, alpha=0.2) + 
  facet_wrap(~num_clust)