nlmixrdevelopment / RxODE

RxODE is an R package that facilitates easy simulations in R
https://nlmixrdevelopment.github.io/RxODE/
GNU General Public License v3.0
54 stars 14 forks source link

Different interpolation methods for different time varying covariates #154

Open PhilP86 opened 4 years ago

PhilP86 commented 4 years ago

Hi,

I am simulating a model with 2 different time-varying covariates and I wanted to have one interpolation method for the first covariate (e.g 'linear') and a different interpolation method for the second covariate (e.g. 'constant').

Could you tell me whether it is possible to implement it and if yes how?

Many thanks.

Philippe

P.S: Happy new year and all my best wishes for the RxODE/nlmixr projects!

mattfidler commented 4 years ago

Hi Philippe,

Currently it is only possible to use one method for interpolation. However, you can simulate the time-varying covariates with different methods. I'm not sure if that is what you need.

If you need some help with the two different simulation types, let me know. If it is interpolation during solving it currently isn't possible.

PhilP86 commented 4 years ago

Hi Matt,

Thanks for your quick reply.

In fact, I meant interpolation during solving... Would it possible for next RxODE version to have the possibility to code covs_interpolation=c( cov1="linear" , cov2="constant") for instance?

Also, I would be curious to see your way of simulating time varying covariates with different methods. Could you share a piece of code?

Many thanks again.

Philippe

mattfidler commented 4 years ago

Also, I would be curious to see your way of simulating time varying covariates with different methods. Could you share a piece of code?

Adapting the rxode speed example you can see both the constant and linear methods of covariate interpolation with simulation.

library(RxODE)
library(tidyverse)
set.seed(10)

rx <- RxODE({
  CL =  log(4)
  V = log(70)
  KA = log(1)
  CL = exp(CL + eta.CL) * (WT/70)^0.75 * (CRCL/125)^0.9
  V = exp(V + eta.V)
  KA = exp(KA + eta.KA)
  d/dt(abs)    = -KA*abs;
  d/dt(centr)  =  KA*abs-(CL/V)*centr;
  C2=centr/V;
})

nsubg <- 2500 # subjects per dose
doses <- c(10, 30, 60, 120)
nsub <- nsubg * length(doses)

omega <- lotri(eta.CL ~ 0.09,
               eta.V ~ 0.09,
               eta.KA ~ 0.09)

doses <- c(10, 30, 60, 120)

# Time varying covariates
dfCOV <- data.frame(TIME=seq(0,24), WT=seq(70,75,length=25)+rnorm(25,sd=0.1), CRCL=15+rnorm(25,sd=10))

wtFun <- approxfun(dfCOV$TIME, dfCOV$WT,method="linear")
crclFun <- approxfun(dfCOV$TIME, dfCOV$CRCL, method="constant")

startParallel <- Sys.time()
ev <- do.call("rbind",
              lapply(seq_along(doses), function(i){
                et() %>%
                  et(amt=doses[i]) %>% # Add single dose
                  et(0) %>% # Add 0 observation
                  ## Generate 4 samples in 24 hour period
                  et(lapply(1:4, function(...){c(0, 24)})) %>%
                  et(id=seq(1, nsubg) + (i - 1) * nsubg) %>%
                  ## Convert to data frame to skip sorting the data
                  ## When binding the data together
                  as.tbl %>%
                  mutate(WT=wtFun(time), CRCL=crclFun(time))
              }))

res <- rxSolve(rx, ev, omega=omega)
#> [====|====|====|====|====|====|====|====|====|====] 0:00:02

res
#> ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ Solved RxODE object ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
#> ── Initial Conditions (x$inits): ───────────────────────────────────────────────
#>   abs centr 
#>     0     0 
#> ── First part of data (object): ────────────────────────────────────────────────
#> # A tibble: 50,000 x 8
#>   id     time     CL     V    KA     C2     abs centr
#>   <fct> <dbl>  <dbl> <dbl> <dbl>  <dbl>   <dbl> <dbl>
#> 1 1      0    0.508   63.9 0.653 0      10       0   
#> 2 1      1.47 0.380   63.9 0.653 0.0959  3.83    6.13
#> 3 1      8.26 0.743   63.9 0.653 0.149   0.0454  9.55
#> 4 1      8.86 0.744   63.9 0.653 0.149   0.0309  9.50
#> 5 1      9.78 0.0693  63.9 0.653 0.147   0.0169  9.41
#> 6 2      0    0.505   73.4 0.938 0      10       0   
#> # … with 49,994 more rows
#> ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂

Created on 2020-01-02 by the reprex package (v0.3.0)

mattfidler commented 4 years ago

Would it possible for next RxODE version to have the possibility to code covs_interpolation=c( cov1="linear" , cov2="constant") for instance?

I'm not sure what the value of this is. Please help me understand.

PhilP86 commented 4 years ago

Many thanks for the example of covariate simulation.

In my model I have a continuous time varying covariate (let's say CRCL) acting on clearance and a categorical time varying covariate acting on Ka (covariate representing whether the compound was administered under fasted or fed condition). I want to interpolate the CRCL with the 'linear method', but it wouldn't make sense to have a linear interpolation for the food effect.

Do you see my point or perhaps you have a workaround?

Many thanks again.

Philippe

mattfidler commented 4 years ago

Hi Philippe,

I see your point. There is no work-around currently. I will add a feature request for this, though I am still unsure if the difference in solving is large or not. Hence, I'm unsure of the value still, though theoretically there could be a difference between the two. Additionally, from a theoretical point of view it makes sense that most continuous covariates use linear interpolation (except for clock time) but it doesn't make sense for categorical covariates.

On the other hand, currently Monolix uses last observation carried forward and NONMEM uses next observation carried backward and there is no way to mix the two for either software. The inductive linearization/matrix exponential method that is in the development version of RxODE does not support linear extrapolation of time-varying covariates either (and I do not know if it is possible to add this).

Based on some high priority items (like removing python dependency, keeping RxODE/nlmixr compatible with R 4.0, etc) it isn't likely to make the next version of RxODE.

PhilP86 commented 4 years ago

Hi Matt,

Many thanks!

Have a nice day.

Philippe