1

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))
Mandy94
  • 37
  • 4

1 Answers1

2

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
))
Mossa
  • 1,656
  • 12
  • 16
  • 2
    This is the right answer, because `variables$c.w` etc. contain more than a single value. That's why a list is more appropriate. I see also that it was used to implement time dependent variables. It can be done this way, but you may also consider to use the **forcing function** mechanism instead. This is well documented in the package `?forcings`, on several websites and on StackOverflow, just search for https://stackoverflow.com/search?q=%5Br%5D+desolve+forcing – tpetzoldt May 30 '22 at 14:02
  • Thank you, it runs with no problem now. Just for my understanding - if I were to use "<-" for the three parameters, what kind of interferences could occur when running the model? Will there be direct effects to the result of the model or possible consequential errors in the following code? Thanks for the forcings-tip, looking into it as we speak! – Mandy94 May 30 '22 at 15:13
  • 2
    You have to understand that the assignment semantics of `=` differ from `<-`. What you're doing is making these values available in the "global environment", and they are then read from there, instead of being read in your `parms` list or whatever.. – Mossa May 30 '22 at 15:17
  • 2
    In addition to @Mossa's correct answer, see my 2nd answer to the former question [here](https://stackoverflow.com/a/72309520/3677576). I thought it became obsolete, but as the distinction is not yet clear, I want to point at it again. Just try the toy example at the end. – tpetzoldt May 30 '22 at 15:26