We will create functions and use the package deSolve to numerically integrate population dynamics. This package contains several solvers for diferential equations that perform numerical integration.
Let’s start with a simple example using the logistic growth model.
\[\frac{dN}{dt} = rN(1 − \alpha N)\]
We will specify the parameters and then solve the ODE.
Then, we can use the ode()
function to solve the model for our defined parameters.
# give the function and the parameters to the ode function
out_log <- ode(y = y0, times = t, func = logGrowth, parms = p)
Let’s explore the output. The object out
is a matrix with a first column containing the time stamp and the following columns, the state variable(s).
head(out_log)
time N
[1,] 1 10.00000
[2,] 2 26.72369
[3,] 3 69.45310
[4,] 4 168.66426
[5,] 5 355.46079
[6,] 6 599.85982
Plotting the result using ggplot2. We will need to convert the out
object into a data.frame.
df_log <- as.data.frame(out_log)
ggplot(df_log) +
geom_line(aes(x = time, y = N)) +
theme_classic()
\[\frac{dN_1}{dt} = r_1 N_1 (1 − \alpha_{11} N_1 − \alpha_{12} N_2)\]
\[\frac{dN_2}{dt} = r_2 N_2 (1 − \alpha_{22} N_2 − \alpha_{21} N_1)\]
First, we creating a function to represent the ODEs.
Then, we solve the system using defined parameters and the function ode()
. Here, we have to define the \(\alpha\) matrix with the competition coefficients:
\[ \left(\begin{array}{cc} \alpha_{11} & \alpha_{12}\\ \alpha_{21} & \alpha_{22} \end{array}\right) \left(\begin{array}{cc} 0.02 & 0.01\\ 0.01 & 0.03 \end{array}\right) \]
Solving the system of ODE.
time 1 2
[1,] 1 10.00000 10.00000
[2,] 2 18.06979 15.99489
[3,] 3 26.20962 20.10821
[4,] 4 32.07829 21.66437
[5,] 5 35.53914 21.79929
[6,] 6 37.43160 21.46853
Notice that now we have two columns because we have two state variables.
We can again plot the solution. But first, we have to convert out data in a format in which every variable is represented in a column and every observation is represented in a row. We will use the function pivot_longer()
from the package tidyr.
df_lv <- pivot_longer(as.data.frame(out_lv), cols = 2:3)
ggplot(df_lv) +
geom_line(aes(x = time, y = value, color = name)) +
labs(x = "Time", y = "N", color = "Species") +
theme_classic()