knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  gganimate = list(nframes = 50)
)

library(ggplot2)

theme_set(theme_minimal(base_size = 21))

if(require(showtext)){

  sysfonts::font_add_google("IBM Plex Sans", "plex")
  showtext::showtext_auto()

}

Generating data set

The main function is sim_groups, you need to define:

library(klassets)

set.seed(123)

df <- sim_groups(n = 500, groups = 3)

plot(df)

Fit cluster algorithms

K-means stats::kmeans

You can apply the stats::kmeans using fit_statskmeans_clust.

dfc1 <- fit_statskmeans(df, centers = 2)

plot(dfc1)

Hierarchical Clustering stats::hclust

dfhc <- fit_hclust(df, k = 2)

plot(dfhc)

K-means: Basic {klassets} implementation

Or use a basic K-means implementation with:

set.seed(234)

dfc2 <- fit_kmeans(df, centers = 2, max_iteration = 6)

plot(dfc2)

What is the benefit? In the second one use a helper function kmeans_iterations to keep the iteration and see how the algorithm converges.

set.seed(234)

kmi <- kmeans_iterations(df, centers = 2, max_iteration = 6)

plot(kmi)

Now we can use gganimate package using object result from kmeans_iterations due have the classification for every point in every step:

kmi

So you can take the output of this function data and use gganimate to make the animation using in the klassets home page. The code used in that animation can be found in the package using:

system.file("animation_kmeans_iterations.R", package = "klassets")


jbkunst/klassets documentation built on Dec. 7, 2022, 9:18 p.m.