nlmixrdevelopment / nlmixr

nlmixr: an R package for population PKPD modeling
https://nlmixrdevelopment.github.io/nlmixr/
GNU General Public License v2.0
116 stars 45 forks source link

access to internal variables? #45

Closed ronkeizer closed 6 years ago

ronkeizer commented 6 years ago

was wondering if there is a way in the model definition to use internal variables, especially time (either discrete or continuous time, i.e. TIME or T in NONMEM), and the event type at a specific time? Use case is e.g. calculation of time after dose and interpolation of time-varying covariates. If there are other ways to implement those I'd be happy to learn as well :)

Was also wondering how nlmixr/rxode identifies what part of model definition goes into $PK and what goes into $DES (to use the NONMEM analogy), or does it treat all the code as $DES-type?

Thanks!

mattfidler commented 6 years ago

was wondering if there is a way in the model definition to use internal variables

Yes, accessing internal RxODE variables is possible.

Use case is ... calculation of time after dose

This is calculated for the tranit compartment models. However, it isn't turned on for everything. This should be easy to add.

Use case is ... interpolation of time-varying covariate

Time varying covarites are on the TODO list for nlmixr. The experimental FOCEi supports them, though SAEM and nlme do not. RxODE does support them so it should be something that is acheivable, though not yet mature.

Was also wondering how nlmixr/rxode identifies what part of model definition goes into $PK and what goes into $DES (to use the NONMEM analogy), or does it treat all the code as $DES-type?

This may change, so be advised;

Currently a model defined by:

model({
       ## $PK block
        ka <- exp(tka + eta.ka)
        cl <- exp(tcl + eta.cl)
        v <- exp(tv + eta.v)
        ## $DES block
        d/dt(depot) = -ka * depot
        d/dt(center) = ka * depot - cl / v * center
        cp = center / v
        ## $ERR block
        cp ~ add(add.err)
    })

Where it breaks it down is determined by parsing the ini block and parsing the PK plot and looking for where the parameters are defined.

ronkeizer commented 6 years ago

Thanks for clarifications, Matt.

rdoctor commented 6 years ago

Is there a way in add.dosing (or some workaround) to use internal variables (time or other variables used in the model)? The use case is the dose's effect changing based on the status of the variables. An example of what I'm trying to do is below.

SD.model <-
"
    d/dt(WEL)  <- 0
"
##omitting model and table construction
migr.table$add.dosing(dose = shock.ES *(WEL-2)/2,        #the effect of the dose depends on the current status of the variable WEL
                      nbr.doses = shock.nbr,
                      dosing.interval = shock.period,
                      dosing.to = 1,
                      start.time = shock.t)
mattfidler commented 6 years ago

Hi @rdoctor,

The easiest way to add other data to a nlmixr model is to use a full dataset and treat it as a covariate.

If you are familiar with NONMEM and Monolix, there is a function nmDataConvert that converts a NONMEM dataset into a nlmixr dataset. Most datasets are converted automatically with some warning, so it shouldn't be a problem.

Currently time-varying covariates are not supported by SAEM. They may be supported by nlme but I'm unsure; They will be supported by the newer experimental FOCEi which is nearing completion.

If you have any other questions, feel free to open up another issue.