nlmixr2 / rxode2

rxode2
https://nlmixr2.github.io/rxode2/
GNU General Public License v3.0
24 stars 6 forks source link

`rxode2` unexpected simulation #654

Closed mattfidler closed 4 months ago

mattfidler commented 4 months ago
library(rxode2)
#> rxode2 2.1.2 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`

# First fit with if statement to indicate diff IV/PO
modA <- function() {
  ini({
    tka <- 0.45; backTransform("exp") # Log Ka
    tcl <- -7     # Log Cl
    tv  <- -8     # Log V
    eta.ka ~ 0.6
    eta.cl ~ 0.3
    eta.v ~ 0.1
    prop.sd <- 0.7
  })
  model({
    if(ROUTE!=1) ka <- exp(tka + eta.ka)
    cl <- exp(tcl + eta.cl)
    v <- exp(tv + eta.v)
    d/dt(depot) = -ka * depot
    d/dt(center) = ka * depot - cl / v * center
    cp = center / v
    cp ~ prop(prop.sd)
  })
}

et <- et(amount.units='mg', time.units='hours') %>%
  et(dose=10) %>% et(seq(0,8,0.2)) %>% mutate(ROUTE=1)
#> Error in mutate(., ROUTE = 1): could not find function "mutate"
sims <- rxSolve(modA,et)
#> ℹ parameter labels from comments will be replaced by 'label()'
#> Warning: some etas defaulted to non-mu referenced, possible parsing error: eta.ka
#> as a work-around try putting the mu-referenced expression on a simple line
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
#> Error in rxSolveSEXP(object, .ctl, .nms, .xtra, params, events, inits, : cannot solve without event information
#> this can occur when the data frame you are providing does not have the column 'time'
plot(sims)
#> Error in eval(expr, envir, enclos): object 'sims' not found

Created on 2024-02-12 with reprex v2.1.0

mattfidler commented 4 months ago

@RichardHooijmaijers I believe this is expected behavior.

What happens is the values in the problem are intiailized to NA; If they are not changed in the simulation then they would remain NA.

I suppose the better model would be as follows:

library(rxode2)
#> rxode2 2.1.2 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# First fit with if statement to indicate diff IV/PO
modA <- function() {
  ini({
    tka <- 0.45; backTransform("exp") # Log Ka
    tcl <- -7     # Log Cl
    tv  <- -8     # Log V
    eta.ka ~ 0.6
    eta.cl ~ 0.3
    eta.v ~ 0.1
    prop.sd <- 0.7
  })
  model({
    ka <- 0
    if(ROUTE!=1) ka <- exp(tka + eta.ka)
    cl <- exp(tcl + eta.cl)
    v <- exp(tv + eta.v)
    d/dt(depot) = -ka * depot
    d/dt(center) = ka * depot - cl / v * center
    cp = center / v
    cp ~ prop(prop.sd)
  })
}

et <- et(amount.units='mg', time.units='hours') %>%
  et(dose=10) %>% et(seq(0,8,0.2)) %>% mutate(ROUTE=1)
sims <- rxSolve(modA,et)
#> ℹ parameter labels from comments will be replaced by 'label()'
#> Warning: some etas defaulted to non-mu referenced, possible parsing error: eta.ka
#> as a work-around try putting the mu-referenced expression on a simple line
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
plot(sims)


#note that a dose to the depot (that is turned off) doesn't make sense
#anyway, so consider dosing to the central compartment:

et <- et(amount.units='mg', time.units='hours') %>%
  et(dose=10, cmt="center") %>% et(seq(0,8,0.2)) %>% mutate(ROUTE=1)
sims <- rxSolve(modA,et)
#> ℹ parameter labels from comments will be replaced by 'label()'
#> Warning: some etas defaulted to non-mu referenced, possible parsing error: eta.ka
#> as a work-around try putting the mu-referenced expression on a simple line
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
plot(sims)

Created on 2024-02-12 with reprex v2.1.0