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)Lecture 30 — PCA III: Two Possums, and PERMANOVA
Can nine tape-measure numbers find a species nobody had named yet?
PCA on a real biological question. The 104 brushtail possums from the one-factor ANOVA module were measured in 1995 as one species; in 2002 they were split into two. Nine body measurements go into a PCA with no species label: PC1 turns out to be overall size and does not separate them, PC2 is a long-ears-and-feet versus long-tail shape axis and separates them with no overlap at all. Then PERMANOVA (vegan::adonis2) tests the difference across all nine measurements at once, betadisper checks the one assumption that matters, and the results are written up.
Where We Left Off
- Module 24 — possums: one-factor ANOVA
- 104 possums, 7 sites, one measurement at a time
- total length, then ears for homework
- ears split the Victorian sites from the northern ones
- Modules 27–28 — Pokémon: PCA
- six stats → two axes
- name the axis from the loadings, confirm from the extremes
- colour by a group after the PCA, never put it in
✅ Today puts them together
- the same possums
- all nine measurements at once
- PCA to see the groups
- then a new test, PERMANOVA, to ask if the groups really differ
Goals for Today
- Meet the two species hiding in the possum data
- Map where they were caught
- Run a PCA on nine body measurements
- Name PC1 and PC2 — and find which one splits the species
- Check the PCA against the raw measurements
- Test the difference with PERMANOVA (
adonis2()) - Check its one assumption with
betadisper() - Write it up
Tools today:
tidyverse,plotly,ggrepel,leafletvegan— install this
Data: 🔴 possum.csv + possum_sites.csv
⚠️ Install first
install.packages("vegan")🧩 Chunk 1 of 5 · Two Possums
We will cover: the animals, the 2002 species split, and where the seven sites are.
One Species in 1995 — Two by 2002
The Backstory
- 1995: Lindenmayer and colleagues measured possums at 7 sites
- all called Trichosurus caninus — one species
- this is our
possum.csv
- 2002: the same group published the case for two species
- genetics + body shape
- the Victorian animals became T. cunninghami
- Look at the common name of the northern one:
- short-eared possum 👂
✅ Today’s question
If we give PCA only the nine body measurements — no site, no state, no species — does it find the split on its own?
Lindenmayer et al. 1995 doi:10.1071/ZO9950449 · 2002 doi:10.1071/ZO01047
Read the Data
🧠 Two things you have seen before
left_join()(Module 19) adds each site’s name, state, species and coordinates to every possum — nocase_when()needed this timedrop_na(foot_l)drops the one possum with a missing foot. PCA cannot handle anNA
The Site Table
sites %>% select(site, site_name, state, species, latitude)# A tibble: 7 × 5
site site_name state species latitude
<dbl> <chr> <chr> <chr> <dbl>
1 1 Cambarville Vic cunninghami -37.6
2 2 Bellbird Vic cunninghami -37.6
3 3 Whian Whian NSW caninus -28.6
4 4 Byrangery NSW caninus -28.6
5 5 Conondale Qld caninus -26.4
6 6 Allyn River NSW caninus -32.1
7 7 Bulburin Qld caninus -24.6
- One row per site;
speciesfollows the 2002 split left_join()copies these columns onto all 103 possums
Where Were They Caught?
leaflet(sites) %>%
addTiles() %>%
addCircleMarkers(lng = ~longitude, lat = ~latitude,
label = ~site_name,
color = ~if_else(species == "caninus", "darkorange", "purple"))And Where the Species Live

- Small dots — ~3,000 sightings from GBIF (Module 20’s database)
- orange = short-eared, purple = mountain brushtail
- Big dots — our seven trapping sites
- The two species barely touch
- Victoria + southern NSW → mountain brushtail
- northern NSW + Queensland → short-eared
- Bellbird is ~650 km from the nearest northern site
⚠️ Geography is not evidence of a species
- Far-apart populations can be one species
- We want the bodies to show it — measurements only, into the PCA
🧩 Chunk 2 of 5 · The PCA
We will cover: nine measurements, the correlation table, and prcomp().
Nine Measurements
possum_meas <- possum_df %>%
select(head_l, skull_w, total_l, tail_l,
foot_l, ear_l, eye, chest, belly)| Column | What it is | Units |
|---|---|---|
head_l, skull_w |
head length, skull width | mm |
total_l, tail_l |
body length, tail length | cm |
foot_l, ear_l, eye |
foot, ear, eye | mm |
chest, belly |
girths | cm |
✅ What is NOT in there
- no
site,stateorspecies - no
sexorage
Same rule as the Pokémon: build the axes from measurements, then colour by the group afterwards.
- Units are mixed (mm and cm) →
scale. = TRUEis a must
Which Measurements Travel Together?
round(cor(possum_meas), 2) head_l skull_w total_l tail_l foot_l ear_l eye chest belly
head_l 1.00 0.72 0.69 0.29 0.39 0.10 0.36 0.64 0.56
skull_w 0.72 1.00 0.53 0.26 0.28 0.00 0.32 0.63 0.45
total_l 0.69 0.53 1.00 0.57 0.44 0.14 0.26 0.58 0.52
tail_l 0.29 0.26 0.57 1.00 -0.13 -0.40 0.20 0.18 0.29
foot_l 0.39 0.28 0.44 -0.13 1.00 0.78 0.01 0.45 0.30
ear_l 0.10 0.00 0.14 -0.40 0.78 1.00 -0.15 0.20 0.05
eye 0.36 0.32 0.26 0.20 0.01 -0.15 1.00 0.15 0.24
chest 0.64 0.63 0.58 0.18 0.45 0.20 0.15 1.00 0.61
belly 0.56 0.45 0.52 0.29 0.30 0.05 0.24 0.61 1.00
Two Families of Correlation
- Size family — head, skull, total length, chest, belly
head_l–skull_w0.72,head_l–total_l0.69- big possums are big all over
- Ear-and-foot pair —
foot_l–ear_l0.78- the strongest correlation in the table
- Ears vs tail —
ear_l–tail_l−0.40- long ears tend to go with short tails
🧠 Prediction time
- Lots of correlation → PCA will squeeze this well
- Two “families” → expect two big axes
- 🔮 Which family will be PC1?
Run the PCA
possum_pca <- prcomp(possum_meas, center = TRUE, scale. = TRUE)
summary(possum_pca)Importance of components:
PC1 PC2 PC3 PC4 PC5
Standard deviation 1.9859 1.4069 0.94399 0.85915 0.75083
Proportion of Variance 0.4382 0.2199 0.09901 0.08202 0.06264
Cumulative Proportion 0.4382 0.6581 0.75712 0.83913 0.90177
PC6 PC7 PC8 PC9
Standard deviation 0.55626 0.51648 0.40479 0.3795
Proportion of Variance 0.03438 0.02964 0.01821 0.0160
Cumulative Proportion 0.93615 0.96579 0.98400 1.0000
How Much Did We Keep?
- PC1 = 43.8% of all the variation
- PC2 = 22.0%
- Together 65.8% — two-thirds of nine columns on one flat plot
- PC3 drops to 9.9% — the first two do the heavy lifting
🧠 Compare with the Pokémon
| PC1 | PC2 | Both | |
|---|---|---|---|
| Pokémon (6) | 44.1% | 18.4% | 62.5% |
| Possums (9) | 43.8% | 22.0% | 65.8% |
Nine columns, and more kept.
🧩 Chunk 3 of 5 · Naming the Axes
We will cover: the loadings, a sign surprise, and the plot that answers today’s question.
The Loadings
round(possum_pca$rotation[, 1:2], 2) PC1 PC2
head_l -0.44 -0.05
skull_w -0.39 -0.10
total_l -0.42 -0.07
tail_l -0.21 -0.46
foot_l -0.28 0.52
ear_l -0.11 0.65
eye -0.19 -0.26
chest -0.41 0.09
belly -0.37 -0.06
PC1: All the Same Sign — Again
- Every PC1 loading is negative
- biggest:
head_l−0.44,total_l−0.42,chest−0.41 - all the same sign → a “total” axis
- biggest:
- PC1 = overall body size
- But the sign means:
- big possum → negative PC1
- small possum → positive PC1
⚠️ The sign of a PC is arbitrary
prcomp()picks a direction at random-ish- All −0.4 and all +0.4 describe the same axis, pointing the other way
- Your laptop might even give you the opposite signs to the slide
- Read which loadings share a sign, never whether the sign is + or −
PC2: Mixed Signs — a Trade-off
| Measurement | PC2 loading | |
|---|---|---|
ear_l |
+0.65 | ⬆️ |
foot_l |
+0.52 | ⬆️ |
chest |
+0.09 | |
head_l, total_l, belly |
≈ 0 | |
eye |
−0.26 | ⬇️ |
tail_l |
−0.46 | ⬇️ |
✅ Mixed signs → a trade-off
- high PC2 = long ears and feet
- low PC2 = long tail
- the size measurements sit near zero — PC2 is not about size at all
- PC2 is body shape: “ears-and-feet ↔︎ tail”
The Big Reveal
possum_scores <- possum_df %>%
mutate(PC1 = possum_pca$x[, 1],
PC2 = possum_pca$x[, 2])
ggplot(possum_scores, aes(x = PC1, y = PC2, color = species)) +
geom_point()
What You Are Looking At
- Up–down (PC2, shape): two clouds with a gap between them
- every mountain brushtail is above zero
- every short-eared possum is below zero
- Left–right (PC1, size): both species spread across the whole width
- big and small animals in both species
- PCA never saw the word
species
✅ The answer to today’s question
- Size does not tell the two species apart
- Shape does — perfectly
- The biggest axis (PC1, 43.8%) is not the interesting one here
- PC1 is not automatically the answer
Prove the Gap
possum_scores %>%
group_by(species) %>%
summarize(n = n(),
mean_PC1 = mean(PC1),
min_PC2 = min(PC2),
max_PC2 = max(PC2))# A tibble: 2 × 5
species n mean_PC1 min_PC2 max_PC2
<chr> <int> <dbl> <dbl> <dbl>
1 caninus 58 0.262 -2.45 -0.118
2 cunninghami 45 -0.337 0.173 2.59
- Highest short-eared: −0.12 · lowest mountain brushtail: +0.17
- No overlap — not one animal on the wrong side
- PC1 means: 0.26 vs −0.34 — almost the same size
Back to the Raw Data
possum_df %>%
group_by(species) %>%
summarize(ear_l = mean(ear_l),
foot_l = mean(foot_l),
tail_l = mean(tail_l),
total_l = mean(total_l))# A tibble: 2 × 5
species ear_l foot_l tail_l total_l
<chr> <dbl> <dbl> <dbl> <dbl>
1 caninus 44.9 65.4 37.9 86.8
2 cunninghami 52.5 72.4 35.9 87.6
✅ Step 2 of the two-step: check the extremes against what you know
- The short-eared possum has ears 7.6 mm shorter (44.9 vs 52.5) and feet 7 mm shorter
- Its tail is 2 cm longer — the low end of PC2
- Body length is the same (86.8 vs 87.6 cm) — which is why PC1 cannot split them
🧩 Chunk 4 of 5 · Explore It
We will cover: hover to find the sites, and a biplot of all nine arrows.
Hover: Which Site Is Which?
hover_plot <- ggplot(possum_scores,
aes(x = PC1, y = PC2, color = species,
text = paste(site_name, sex, "· animal", animal))) +
geom_point()
ggplotly(hover_plot, tooltip = "text")What the Hover Shows
- Far left (biggest): Byrangery — module 24’s longest possums (92.2 cm)
- Far right (smallest): Bellbird and Allyn River
- Bellbird and Cambarville are both Victorian, both mountain brushtail
- at opposite ends of PC1
- same height on PC2
🧠 Size is a site thing, shape is a species thing
- Size changes from forest to forest — food, climate, chance
- Shape stays with the species wherever it lives
- That is the pattern the 2002 paper leaned on
The Biplot
arrows_df <- as_tibble(possum_pca$rotation[, 1:2], rownames = "measure") %>%
mutate(PC1 = PC1 * 4,
PC2 = PC2 * 4)
ggplot(possum_scores, aes(x = PC1, y = PC2)) +
geom_point(aes(color = 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(label = measure), size = 3.2)
Reading the Arrows
- Every arrow points left — every measurement grows towards big possums
ear_landfoot_lpoint up, towards the mountain brushtails- close together → correlated (0.78)
tail_lpoints down, towards the short-eared possumshead_l,skull_w,total_l,chest,bellypoint straight left- pure size, no help with species
✅ Same rules as the Pokémon
- arrows close together → correlated
- at right angles → unrelated
- opposite → trade-off
And report the shadow: PC1 + PC2 = 65.8%
🧩 Chunk 5 of 5 · PERMANOVA
We will cover: turning “it looks different” into a test.
PCA Describes. It Does Not Test.
- The plot looks convincing
- But a reviewer will ask: “Could two random groups look that different?”
- PCA has no p-value — it only draws
- Module 24 answered that kind of question with ANOVA
- but ANOVA takes one response at a time
- we have nine
🧠 PERMANOVA
PERMutational Multivariate ANOVA
- multivariate — all nine measurements at once
- permutational — gets its p-value by shuffling, not from an F table
The Idea in Three Steps
- Distance — how different is every possum from every other, across all nine measurements?
- F — are possums of the same species closer together than possums of different species?
- same idea as ANOVA: between-group vs within-group
- Shuffle — scramble the species labels 999 times, recompute F each time
- p = how often a shuffle beats the real F
🎯 Why shuffle?
- If species did not matter, the labels would be arbitrary
- Shuffling them shows what F looks like by chance
- No normality assumption needed — the shuffles are the reference
Step 1 · Distances
possum_dist <- dist(scale(possum_meas))scale()— the same standardisingscale. = TRUEdid insideprcomp()- every measurement gets mean 0, sd 1
- so millimetres and centimetres count equally
dist()— the straight-line distance between every pair of possums in 9-D- 103 possums → 5,253 pairs
⚠️ Forget scale() and the test changes
Raw dist() lets the measurements with the biggest numbers (head length, ~90 mm) drown out the small ones (eye, ~15 mm).
Step 2 + 3 · adonis2()
set.seed(42)
adonis2(possum_dist ~ species, data = possum_df, permutations = 999)Permutation test for adonis under reduced model
Permutation: free
Number of permutations: 999
adonis2(formula = possum_dist ~ species, data = possum_df, permutations = 999)
Df SumOfSqs R2 F Pr(>F)
Model 1 184.22 0.20068 25.357 0.001 ***
Residual 101 733.78 0.79932
Total 102 918.00 1.00000
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Reading the Table
- F = 25.4 — between-species distance is large next to within-species
- R² = 0.20 — species explains 20% of all the variation in nine measurements
- p = 0.001 — none of the 999 shuffles reached 25.4
- with 999 shuffles, 0.001 is the smallest p possible
🧠 set.seed(42)
- shuffling is random
set.seed()makes your random shuffles the same as mine- without it, p might come out 0.001 one run and 0.002 the next
What the Shuffles Looked Like

- Grey = F when species labels are scrambled — the biggest of 999 was 5.9
- Red = the real F. Nowhere near the shuffles
Why Only R² = 0.20?
- PC2 separated the species perfectly… so why does species explain only 20%?
- Because PERMANOVA uses all nine dimensions
- PC1 (size) is the biggest source of variation — 43.8%
- and size has nothing to do with species
- Most of the distance between two possums is about size
✅ Two true statements
- Species explains only a fifth of the total variation
- Species separates the animals completely on body shape
A small R² does not mean a weak result. It means other things vary too.
The One Assumption: Equal Spread
- PERMANOVA does not care about normality
- It does care whether the groups have similar spread
- one tight cloud + one loose cloud can fool it
- it would call that “different” when only the spread differs
betadisper()= the multivariate Levene’s test
🧠 Same logic as Module 24
| ANOVA | PERMANOVA |
|---|---|
leveneTest() |
betadisper() |
| equal variance | equal spread |
| want p > 0.05 | want p > 0.05 |
Check the Spread
possum_disp <- betadisper(possum_dist, possum_df$species)
set.seed(42)
permutest(possum_disp)
Permutation test for homogeneity of multivariate
dispersions
Permutation: free
Number of permutations: 999
Response: Distances
Df Sum Sq Mean Sq F N.Perm Pr(>F)
Groups 1 3.81 3.8098 3.3197 999 0.078 .
Residuals 101 115.91 1.1476
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Reading the Spread Check
- p = 0.078 → above 0.05 → spreads are not significantly different ✅
- Average distance to the group centre:
- short-eared 2.62, mountain brushtail 2.23
- short-eared a bit more variable — five sites, ~850 km of coast
- Passes, but not by a mile — say so in the write-up
🎯 Bonus: does sex matter too?
adonis2(possum_dist ~ species + sex,
data = possum_df, by = "margin")- species: F = 25.0, p = 0.001
- sex: F = 1.6, p = 0.19 — no
Writing It Up
📝 The model paragraph
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).
What That Paragraph Does
- PCA part: what went in, standardised, % explained, names with loadings, evidence from the raw data
- PERMANOVA part: distance used, F with both df, R², p, number of permutations
- Assumption reported — with its p
⚠️ Don’t over-claim
Not OK: “PCA proves these are two species.”
OK: “The two species differ consistently in body shape.”
Species are decided by genetics, breeding and shape together. We showed the shape part.
Recap
- The 1995 possums are two species — split in 2002
- PC1 (43.8%) = size — all loadings one sign; it does not separate the species
- PC2 (22.0%) = shape — ears + feet vs tail; it separates them with no overlap
- The short-eared possum really does have shorter ears — check the raw data
- The sign of a PC is arbitrary; read which loadings share a sign
- PERMANOVA = ANOVA on distances, p-value by shuffling
dist(scale(...))→adonis2()→betadisper()
- R² = 0.20 but perfect separation — both true; size takes the rest
✅ Next
Open the activity and type it in — skeleton sections match the activity Parts. 🔴

