Chapter 1 Model implementation

The implementation of a model is identical to the implementation used in the package deSolve (Soetaert, Petzoldt, and Setzer 2010) for systems of ODEs. The model implementation has 3 elements: a vector of state variables, a vector of parameters and the specification of the right-hand side of the ODEs. Both the vector of state variables and the vector of parameters, should consist of named vector elements only. The right-hand sides of the ODEs have to be specified in a R function that has the following layout:

model <- function(t, state, parms) {
  with(as.list(c(state,parms)), {
    #
    # Define your derivatives here as a list. For example:
    #
    # derivatives <- list(c(dS1, dS2))
    #
    # where dS1 and dS2 are variables to be assigned the
    # values of state variable derivatives
    #
    return(derivatives)
  })
}

The name of the function (here model) can be chosen freely. The returned variable derivatives should be a list of derivative values that specify the right-hand side of the ODEs.

Notice, the order of these derivatives should be identical to the order of the state variables that are contained in the variable state.

After defining the right-hand side of the ODEs the functions phaseplane() and bifurcation() can be invoked using the commands:

phaseplane(model, state, parms)

and

bifurcation(model, state, parms)

in which model is the name of the function that specifies the right-hand side of the ODEs, state should be the name of the vector with values for the state variables and parms should be the name of the vector with values for the parameters.

1.1 Model implementation example

As an example, consider the Rosenzweig-MacArthur model for the interaction between a predator and a prey:

\[\begin{align*} \dfrac{dR}{dt}&=\;r R \left(1- \dfrac{R}{K}\right) \;-\; \dfrac{aR}{1 + a h R}\,C\\ \dfrac{dC}{dt}&=\;\epsilon\dfrac{aR}{1 + a h R}\,C\;-\;\mu \, C \end{align*}\]

with variables \(R\) and \(C\) representing the densities of the prey and predator, respectively. The parameters in this model are the prey per-capita growth rate \(r\) and its carrying capacity \(K\), the attack rate of the predator \(a\), its handling time \(h\), conversion efficiency \(\epsilon\) and its mortality rate \(\mu\).

To implement this model first a named vector of values for the state variables has to be defined:

state <- c(R = 0.05, C = 0.1)

and similarly a named vector of default values for the parameters:

parms <- c(r = 0.5, K = 0.1, a = 5.0, h = 3.0, eps = 0.5, mu = 0.05)

The named elements of the state variable vector and the parameter vector can now be used in the function that described the right-hand side of the ODEs:

rosenzweig <- function(t, state, parms) {
  with(as.list(c(state,parms)), {

    dR = r*R*(1 - R/K) - a*R*C/(1 + a*h*R)
    dC = eps*a*R*C/(1 + a*h*R) - mu*C

    return(list(c(dR, dC)))
  })
}

To carry out bifurcation analysis of the Rosenzweig-MacArthur model implemented above an R script can be created with the following content:

# The initial state of the system has to be 
# specified as a named vector of state values.
state <- c(R = 0.05, C = 0.1)

# Parameters has to be specified as a named vector of parameters.
parms <- c(r = 0.5, K = 0.1, a = 5.0, h = 3.0, eps = 0.5, mu = 0.05)

# The model has to be specified as a function that returns
# the derivatives as a list. You can adapt the body below
# to represent your model
rosenzweig <- function(t, state, parms) {
  with(as.list(c(state,parms)), {

    dR = r*R*(1 - R/K) - a*R*C/(1 + a*h*R)
    dC = eps*a*R*C/(1 + a*h*R) - mu*C

    # The order of the derivatives in the returned list has to be
    # identical to the order of the state variables contained in 
    # the argument `state` 
    return(list(c(dR, dC)))
  })
}

bifurcation(rosenzweig, state, parms)

Running this script will define the vectors of the state variables and parameters as well as the function defining the system of ODEs and will start up the shiny application for bifurcation analysis.

The above script is in fact included in the deBif package as an example. It can be started up by issuing the command deBifExample("rosenzweig"). The function deBifExample() provides a list of examples included in the package when the function is called without any argument and executes an example script if the argument provided to deBifExample() refers to an included example.

References

Soetaert, Karline, Thomas Petzoldt, and R. Woodrow Setzer. 2010. “Solving Differential Equations in R: Package deSolve.” Journal of Statistical Software 33 (9): 1–25. https://doi.org/10.18637/jss.v033.i09.