owls_t <- c(young = 250, subadult = 31, adult = 500)
owls_t young subadult adult
250 31 500
EEE330 Population Ecology · Practical 2
Yesterday every whooping crane counted the same, a chick as much as a breeding adult. This morning, in L03, you built the life cycle and matrix of the northern spotted owl. This afternoon you put that matrix to work in R: project the owls, see what settles, find the same answers with one function, and ask yesterday’s question again: how sure can we be?
Keep this file and its data folder together in the P02 folder, as they came. Open it in RStudio as you did in P1, and set the working directory to the source file location (Session › Set Working Directory › To Source File Location).
Write your predictions and answers into this file as you go. At the end, render it and upload it as Lastname_P02.html to your Participant Drop Box in OLAT (Practica / Exercises › Participant Folder), as in P1. It counts towards participation, not for being right; it is your own record, and the rotifer report later builds on what you do here.
Core and optional. Parts 1, 2, 3 and 4 are the core of today’s practical. Parts 5 and 6 are optional: do them if you have time, now or later.
A population with stages is a vector: one number per stage. These are the owls of L03 in year \(t\):
owls_t <- c(young = 250, subadult = 31, adult = 500)
owls_t young subadult adult
250 31 500
c() combines values into a vector; the names make the output easier to read. matrix() builds a matrix. It fills column by column unless told otherwise; byrow = TRUE fills it row by row, the way it is written on paper. Here is the owl matrix from this morning:
stages <- c("young", "subadult", "adult")
A <- matrix(c(0, 0, 0.226,
0.108, 0, 0,
0, 0.71, 0.942),
nrow = 3, byrow = TRUE,
dimnames = list(to = stages, from = stages))
A from
to young subadult adult
young 0.000 0.00 0.226
subadult 0.108 0.00 0.000
adult 0.000 0.71 0.942
Read the matrix by rows: each row says where next year’s individuals of one stage come from; each column says what one individual of a stage this year contributes to every stage next year.
In L03 you projected the owls one year ahead by hand. In matrix form this is \(\mathbf{A}\,\mathbf{n}_t = \mathbf{n}_{t+1}\). In R, matrix multiplication is %*%. (Plain * would multiply element by element, which is something else.)
A %*% owls_t
to [,1]
young 113.00
subadult 27.00
adult 493.01
This is the hand calculation of this morning: 113 young, 27 subadults and 493 adults.
A matrix times a vector works when the matrix has as many columns as the vector has entries. Here \(3 \times 3\) times \(3 \times 1\) gives \(3 \times 1\): next year’s three stages. Check with dim(A) and length(owls_t).
The numbers in the matrix are not all vital rates themselves. Two of them are products of rates that biologists estimate and that managers can change separately (L03):
A small function builds the matrix from the vital rates, so that you can change one rate and let the matrix follow:
owl_matrix <- function(fledgling_survival = 0.60,
dispersal_success = 0.18,
subadult_survival = 0.71,
adult_survival = 0.942,
fecundity = 0.24) {
young_survival <- fledgling_survival * dispersal_success
recruitment <- adult_survival * fecundity
matrix(c(0, 0, recruitment,
young_survival, 0, 0,
0, subadult_survival, adult_survival),
nrow = 3, byrow = TRUE,
dimnames = list(to = stages, from = stages))
}
owl_matrix() from
to young subadult adult
young 0.000 0.00 0.22608
subadult 0.108 0.00 0.00000
adult 0.000 0.71 0.94200
Called without arguments, it uses the estimates of L03 and gives the same matrix as A. Called with one rate changed, it gives the matrix of a population with that one difference:
owl_matrix(dispersal_success = 0.30) from
to young subadult adult
young 0.00 0.00 0.22608
subadult 0.18 0.00 0.00000
adult 0.00 0.71 0.94200
Your answer:
# your code hereSolution.
owl_matrix(adult_survival = 0.952) - owl_matrix() from
to young subadult adult
young 0 0 0.0024
subadult 0 0 0.0000
adult 0 0 0.0100
Dispersal success changes only young survival (row 2, column 1). Adult survival changes two entries: adults staying adults (row 3, column 3), and recruitment (row 1, column 3), because with a census just after breeding an adult can only produce next year’s young if it survives to breed. A change in one vital rate can therefore move λ through more than one entry, a point L05 comes back to.
Often missed. Adult survival changes two entries, adult → adult and adult → young; some answers found only one. Dispersal success changes the young → subadult rate, the share of young that become subadults, not the number of subadults.
Before you project: will this owl population grow or decline? Look at the matrix and the life cycle, not at a calculator. And will the share of young, subadults and adults stay as it is now (250, 31, 500)?
Your prediction:
A for loop repeats the step for 50 years. Each year’s numbers go into one row of a table N, which starts with the owls of year \(t\):
years <- 50
N <- matrix(NA, nrow = years + 1, ncol = 3, dimnames = list(NULL, stages))
N[1, ] <- owls_t
for (t in 1:years) {
N[t + 1, ] <- A %*% N[t, ]
}
head(N) young subadult adult
[1,] 250.0000 31.00000 500.0000
[2,] 113.0000 27.00000 493.0100
[3,] 111.4203 12.20400 483.5854
[4,] 109.2903 12.03339 464.2023
[5,] 104.9097 11.80335 445.8223
[6,] 100.7558 11.33025 428.3450
To plot it with ggplot(), turn the table into a tibble with a column for the year, and then into “long” form, one row per year and stage, with pivot_longer():
projection <- as_tibble(N) |>
mutate(year = 0:years) |>
pivot_longer(cols = young:adult, names_to = "stage", values_to = "number") |>
mutate(stage = factor(stage, levels = stages))
projection# A tibble: 153 × 3
year stage number
<int> <fct> <dbl>
1 0 young 250
2 0 subadult 31
3 0 adult 500
4 1 young 113
5 1 subadult 27
6 1 adult 493.
7 2 young 111.
8 2 subadult 12.2
9 2 adult 484.
10 3 young 109.
# ℹ 143 more rows
ggplot(projection, aes(x = year, y = number, colour = stage)) +
geom_line() +
scale_y_log10() +
labs(x = "Year", y = "Number of owls (log scale)", colour = "Stage")Now the share of each stage in each year, and the growth rate of the whole population from one year to the next, \(\lambda_t = N_{t+1}/N_t\), as in P1:
shares <- projection |>
group_by(year) |>
mutate(share = number / sum(number)) |>
ungroup()
ggplot(shares, aes(x = year, y = share, colour = stage)) +
geom_line() +
labs(x = "Year", y = "Share of the population", colour = "Stage")totals <- projection |>
group_by(year) |>
summarise(total = sum(number)) |>
mutate(lambda = lead(total) / total)
head(totals, 8)# A tibble: 8 × 3
year total lambda
<int> <dbl> <dbl>
1 0 781 0.811
2 1 633. 0.959
3 2 607. 0.964
4 3 586. 0.961
5 4 563. 0.961
6 5 540. 0.961
7 6 519. 0.961
8 7 499. 0.961
Your answer:
Solution. The owls decline. After a few years every stage and the total shrink by the same factor each year, 0.961, about 4% a year, and the lines on the log scale become parallel straight lines. The first year is different: \(\lambda_t\) is only 0.81 because the population started with many young, and only about one young owl in nine survives its first year. Within three or four years both \(\lambda_t\) and the shares settle: about 19% young, 2% subadults and 79% adults. The growth rate and the composition settle together, and both are properties of the matrix, not of the starting numbers. L04 takes this up.
Often missed. A constant factor of 0.96 a year is exponential decline: the owls lose a constant share (about 4%), not a constant number, and only on the log scale is that a straight line. Parallel straight lines there mean that every stage shrinks at the same rate, so the shares stay fixed. The drop in the first year comes from the start (many young, few of which survive), not from weather, which is not in the model. And recruitment (0.226) is the number of young per adult at the next census, not the share of adults that breed.
eigen() returnsYou met this in Introduction to Ecology (EEE102): λ of a Leslie matrix is its dominant eigenvalue. In Part 3 the shares of young, subadults and adults stopped changing after a few years, and from then on the population grew by the same factor every year. Once the shares have settled, one more year of the matrix changes the numbers but not their proportions: every stage is multiplied by the same factor, λ.
The settled shares are therefore a special set of proportions: the one set that the matrix only scales up or down, without reshuffling it. Such a vector is called an eigenvector of \(\mathbf{A}\), and the factor it is scaled by is its eigenvalue:
\[\mathbf{A}\,\mathbf{w} = \lambda\,\mathbf{w}\]
Read it as: one year of the matrix turns \(\mathbf{w}\) into \(\mathbf{w}\) again, only λ times as large. L04 (Thursday) explains why every population ends up there. R’s eigen() finds λ and \(\mathbf{w}\) directly, without projecting:
e <- eigen(A)
eeigen() decomposition
$values
[1] 0.960773636+0.0000000i -0.009386818+0.1339743i -0.009386818-0.1339743i
$vectors
[,1] [,2] [,3]
[1,] 0.22890171+0i -0.70714078+0.0000000i -0.70714078+0.0000000i
[2,] 0.02573071+0i 0.03974464+0.5672592i 0.03974464-0.5672592i
[3,] 0.97310942+0i 0.02937080-0.4191976i 0.02937080+0.4191976i
The output needs some care. There are as many eigenvalues as stages, and here two of them are complex numbers (the i part). They describe how the population approaches its settled state, not where it settles. The one that matters is the dominant eigenvalue: the one with the largest size, Mod(). Its eigenvector is the column of e$vectors in the same position.
dominant <- which.max(Mod(e$values))
dominant[1] 1
lambda <- Re(e$values[dominant]) # Re() keeps the real part
lambda[1] 0.9607736
w_raw <- Re(e$vectors[, dominant])
w_raw[1] 0.22890171 0.02573071 0.97310942
sum(w_raw)[1] 1.227742
Its entries do not add up to 1, so they are not yet shares: an eigenvector gives only the proportions between stages, and for other matrices R may even return it with all signs negative. Dividing by its sum turns it into shares, whatever its scale or sign:
w <- w_raw / sum(w_raw)
round(w, 3)[1] 0.186 0.021 0.793
Now check the claim: the matrix only rescales \(\mathbf{w}\), by the factor \(\lambda\).
cbind(A_times_w = A %*% w, lambda_times_w = lambda * w) lambda_times_w
young 0.17912783 0.17912783
subadult 0.02013565 0.02013565
adult 0.76151015 0.76151015
Compare lambda and w with the settled \(\lambda_t\) and shares of your projection.
Your answer:
Solution. lambda (0.9608) is the value \(\lambda_t\) settled to, and w the settled shares: the projection and the eigenvalue answer the same question.
Often missed. The comparison is: is lambda the value \(\lambda_t\) settled to in Part 3, and is w the settled shares of the three stages? The two agree to all printed digits, not just nearly. The adults are the large share, about 79%; the young about 19%.
You have finished the core. The optional parts below can wait; if you stop here, go straight to the Wrap-up at the end.
λ = 0.961 says the owls decline by about 4% a year. But the rates behind it were estimated from a limited number of banded birds (L03, counts hypothetical but built on Lande’s 1988 estimates):
| Stage in year \(t\) | Banded | Alive in year \(t+1\) | Young produced |
|---|---|---|---|
| young | 250 | 27 (as subadults) | |
| subadult | 31 | 22 (as adults) | |
| adult | 500 | 471 (still adults) | 113 |
A different sample of 250 young owls would not have given exactly 27 survivors. As in P1, we can find out how much the estimate could vary by simulating it: draw new counts many times from the estimated rates, rebuild the matrix from each set, and see how λ spreads.
banded <- c(young = 250, subadult = 31, adult = 500)
survived <- c(young = 27, subadult = 22, adult = 471)
young_produced <- 113
dominant_lambda <- function(A) {
max(Mod(eigen(A)$values))
}
resample_lambda <- function(banded, survived, young_produced) {
p <- survived / banded
# new counts of survivors: binomial, like coin flips with the estimated survival probability
s <- rbinom(3, size = banded, prob = p) / banded
# new count of young: Poisson, a common model for counts of offspring
recruitment <- rpois(1, lambda = young_produced) / banded["adult"]
A_new <- matrix(c(0, 0, recruitment,
s[1], 0, 0,
0, s[2], s[3]),
nrow = 3, byrow = TRUE)
dominant_lambda(A_new)
}Will the 95% interval of λ include 1, a stable population? Write down your guess before you run the next chunk.
Your prediction:
set.seed(1)
lambdas <- replicate(10000, resample_lambda(banded, survived, young_produced))
quantile(lambdas, c(0.025, 0.975)) 2.5% 97.5%
0.9390733 0.9815338
ggplot(tibble(lambda = lambdas), aes(x = lambda)) +
geom_histogram(bins = 50, fill = "grey70") +
geom_vline(xintercept = 1, linetype = "dashed") +
labs(x = expression(paste("Resampled ", lambda)), y = "Number of samples")Your answer:
# your code hereSolution.
set.seed(2)
lambdas_small <- replicate(10000, resample_lambda(
banded = c(young = 50, subadult = 6, adult = 100),
survived = c(young = 5.4, subadult = 4.26, adult = 94.2),
young_produced = 22.6))
quantile(lambdas_small, c(0.025, 0.975)) 2.5% 97.5%
0.909349 1.004054
With 781 banded owls the interval runs from about 0.94 to 0.98: it excludes 1, so the decline is real and not a fluke of sampling. With a study one fifth the size, the interval widens to about 0.909–1.004 and just includes 1: the same owls, but the data can no longer quite tell a decline from a stable population. A study smaller still would leave no doubt about that. (The survivor numbers are not whole here because they are the expected numbers at the same rates; rbinom() draws whole numbers anyway.) Real field studies of a rare, long-lived bird rarely band hundreds of owls of every stage, which is why Lande’s interval included 1 even though his best estimate pointed to a decline. As in P1: “not significantly different from stable” is not the same as “stable”.
If you have time: resample one rate at a time, keeping the others at their estimates, and compare how much λ varies. You will find that adult survival contributes most of the uncertainty in λ, even though its estimate is the most precise of the four. Why could that be? Keep the question for L05.
Solution.
one_rate_sd <- function(which_rate, sims = 5000) {
sd(replicate(sims, {
p <- survived / banded
s <- p
recruitment <- young_produced / banded["adult"]
if (which_rate == "recruitment") {
recruitment <- rpois(1, young_produced) / banded["adult"]
} else {
s[which_rate] <- rbinom(1, banded[which_rate], p[which_rate]) / banded[which_rate]
}
dominant_lambda(matrix(c(0, 0, recruitment, s[1], 0, 0, 0, s[2], s[3]), nrow = 3, byrow = TRUE))
}))
}
set.seed(3)
sapply(c("young", "subadult", "adult", "recruitment"), one_rate_sd) young subadult adult recruitment
0.003259624 0.002084520 0.010177308 0.001722073
Adult survival contributes most: λ responds strongly to it, so even a small uncertainty in it moves λ a lot. How strongly λ responds to each rate is its sensitivity, the subject of L05.
If you have time left, analyse the teasel of L03 on your own. The file data/teasel-matrix.csv holds its six-stage matrix (Werner and Caswell 1977): dormant seeds of one and two years, small, medium and large rosettes, and flowering plants. Read it with read_csv("data/teasel-matrix.csv", comment = "#"); the first column names the rows, so turn the rest into a matrix with as.matrix(select(teasel, -to)).
eigen() for λ and the stable structure, with the same care as for the owls. How many of the eigenvalues are complex?# your code hereSolution.
teasel <- read_csv("data/teasel-matrix.csv", comment = "#")
A_teasel <- as.matrix(select(teasel, -to))
rownames(A_teasel) <- teasel$to
e_teasel <- eigen(A_teasel)
e_teasel$values[1] 2.3338802+0.0000000i -0.9569213+1.4942451i -0.9569213-1.4942451i
[4] 0.1186947+0.1953115i 0.1186947-0.1953115i -0.1274269+0.0000000i
dominant_teasel <- which.max(Mod(e_teasel$values))
lambda_teasel <- Re(e_teasel$values[dominant_teasel])
w_teasel <- Re(e_teasel$vectors[, dominant_teasel])
w_teasel <- w_teasel / sum(w_teasel)
lambda_teasel[1] 2.33388
round(setNames(w_teasel, teasel$to), 3)dormant_1 dormant_2 small medium large flowering
0.638 0.264 0.012 0.069 0.012 0.005
N_teasel <- matrix(NA, nrow = 31, ncol = 6, dimnames = list(NULL, teasel$to))
N_teasel[1, ] <- c(0, 0, 0, 0, 0, 100)
for (t in 1:30) N_teasel[t + 1, ] <- A_teasel %*% N_teasel[t, ]
as_tibble(N_teasel) |>
mutate(year = 0:30) |>
pivot_longer(-year, names_to = "stage", values_to = "number") |>
ggplot(aes(x = year, y = number, colour = stage)) +
geom_line() +
scale_y_log10() +
labs(x = "Year", y = "Number (log scale)", colour = "Stage")Four of the six eigenvalues are complex, which is why Mod() and Re() are needed. Teasel in this field would grow fast, λ = 2.33, more than doubling each year. In the stable structure about 64% of the population are first-year dormant seeds and another 26% second-year seeds: most of a teasel population lives in the seed bank, where nobody counts it. A count of rosettes and flowering plants would see only a few percent of the population. (Such fast growth is real in a newly colonised field; it cannot last, which brings in density dependence, L07.)
Your answers:
Solution.
Often missed. A matrix with constant rates has no carrying capacity: its rates are the same at any population size, so it cannot level off at K. That needs density dependence (L07).
What was unclear, too long, redundant or missing?
Your feedback:
Before you leave. Render this file and upload it as Lastname_P02.html to your Participant Drop Box in OLAT.
Next time. On Tuesday (E1) you set up the rotifer experiment: a population whose stages you will count yourselves, and whose matrix you will build from your own data in P5. On Wednesday morning (P3) you estimate a matrix from data, and learn how the timing of the count changes what each entry means.