vignettes/02a-dynamicModellingPreyPred.Rmd
02a-dynamicModellingPreyPred.Rmd
Lotka-Volterra predator-prey models are simple models that simulate the population dynamics of a prey and a predator, relying on quite a few assumptions. In this case we assume that: * in the absence of predators, the prey population density follows a logistic growth equation, that depends on its intrinsic growth rate, r, and the environment’s carrying capacity K. * in the presence of predators, the number of prey consumed is a function of the hunting/consumption efficiency if the predator, beta, and the number of predators. * the predator population density is solely dependent on the availability of prey, the predator’s ability to convert prey into energy (predation efficiency), delta, and the intrinsic death rate of the predators in the absence of prey, gamma.
Th code bellow simulates these dynamics in a for-loop in the following steps:
You’ll notice that the model is extremely sensitive to parameter values - this is a well-know feature of the Lotka-Volterra model. The ones bellow will ensure an oscillating equilibrium between prey and predator populations.
## load libraries
library(ggplot2)
library(data.table)
## define parameter values
r <- 0.1
K <- 100
beta <- 0.02
delta <- 0.5
gamma <- 0.1
time <- 100
## make 2 storage vectors for prey and predator densities
prey <- pred <- rep(NA, time)
## set the inital population values
prey[1] <- 10
pred[1] <- 2
## The model in a loop - note that we need to start from the second time step
for(i in 2:time) {
## prey growth
prey[i] <- prey[i-1]*exp(r*(1 - prey[i-1]/K))
if (prey[i] < 0) prey[i] <- 0
## predation
preyConsumed <- beta*prey[i-1]*pred[i-1]
prey[i] <- prey[i] - preyConsumed
## predator growth
pred[i] <- pred[i-1] + delta*preyConsumed - gamma*pred[i-1]
if (pred[i] < 0) pred[i] <- 0
}
## plot results:
plotData <- data.table(prey = prey, pred = pred, time = 1:time)
plotPrey <- ggplot(plotData) +
geom_line(aes(x = time, y = prey), col = "blue", size = 1)
plotPred <- ggplot(plotData) +
geom_line(aes(x = time, y = pred), col = "red", size = 1)
Plot(plotPrey, plotPred)
Now to the actual exercise. Try to convert this model into a 2-module SpaDES project/model. Here are a few tips:
newModule()
(twice) to create the blank templates for your predator and prey population modules..R
files that show up in the module folders..inputObjects
section to create the needed inputs.distFromSource
and establish
in the “init” event.plot
event.Try to make these modules spatially explicit if you have extra time. Hints: 1. You can use raster layers, instead of vectors, to save/track preyPop
and predPop
and use raster-based operations to calculate how many prey and consumed in each pixel. 2. You can use data.tables
to speed up the computations, where each line corresponds to a pixel in a template raster and the columns are preyPop
, predPop
and consumedPrey
. Remaining paratemers can be in a separate data.table
, or simply left as numeric vectors. Then, for plotting, you can create and replace values in rasters to show spatial variation in predator and prey densities.
For prey module For predator module
This solution follows the a-spatial option.