Home > Software engineering >  ODE function in R
ODE function in R

Time:09-07

Relatively new with R and ODE modeling in R. I was wondering if anyone can help me explain in layman terms what the terms within this code mean. I know what the output is, however I cannot simply explain what "with", "as.list", and "list(c(dY))" represent.

test = function(timepoint, state , parameters) {
  with(as.list(c(state, parameters)), {
    dX = a * X
    list(c(dX))
  })
}

CodePudding user response:

as.list transforms named vectors to a list, for example :

``` r
state = c(X=1)
parameters = c(a=3,b=4)

# Create a named vector
c(state,parameters)
#> X a b 
#> 1 3 4

# Convert to list
as.list(c(state,parameters))
#> $X
#> [1] 1
#> 
#> $a
#> [1] 3
#> 
#> $b
#> [1] 4

with evaluates the RHS expression inside LHS environment.
In the example below it makes a, b and X available for evaluation:

with(as.list(c(state, parameters)), {dX = a*X; dX})
[1] 6

To answer the third part of your question, providing examples of state and parameters would be useful.

CodePudding user response:

Here a more complete example, using the function testof the original poster:

library("deSolve")
test <- function(timepoint, state , parameters) {
  with(as.list(c(state, parameters)), {
    dX <- a * X
    list(c(dX))
  })
}

test2  <- function(timepoint, state , parameters) {
    dX <- parameters["a"] * state["X"]
    list(dX)
}

state0 <- c(X = 1)
pars   <- c(a = 0.5)
times  <- seq(0, 10, 0.1)

out <- ode(state0, times, test, pars)

out2 <- ode(state0, times, test2, pars)

plot(out, out2)

Here, functions test and test2 do exactly the same, but the equation is easier to read in version test. The with(as.list(...))-construction just unpacks the contents of state and parameters, so that the differential equation(s) can be written less technically, i.e. similar to mathematical notation. The benefit is more obvious in bigger models with more than one equation.

The return value of test is a list with two elements, as defined in the deSolve documentation. The help page of ode tells us:

The return value of func should be a list, whose first element is a vector containing the derivatives of y with respect to time, and whose next elements are global values that are required at each point in times. The derivatives must be specified in the same order as the state variables y.

This means that it may contain one or more elements, where the first is a vector of the derivatives and the other are optional other internal values of the ODE model that are to be stored. In the example here list(dX) would be enough. The list(c(dX)) notation ist often used for didactical reasons to indicate that the first element of the list (the vector) may contain more elements, precisely as many as the number of states given in the initial state state0. It can for example be: list(c(dX, dY, dZ)), given that a model has three states. Or it may be list(c(dX, dY, dZ), a, b, c) if we want to save internal or "global" variables from the model.

The plot at the end is just to show that testand test2 give the same results.

  • Related