Search code examples
rdesolve

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


My ODE-model is given parameters from existing dataframes, but doesn't seem to find some of the parameters unless I use "<-" and not "=". In a recent question I posted for a different model I was corrected in the way I used "<-" and "=", so I am adjusting my code to correct this problem, however now I get this error for object 'cw', 'mw' and 'pr':

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

This is a simplified version of my model below. I have tried using as.list() to adjust the parameters, but it doesn't work. Is it better to adjust the parameters or can I run using "<-" for 'cw', 'mw' and 'pr'? Will this potentially lead to any mistakes in the model? If yes, any ideas how I can adjust the parameters from the dataframe so 'cw', 'mw' and 'pr' is found? Or perhaps there is a mistake in the code that I am not seeing. I have commented next to the three parameters that I am having problems with.

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))

Solution

  • You're right to be worried about this. I fixed this by using a list for parms, instead of a vector:

    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
    ))