library(MASS)
library(tidyverse)
library(patchwork)
library(ggdendro)
library(mclust)Clustering: hierarchical, k-means, and model-based (mclust)
Introduction
In this practical, we apply three clustering approaches. In Part 1, we use hierarchical and k-means clustering on synthetic bivariate data, to build intuition about how distance metrics, linkage, and the number of clusters affect the result, and about the instability of k-means across runs. In Part 2, we move to a real dataset of Swiss banknote measurements and use model-based clustering (mclust) to fit and compare probabilistic cluster models, including choosing the number of clusters and comparing model fit via BIC.
Make sure to load MASS before tidyverse, otherwise the function MASS::select() will overwrite dplyr::select().
Part 1: Hierarchical and k-means clustering
Before we start, set a seed for reproducibility, we use 123, and also use options(scipen = 999) to suppress scientific notations, making it easier to compare and interpret results later in the session.
set.seed(123)
options(scipen = 999)Data processing
Hierarchical clustering
K-means clustering
Hierarchical and k-means clustering both partition observations into hard, non-overlapping groups based on distance. In Part 2, we take a different, probabilistic approach: model-based clustering, which fits a mixture of distributions to the data and lets us evaluate fit statistically (via BIC) rather than only visually.
Part 2: Model-based clustering using mclust
We apply model-based clustering on a data set of bank note measurements. The data is built into the mclust package and can be loaded as a tibble:
df <- as_tibble(banknote)Data exploration
Univariate model-based clustering
Multivariate model-based clustering
We will now use all available information in the data set to cluster the observations.