24 Discrete-time Lotka-Volterra Predator-Prey Model in R
Learning outcomes:
- Implement and explore a discrete-time predator-prey model in R.
- Understand how time steps introduce delays and affect stability.
- Compare discrete-time dynamics to continuous-time dynamics.
24.1 Key idea
With \(\Delta t = 1\), the phase plane typically shows expanding spirals, reflecting instability introduced by discrete time steps.
24.2 Introduction
The Lotka-Volterra model describes the interaction between predators and prey. This document demonstrates how to program the discrete-time version of the model in R, explore its features, and visualise the dynamics of predator and prey populations over time. The time step (\(\Delta t\)) can be adjusted to explore the impact of a time delay in the system. In ecological systems, time delays can represent biological processes such as the time it takes for predators to respond to changes in prey density, for prey populations to recover after predation. If \(\Delta t\) is set to 1, this model is equivalent to a year-by-year model (the Excel spreadsheet with dt = 1), where the impact of predators on prey has a 1 year lag. i.e. the growth of predators, depends on the availability of prey 1 year ago and the growth of prey depends on the amount of predators 1 year ago.
There are two ways to think about \(\Delta t\). With \(\Delta t = 1\) this is a model of populations that really do change in steps (e.g. one breeding season per year). With \(\Delta t\) very small, nothing biological has changed: we are simply calculating the continuous-time model more accurately, in many small steps. The parameters and the starting numbers are the same as in the Excel spreadsheet and the continuous-time R chapter.
24.3 Step 1: Define the Model
The discrete-time equations are as follows:
Prey population growth:
\(H_{t+1} = H_t + \Delta t \cdot (r \cdot H_t - a \cdot H_t \cdot P_t)\)Predator population growth:
\(P_{t+1} = P_t + \Delta t \cdot (c \cdot H_t \cdot P_t - d \cdot P_t)\)
Note that, when \(\Delta t = 1\), the model is as follows:
Prey population growth:
\(H_{t+1} = H_t + r \cdot H_t - a \cdot H_t \cdot P_t\)Predator population growth:
\(P_{t+1} = P_t + c \cdot H_t \cdot P_t - d \cdot P_t\)
These are the formulas you typed into the Excel spreadsheet (there with the time step dt in cell F8).
The function below implements these equations in R.
discrete_predator_prey_model <- function(time_steps, dt, initial_state, parameters) {
# Initialise vectors to store populations
time_steps <- round(time_steps)
H <- numeric(time_steps + 1)
P <- numeric(time_steps + 1)
time <- seq(0, time_steps * dt, by = dt)
# Set initial populations
H[1] <- initial_state["H"]
P[1] <- initial_state["P"]
# Extract parameters
r <- parameters["r"]
a <- parameters["a"]
c <- parameters["c"]
d <- parameters["d"]
# Loop through time steps to calculate populations
for (t in 1:time_steps) {
# max(0, ...) stops populations going negative (like MAX() in Excel)
H[t + 1] <- max(0, H[t] + dt * (r * H[t] - a * H[t] * P[t]))
P[t + 1] <- max(0, P[t] + dt * (c * H[t] * P[t] - d * P[t]))
}
# Combine results into a data frame
data.frame(time = time, H = H, P = P)
}24.4 Step 2: Set Parameters and Initial Conditions
We set the parameters, initial conditions, the total time to simulate (total_time) and the time step (\(\Delta t\)). The number of steps is calculated from these, so that changing \(\Delta t\) always covers the same total time and the runs can be compared fairly.
parameters <- c(r = 0.25, # Prey growth rate
a = 0.01, # Attack rate
c = 0.00008, # Conversion efficiency
d = 0.1) # Predator death rate
initial_state <- c(H = 1000, # Initial prey population
P = 20) # Initial predator population
total_time <- 300 # Total time to simulate
dt <- 1 # Time step (dt), 1 = a standard discrete model
time_steps <- total_time / dt # Number of time steps needed to cover total_time24.6 Step 4: Visualise the Dynamics
24.6.1 Population Dynamics Over Time
The plot below shows the changes in the prey and predator populations over time.
# Make room in the right-hand margin for the second axis label
par(mar = c(5, 4, 4, 5))
# Plot the prey population on the primary y-axis
plot(output_df$time, output_df$H, type = "l", col = "blue", lwd = 2,
ylab = "Prey (H)", xlab = "Time", main = "Discrete Predator-Prey Dynamics")
# Add the predator population on a secondary y-axis
par(new = TRUE)
plot(output_df$time, output_df$P, type = "l", col = "red", lwd = 2,
axes = FALSE, xlab = "", ylab = "")
axis(side = 4, col = "red", col.axis = "red")
mtext("Predators (P)", side = 4, line = 3, col = "red")
# Add legend
legend("topright", legend = c("Prey (H)", "Predators (P)"),
col = c("blue", "red"), lwd = 2)
24.6.2 Phase Plot with ZNGIs
The phase plot shows the relationship between the prey and predator populations.
### Phase Plot with ZNGIs, Initial Point, and Arrow
plot(output_df$H,
output_df$P,
type = "l",
col = "purple",
lwd = 2,
xlab = "Prey (H)", ylab = "Predators (P)",
main = "Discrete Predator-Prey Dynamics with ZNGIs")
# Add predator ZNGI: no change in P when H = d / c (a vertical line)
abline(v = parameters["d"] / parameters["c"],
col = "red", lwd = 2, lty = 2)
# Add prey ZNGI: no change in H when P = r / a (a horizontal line)
abline(h = parameters["r"] / parameters["a"], col = "blue", lwd = 2, lty = 2)
# Add point for initial population sizes
points(output_df$H[1], output_df$P[1], pch = 19, col = "black", cex = 1.5)
# Add a direction arrow only if the final step has visible movement.
n <- nrow(output_df)
step_inches <- c(
diff(grconvertX(output_df$H[c(n - 1, n)], from = "user", to = "inches")),
diff(grconvertY(output_df$P[c(n - 1, n)], from = "user", to = "inches"))
)
if (all(is.finite(step_inches)) && sqrt(sum(step_inches^2)) > 1e-3) {
arrows(x0 = output_df$H[n - 1], y0 = output_df$P[n - 1],
x1 = output_df$H[n], y1 = output_df$P[n],
col = "purple", length = 0.1, lwd = 2)
}24.7 Your task
Before each run, predict what you will see (sketch the phase plot), then check.
- Run the model with the default settings (\(\Delta t = 1\),
total_time = 300). How does the phase plot differ from the closed orbit in the continuous-time chapter? When does the first population hit zero? Find it withoutput_df$time[which(output_df$P == 0 | output_df$H == 0)[1]]. - Decrease \(\Delta t\) to 0.1 (keep
total_time = 300; the number of steps is recalculated for you, so re-run the parameter chunk as well as the simulation) and compare the phase plane behaviour to \(\Delta t = 1\). Then try \(\Delta t = 0.01\). - Increase \(\Delta t\) to 2 and observe whether extinction happens faster.
- Change \(r\) or \(d\) (e.g. double one of them) with \(\Delta t = 1\). Does the spiral grow faster or slower? Now double \(a\) or \(c\) instead. Which parameters control how quickly the spiral grows? (Careful: changing \(a\) or \(c\) also moves the equilibrium, so the default start is then nearer to or further from it. Compare how fast the spiral widens, not only when it ends.)
24.8 Conclusion
This document demonstrates how to implement and explore the discrete-time Lotka-Volterra predator-prey model. By adjusting the time step (\(\Delta t\)) and other parameters, you can investigate how predator-prey dynamics are influenced by ecological factors. With the default settings (\(\Delta t = 1\)), the time lag of one step makes the phase plot spiral outwards: the cycles grow until the prey are wiped out (they hit zero at \(t = 220\)), after which the predators starve and dwindle towards zero. Smaller time steps make the spiral grow more slowly, and with \(\Delta t = 0.01\) the orbit is almost closed, like the continuous-time model. Larger time steps make it grow faster: with \(\Delta t = 2\) the prey hit zero at \(t = 142\). Contrast this with the perpetual (neutrally stable) oscillations you get with the continuous-time model.
24.9 Takeaways
- Discrete time steps destabilise the predator-prey cycles of the continuous-time model: the populations react one step too late, so each cycle overshoots a little more than the last.
- Each time step, the distance from the equilibrium grows by a factor of about \(\sqrt{1 + \Delta t^2 r d}\). This depends on the prey growth rate, the predator death rate and the step length, but not on the attack rate or conversion efficiency. With the default values it is about 1.012 per step (roughly 1 % per step).
- Smaller \(\Delta t\) values bring the discrete model closer to the continuous-time behaviour.