1 Groups behind the data

A discrete latent variable

In a factor model, the centre \(\lambda f\) slides along a line. In a cluster model, the latent variable is discrete: each observation belongs to one of a few groups, and its centre jumps among a few points.

The cluster model

The group of observation \(t\), one of \(k\) groups:

\[ g_t=\begin{cases} 1 & \text{if observation } t \text{ belongs to group } 1,\\ \vdots & \\ k & \text{if observation } t \text{ belongs to group } k. \end{cases} \]

With \(\mu_{li}\) the mean of variable \(i\) in group \(l\), and two variables:

\[ x_{t1}=\mu_{g_t1}+u_{t1},\qquad x_{t2}=\mu_{g_t2}+u_{t2} \qquad\text{or}\qquad \mathbf x_t=\mu_{g_t}+\mathbf u_t \]

  • \(\mathbf u_t\): the deviation from the group means, with covariance matrix \(\Sigma_l\) within group \(l\)
  • \(\pi_l=P(g_t=l)\): the share of group \(l\)

Setting \(\mu_{g_t}=\Lambda\mathbf f_t\) gives the factor model \(\mathbf x_t=\Lambda\mathbf f_t+\mathbf u_t\), whose centre stays on a line or a plane. In the cluster model, \(\mu_{g_t}\) is one of only \(k\) points, which can lie anywhere. Cluster analysis estimates the groups from the observations alone.

Three hills, three species

The density of the penguins’ bill length and depth has three hills, one for each species.

Each method sees only the measurements. The species then check the result.

2 Hierarchical clustering

Merging step by step

Merge the two closest clusters, again and again. Distance between clusters: the average of the Euclidean distances \(d_{st}=\lVert\mathbf x_s-\mathbf x_t\rVert\) between their members \(s\) and \(t\).

The distance between two clusters

  • Single: the closest pair
  • Complete: the farthest pair
  • Average: the mean of all pairs
  • Ward: merge where \(W\) rises least

\(W\), the within-cluster sum of squares: the squared distances of the observations from the means of their clusters, added up.

Single linkage chains

All 333 penguins, four measurements, each standardised. Height: the distance between the two clusters when they merge, as each linkage measures it. For single linkage one close pair is enough to merge two clusters, so a chain of neighbours links the Adelie and the Chinstrap.

3 k-means

k-means

\(\hat g_t\) is the cluster of observation \(t\), and \(\bar{\mathbf x}_l\) the mean of cluster \(l\), its centre. k-means minimises

\[ W=\sum_{t=1}^n\sum_{i=1}^p\big(x_{ti}-\bar x_{\hat g_ti}\big)^2=\sum_{t=1}^n\big\lVert\mathbf x_t-\bar{\mathbf x}_{\hat g_t}\big\rVert^2 \]

Lloyd’s algorithm repeats two steps until no observation moves:

  1. Assign each observation to its nearest centre.
  2. Update each centre to the mean of its cluster.

\(W\) never rises, but the algorithm can stop at a local minimum, so it is run from many starts.

Lloyd’s algorithm

Three slices, not three species

\(W\) counts a millimetre of length like a millimetre of depth, so k-means favours round clusters of one size, like the dashed circles.

Within each species, bill length varies two to three times as much as depth: a long, flat ellipse.

Both axes have the same scale, so round looks round.

How many clusters?

More clusters always lower \(W\).

Look for an elbow: here at two, the Adelie against the rest, with a smaller step to three.

The plot suggests a number but does not decide it.

4 Model-based clustering

A mixture of two normal densities

The flipper length of the Adelie and Gentoo. Group \(l\) is normal, with density \(f_l\), mean \(\mu_l\) and variance \(\sigma_l^2\):

\[f(x)=\pi_1f_1(x)+\pi_2f_2(x)\]

The membership probability

\[P(g_t=l\mid x_t)=\frac{\pi_lf_l(x_t)}{f(x_t)}\]

is the share of group \(l\) among penguins with flipper length \(x_t\).

The EM algorithm

The shares, means and variances maximise the log-likelihood \(\ln L=\sum_t\ln f(x_t)\). EM repeats two steps:

  1. E-step: compute every \(P(g_t=l\mid x_t)\).
  2. M-step: update the parameters, each observation weighted by its membership probability:

\[ \pi_l=\frac1n\sum_tP(g_t=l\mid x_t),\qquad \mu_l=\frac{\sum_tP(g_t=l\mid x_t)\,x_t}{\sum_tP(g_t=l\mid x_t)}, \] \[ \sigma_l^2=\frac{\sum_tP(g_t=l\mid x_t)\,(x_t-\mu_l)^2}{\sum_tP(g_t=l\mid x_t)} \]

EM is k-means with soft assignments, and \(\ln L\) never falls.

EM at work

Three ellipses

With \(p\) variables, \(f_l\) is normal with mean vector \(\mu_l\) and covariance matrix \(\Sigma_l\): each group is an ellipse. The groups match the species for 97% of the penguins, k-means for 75%.

The shapes of the groups

Three letters, in this order:

  1. volume: the size
  2. shape: the stretch
  3. orientation: the tilt

Each letter is one of:

  • E: the same for all groups
  • V: varies between groups
  • I: round, or along the axes

For example, EVE: the same size, each group its own shape, the same tilt.

BIC picks the shape and the number

\[\text{BIC}=2\ln\hat L-(\text{number of parameters})\times\ln n\]

\(\ln\hat L\) is the largest log-likelihood of a model and \(n\) the number of observations. The largest BIC wins.

More parameters always fit at least as well: without the charge, VVV with many groups would win and overfit.

k-means is a mixture with round groups

With round groups of equal size and share, the shape EII: \(\Sigma_l=\sigma^2I\) (the same variance \(\sigma^2\) in every direction) and \(\pi_l=1/k\):

\[\pi_lf_l(\mathbf x_t)=\frac1k\,(2\pi\sigma^2)^{-p/2}\exp\!\Big(-\frac{\lVert\mathbf x_t-\mu_l\rVert^2}{2\sigma^2}\Big)\]

The most likely group is the one with the nearest mean, as in k-means.

  • The numbers of the groups can be swapped without changing the fit, as the factors can be turned.
  • Membership probabilities, like factor scores, are estimates for each observation, never certain.

5 All four measurements

Units matter for k-means

k-means is the mixture EII: round groups, measured by the Euclidean distance, where body mass in grams dominates. The mixture VEE gives all groups the same ellipse up to its size, which adapts to any units: its Mahalanobis distances do not depend on them, so it finds the same groups.

A fourth group

BIC picks four groups.

The fourth splits the Gentoo by sex: males are larger.

The model was never told the sex.

6 The US states

Three clusters of states

Six indicators, standardised, k-means with \(k=3\). The data find the groups, and we name them from the means.

Do other methods agree?

Every method finds the South. How the other states are grouped depends on the method.

Ward’s method gives the k-means clusters too, but it minimises the same \(W\), so it is no independent check.

Clusters and factors

Factor analysis Model-based clustering
Latent variable continuous factors \(\mathbf f_t\) a discrete group \(g_t\)
Model \(\mathbf x_t=\Lambda\mathbf f_t+\mathbf u_t\) \(\mathbf x_t=\mu_{g_t}+\mathbf u_t\)
What it groups variables observations
Not identified the turn of the factors the numbers of the groups
For each observation factor scores membership probabilities
How many? likelihood ratio test BIC

Takeaways

  • A cluster model has a discrete latent variable, the group \(g_t\) of each observation.
  • Hierarchical clustering merges step by step, and the linkage matters: single linkage chains.
  • k-means minimises \(W\). It favours round clusters, depends on the units and needs many starts.
  • Mixtures give membership probabilities and ellipses, estimated by EM. BIC chooses the shape and the number of groups. k-means is the case of round groups.
  • A method always returns groups. Groups that several methods find deserve more trust.

Cluster analysis finds groups of observations that lie together. A model adds probabilities, shapes and a way to choose their number.