除非我使用“<-”而不是“=”,否则 ODE 找不到给定数据帧的参数

ODE doesn't find the parameters given dataframe unless I use "<-" and not "="

我的 ODE 模型从现有数据帧中获得参数,但似乎找不到某些参数,除非我使用“<-”而不是“=”。在我最近发布的一个不同模型的问题中,我使用“<-”和“=”的方式得到了更正,所以我正在调整我的代码以更正这个问题,但是现在我得到了对象 [=16= 的错误], 'mw' 和 'pr':

Error in eval(substitute(expr), data, enclos = parent.frame()) : 
  object 'xxx' not found

这是我下面模型的简化版本。我试过使用 as.list() 来调整参数,但它不起作用。是调整参数更好还是我可以 运行 对 'cw'、'mw' 和 'pr' 使用“<-”?这是否会导致模型出现任何错误?如果是,有什么想法可以调整数据帧中的参数,以便找到 'cw'、'mw' 和 'pr'?或者我没有看到代码中的错误。我在遇到问题的三个参数旁边进行了评论。

library(deSolve)

c.w <- c(3, 4, 5, 6, 7, 8, 9, 10)
prop <- c(1, 1, 1, 0.5, 0.5, 0.5, 0.2, 0)
m.w <- c(80, 79, 79, 76, 75, 74, 75, 73)

variables <- data.frame(c.w, prop, m.w)

reverse <- function(times, y, parms) {
  with(as.list(c(y, parms)), {

    volume <- ((0.1 - 0.01 * (times * 0.03)) * cw[times+1]) * pr[times+1]
    conc.m <- pm * concentration
    transfer <- conc.m * volume
    
    concentration <- (-transfer) / (vd * mw[times+1])
    
    list(concentration)
  })
}

state <- c(concentration = 0.5)
params <- c(vd = 0.2,
                pm = 0.05,
                cw = variables$c.w,  #error unless I use "<-" and not "="
                mw = variables$m.w,  #error unless I use "<-" and not "="
                pr = variables$prop) #error unless I use "<-" and not "="
rev <- data.frame(ode(y = state,
                           times = c(5:0),
                           func = reverse,
                           parms = params))

你担心这个是对的。我通过对 parms 使用 list 而不是向量来修复此问题:

library(deSolve)

c.w <- c(3, 4, 5, 6, 7, 8, 9, 10)
prop <- c(1, 1, 1, 0.5, 0.5, 0.5, 0.2, 0)
m.w <- c(80, 79, 79, 76, 75, 74, 75, 73)

variables <- data.frame(c.w, prop, m.w)
reverse <- function(times, y, parms, ...) {
  concentration <- y
  with(parms, {
    volume <-
      ((0.1 - 0.01 * (times * 0.03)) * cw[times + 1]) * pr[times + 1]
    conc.m <- pm * concentration
    transfer <- conc.m * volume
    
    concentration <- (-transfer) / (vd * mw[times + 1])
    
    list(concentration)
  })
}

state <- c(concentration = 0.5)
parms <- list(
  vd = 0.2,
  pm = 0.05,
  cw = variables$c.w,
  mw = variables$m.w,
  pr = variables$prop
)
rev <- data.frame(ode(
  y = state,
  times = c(5:0),
  func = reverse,
  parms = parms
))