Activity 30 — PCA III: Two Possums, and PERMANOVA
Can nine body measurements find a species split nobody had named yet?
Hands-on companion to the possum PCA lecture. Join a site table onto the 104 possums from Module 24, map the seven sites with leaflet, run a PCA on nine body measurements, name PC1 (size) and PC2 (ears-and-feet versus tail), see PC2 separate the two species with no overlap, confirm it from the raw ear lengths, then test the difference with PERMANOVA (vegan::adonis2) and check equal spread with betadisper.
In-class Activity 30: Two Possums, and PERMANOVA 🔴
Today’s Objectives
- Map the seven possum sites
- Run a PCA on nine body measurements
- Name PC1 and PC2 from the loadings
- See which axis splits the two species — and check it in the raw data
- Test the difference with PERMANOVA and check its assumption
install.packages("vegan")- Skeleton:
30_pca_possum_skeleton.R→ save it asscripts/30_pca_possum.R - Data (both into your
data/folder):possum.csv— the same 104 possums as Module 24possum_sites.csv— one row per site: name, state, species, coordinates
- Each numbered section of your skeleton has an EXAMPLE in the comments, written with made-up names like
my_data. Read it, then type the real version under Your turn. - ▶ Type this: the code you should end up with. Check yours against it if you get stuck.
- ▶ Already in your skeleton: just run it and read the output.
- 💭 Think lines are for talking over. Nothing to write down.
Part 1 · Libraries and data
▶ Already in your skeleton (section 1) — just run it:
library(tidyverse)
library(plotly)
library(ggrepel)
library(leaflet)
library(vegan)
sites <- read_csv("data/possum_sites.csv")
possum_df <- read_csv("data/possum.csv") %>%
left_join(sites, by = "site") %>%
drop_na(foot_l)left_join()copies each site’s name, state, species and coordinates onto every possum.drop_na(foot_l)drops the one possum with no foot measurement — PCA cannot handleNA. That leaves 103.- The
speciescolumn follows the 2002 split: the two Victorian sites are Trichosurus cunninghami, the mountain brushtail possum; the five NSW/Queensland sites are T. caninus, the short-eared possum. In 1995, when these animals were measured, they were all one species.
Part 2 · Map the seven sites
▶ Already in your skeleton (section 2): just run it:
species_pal <- colorFactor(c("darkorange", "purple"),
domain = sites$species)
leaflet(sites) %>%
addTiles() %>%
addCircleMarkers(~longitude, ~latitude,
color = ~species_pal(species),
label = ~site_name,
radius = 8, fillOpacity = 0.8) %>%
addLegend(pal = species_pal, values = ~species)- Hover over each dot for its name. Purple = mountain brushtail (Victoria), orange = short-eared (north).
- The species live in different places — but that alone does not make them different species. We want the bodies to show it.
Part 3 · Nine measurements
▶ Type this (section 3):
possum_meas <- possum_df %>%
select(head_l:belly)
round(cor(possum_meas), 2)head_l:bellymeans “every column fromhead_ltobelly”: the nine measurements, in order.- Only measurements go in — no site, no species, no sex.
- Two families of correlation:
- size — head, skull, total length, chest, belly all go up together
- ears and feet —
foot_l–ear_l= 0.78, the strongest in the table
ear_l–tail_l= −0.40: long ears tend to come with short tails.
💭 Think: Two families of correlation — how many big PCA axes would you predict?
Part 4 · Run the PCA
▶ Type this (section 4):
possum_pca <- prcomp(possum_meas, center = TRUE, scale. = TRUE)
summary(possum_pca)- PC1 = 43.8%, PC2 = 22.0%, together 65.8% of all the variation in nine columns.
scale. = TRUEmatters here: the measurements are in a mix of mm and cm.
Part 5 · The loadings
▶ Type this (section 5):
round(possum_pca$rotation[, 1:2], 2)| Axis | Loadings | Name |
|---|---|---|
| PC1 | all negative, −0.11 to −0.44; biggest head_l, total_l, chest |
overall size |
| PC2 | ear_l +0.65, foot_l +0.52 vs tail_l −0.46 |
shape: ears-and-feet ↔︎ tail |
All the same sign → a “total” axis, just like Pokémon PC1. Here a big possum gets a negative PC1. The sign of a PC is arbitrary — your computer may even print them all positive. Read which loadings share a sign, not whether it is + or −.
Part 6 · The big reveal
▶ Type this (section 6):
possum_scores_df <- bind_cols(possum_df, as_tibble(possum_pca$x))
ggplot(possum_scores_df, aes(x = PC1, y = PC2, colour = species)) +
geom_point(size = 2) +
coord_equal()bind_cols()glues the scores (PC1toPC9) onto the possum table, so every possum keeps its site, sex and species next to its scores.Up–down (PC2, shape): two clouds with a gap between them.
Left–right (PC1, size): both species spread across the whole width.
PCA never saw the
speciescolumn. It found the split from the measurements alone.
💭 Think: PC1 is the biggest axis. Why is it not the one that splits the species?
Part 7 · Prove the gap, then check the raw data
▶ Type this (section 7):
possum_scores_df %>%
group_by(species) %>%
summarize(n = n(),
mean_PC1 = mean(PC1),
min_PC2 = min(PC2),
max_PC2 = max(PC2),
ear_l = mean(ear_l),
foot_l = mean(foot_l),
tail_l = mean(tail_l),
total_l = mean(total_l))- One table, two checks: the scores (PC1, PC2) and the raw measurements (ears, feet, tail, length).
- Highest short-eared possum on PC2: −0.12. Lowest mountain brushtail: +0.17. No overlap.
- In the raw data, the short-eared possum really does have shorter ears: 44.9 vs 52.5 mm. Feet 7 mm shorter, tail 2 cm longer.
- Body length is almost the same (86.8 vs 87.6 cm) — which is why PC1 (size) cannot split them.
- Read the loadings → PC2 is ears-and-feet vs tail
- Check against the raw data → the short-eared possum has short ears ✅
Part 8 · Hover to find the sites
▶ hover_plot is already in your skeleton (section 8). Type the ggplotly() line under it:
hover_plot <- ggplot(possum_scores_df,
aes(x = PC1, y = PC2, colour = species,
text = paste(site_name, sex, "· animal", animal))) +
geom_point(size = 2) +
coord_equal()ggplotly(hover_plot, tooltip = "text")text =sets what shows up when you hover;tooltip = "text"tellsggplotly()to show only that.Hover along the left edge (biggest) and the right edge (smallest).
Cambarville and Bellbird are both Victorian mountain brushtails — find them on PC1.
💭 Think: Size changes from site to site, but shape stays with the species. Why might that be?
Part 9 · The biplot
▶ Already in your skeleton (section 9): just run it:
arrows_df <- as_tibble(possum_pca$rotation[, 1:2], rownames = "measure") %>%
mutate(PC1 = PC1 * 4,
PC2 = PC2 * 4)
ggplot(possum_scores_df, aes(x = PC1, y = PC2)) +
geom_point(aes(colour = species), alpha = 0.5) +
geom_segment(data = arrows_df,
aes(x = 0, y = 0, xend = PC1, yend = PC2),
arrow = arrow(length = unit(0.2, "cm"))) +
geom_text_repel(data = arrows_df,
aes(x = PC1, y = PC2, label = measure), size = 3.2) +
coord_equal()- Every arrow points left — every measurement grows towards big possums.
ear_landfoot_lpoint up into one species;tail_lpoints down into the other.- The size measurements point straight left — no help telling species apart.
* 4only makes the arrows visible; it never changes their direction.
Part 10 · PERMANOVA
PCA draws; it does not test. PERMANOVA (permutational multivariate ANOVA) asks: are possums of the same species closer to each other, across all nine measurements, than chance would give?
▶ Type this (section 10). New function, so read the EXAMPLE in your skeleton first:
possum_dist <- dist(scale(possum_meas))
set.seed(42)
adonis2(possum_dist ~ species, data = possum_df, permutations = 999)scale()puts every measurement on the same footing (the same thingscale. = TRUEdid);dist()measures how far apart every pair of possums is in 9-D.adonis2()computes an F like ANOVA, then shuffles the species labels 999 times to see how big F gets by chance.set.seed(42)makes your shuffles match the lecture’s.
| Result | Value | Meaning |
|---|---|---|
| F | 25.4 | species differ a lot relative to within-species variation |
| R² | 0.20 | species explains 20% of all variation |
| p | 0.001 | no shuffle reached F = 25.4 — the smallest p 999 shuffles can give |
💭 Think: PC2 split the species perfectly, yet species explains only 20%. Where did the other 80% go? (Hint: what is PC1?)
Part 11 · Check the spread
PERMANOVA does not need normality, but it does need the groups to have similar spread — the multivariate version of Levene’s test from Module 24.
▶ Type this (section 11):
possum_disp <- betadisper(possum_dist, possum_df$species)
set.seed(42)
permutest(possum_disp)- p = 0.078 → above 0.05 → spreads are not significantly different ✅
- It passes, but not by much: the short-eared possums (five sites) are a bit more variable. Say so in a write-up.
Part 12 · The write-up
Nine body measurements from 103 brushtail possums at seven sites were standardised and reduced by principal component analysis. The first two components explained 65.8% of the variance (PC1 43.8%, PC2 22.0%). PC1 loaded in the same direction on all nine measurements and is interpreted as overall body size; it did not differ between species. PC2 contrasted ear length (+0.65) and foot length (+0.52) with tail length (−0.46), and separated Trichosurus cunninghami (Victoria) from T. caninus (NSW and Queensland) with no overlap: short-eared possums averaged 44.9 mm ears against 52.5 mm. A PERMANOVA on Euclidean distances of the standardised measurements confirmed that the species differ (F₁,₁₀₁ = 25.4, R² = 0.20, p = 0.001, 999 permutations); group dispersions did not differ significantly (betadisper, p = 0.078).
“PCA proves these are two species” — no. Species are decided by genetics, breeding and shape together. We showed the shape part: “the two species differ consistently in body shape.”
Does sex matter as well as species? Add it to the PERMANOVA:
set.seed(42)
adonis2(possum_dist ~ species + sex, data = possum_df, by = "margin")Then colour the Part 6 plot by sex instead of species. Do males and females separate on either axis?
Sources
- Lindenmayer DB et al. (1995) Morphological variation among populations of the mountain brushtail possum. Australian Journal of Zoology 43: 449–458. doi:10.1071/ZO9950449
- Lindenmayer DB, Dubach J, Viggers KL (2002) Geographic dimorphism in the mountain brushtail possum: the case for a new species. Australian Journal of Zoology 50: 369–393. doi:10.1071/ZO01047