Practice Session & Exercise

Session 5 :: PCA & Hierarchical Clustering

Author
Affiliation

Bioinformatics Core Facility CECAD

Published

September 25, 2026

1 Intermediate R-Course 2026

2 Session 5 Practice: PCA & Hierarchical Clustering

Every exercise uses the same iris dataset (150 flowers, 3 species: setosa, versicolor, virginica; 4 measurements in cm: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), but each one asks something new: comparing variable subsets, projecting a brand-new flower, picking the number of clusters without knowing the species count, changing the distance metric, and finding exactly which flowers a clustering gets “wrong”. Try each task before you open the solution.

Not everyone works through exercises at the same pace, so each one below is flagged — but the exercises build on each other (later ones reuse objects like hc and cl from earlier ones), so everyone still works through them in order, together as a group. The flags aren’t an invitation to skip ahead; they tell you what to expect, and what to do if you’re falling behind:

  • 🟢 Easy — everyone should get through these by writing the code themselves.
  • 🟡 Medium — a bit harder, or introduces something new; keep typing if you’re keeping pace, but copying from the solution is fine too.
  • 🔴 Challenge — the most demanding exercises, including today’s only new programming idea (sapply()). If you’re falling behind, copy the code from the solution instead of typing it yourself — you’ll need the objects it creates for the exercises that follow — then move on together with the group.

2.1 Session 5

2.1.1 Exercise 1: How many variables do you actually need? 🟢 Core

The slides ran PCA on all four iris measurements together. Here, compare that to PCA on only the two petal measurements (Petal.Length, Petal.Width).

  • Run prcomp(..., center = TRUE, scale. = TRUE) on iris[, 1:4] (all four measurements) and again on iris[, 3:4] (petals only).
  • For each one, work out the percentage of variance PC1 alone explains.
  • Why does PC1 explain so much more when you only use the two petal columns?

2.1.2 Exercise 2: Where would a new flower land? 🟡 Stretch

predict() also works on a prcomp object: it projects a new observation onto the PC axes you already learned from iris, without re-running the PCA. The slides never showed this, since they only ever plotted the 150 training flowers.

Make up one new flower: Sepal.Length = 6.1, Sepal.Width = 2.8, Petal.Length = 4.7, Petal.Width = 1.2 (all in cm, same units as iris). Project it into pca_all’s PC1–PC2 space with predict(pca_all, newdata = ...). Then compare its PC1 score to the typical PC1 range for each species (group the scores by species and summarise the min and max of PC1 with dplyr). Which species does it look most like?

2.1.3 Exercise 3: Picking k without knowing the species count 🔴 Challenge

The slides always cut the tree at k = 3, because we already know iris has three species. In practice you rarely know the “true” number of groups ahead of time.

Build hc <- hclust(dist(scale(iris[, 1:4])), method = "ward.D2"), then work out the mean silhouette width (same as in the slides) for every k = 2, 3, 4, 5, 6. Which k do the numbers alone recommend? Does it match the known 3 species? If not, why, given how separated the three species looked in Exercise 1–2’s PCA?

2.1.4 Exercise 4: Does the distance metric matter? 🟢 Core

Every distance in the slides was Euclidean (dist()’s default): straight- line distance through 4D space. dist(x, method = "manhattan") instead adds up the absolute differences on each variable, like walking a city grid instead of cutting straight across a block. The slides never used this one.

Re-cluster the scaled iris data with Manhattan distance and ward.D2 linkage, cut at k = 3, and cross-tabulate it against the Euclidean-based cl from Exercise 3. Do the two metrics agree?

2.1.5 Exercise 5: Capstone — which flowers does the clustering get “wrong”? 🔴 Challenge

The slides’ combined PCA + clustering plot shows clusters and species side by side, but it never asks which individual flowers disagree between the two. For each cluster, find its majority species (the species most of its flowers belong to). Then flag every flower whose own species isn’t that majority — these are the points the clustering effectively “gets wrong” compared to the known labels.

Build a PCA plot (PC1 vs PC2, colour = cluster), circle the misclassified points in black so they stand out, then save it as a PNG at 300 dpi and as an RDS file, the same way the slides showed for ggsave() and saveRDS().

2.2 Exit ticket

  1. What does predict() do differently from re-running prcomp() on new data?
  2. Why might the “best” k by silhouette not match the known number of species?
  3. Name one thing that changed when you swapped Euclidean for Manhattan distance.

Sources: prcomp() documentation, hclust() documentation, and cluster::silhouette() documentation.