简体   繁体   English

R 中的 ODE function

[英]ODE function in R

Relatively new with R and ODE modeling in R. R 和 R 中的 ODE 建模相对较新。 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.我知道 output 是什么,但是我不能简单地解释“with”、“as.list”和“list(c(dY))”代表什么。

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

as.list transforms named vectors to a list, for example: as.list将命名向量转换为列表,例如:

``` 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. with计算 LHS 环境中的 RHS 表达式。
In the example below it makes a , b and X available for evaluation:在下面的示例中,它使abX可用于评估:

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.要回答您问题的第三部分,提供stateparameters的示例将很有用。

Here a more complete example, using the function test of the original poster:这里有一个比较完整的例子,使用原贴的function test

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 .在这里,函数testtest2做的完全一样,但是等式在版本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, ie similar to mathematical notation. with(as.list(...)) - 构造只是解包 state 和参数的内容,因此可以用更少的技术编写微分方程,即类似于数学符号。 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. test的返回值是一个包含两个元素的列表,如deSolve文档中所定义。 The help page of ode tells us: ode的帮助页面告诉我们:

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. func的返回值应该是一个列表,其第一个元素是一个向量,其中包含y关于时间的导数,其下一个元素是每个时间点所需的全局值。 The derivatives must be specified in the same order as the state variables y .必须按照与 state 变量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.这意味着它可能包含一个或多个元素,其中第一个是导数的向量,另一个是要存储的 ODE model 的可选其他内部值。 In the example here list(dX) would be enough.在这里的例子list(dX)就足够了。 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 . list(c(dX))符号经常用于教学原因,以指示列表的第一个元素(向量)可能包含更多元素,与初始 state state0中给出的状态数一样多。 It can for example be: list(c(dX, dY, dZ)) , given that a model has three states .例如,它可以是: list(c(dX, dY, dZ)) ,假设 model 具有三个 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.或者如果我们想从 model 保存内部或“全局”变量,它可能是list(c(dX, dY, dZ), a, b, c)

The plot at the end is just to show that test and test2 give the same results.最后的 plot 只是为了表明testtest2给出相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM