Individual Covariates

If there is an individual covariate you wish to solve for you may specify it by the iCov dataset:

library(rxode2)

library(units)
library(xgxr)

mod3 <- function() {
  ini({
    TKA   <- 2.94E-01
    ## Clearance with individuals
    TCL   <- 1.86E+01 
    TV2   <-4.02E+01
    TQ    <-1.05E+01
    TV3   <-2.97E+02
    TKin  <- 1
    TKout <- 1
    TEC50 <-200
  })
  model({
    KA            <- TKA
    CL            <- TCL * (WT / 70) ^ 0.75
    V2            <- TV2
    Q             <- TQ
    V3            <- TV3
    Kin           <- TKin
    Kout          <- TKout
    EC50          <- TEC50
    Tz            <- 8
    amp           <- 0.1
    C2            <- central/V2
    C3            <- peri/V3
    d/dt(depot)   <- -KA*depot
    d/dt(central) <- KA*depot - CL*C2 - Q*C2 + Q*C3
    d/dt(peri)    <-                    Q*C2 - Q*C3
    d/dt(eff)     <-  Kin - Kout*(1-C2/(EC50+C2))*eff
    eff(0)        <- 1  ## This specifies that the effect compartment starts at 1.
  })
}

ev <- et(amount.units="mg", time.units="hours") %>%
  et(amt=10000, cmt=1) %>%
  et(0,48,length.out=100) %>%
  et(id=1:4)

set.seed(10)
rxSetSeed(10)
## Now use iCov to simulate a 4-id sample
r1 <- solve(mod3, ev,
            # Create individual covariate data-frame
            iCov=data.frame(id=1:4, WT=rnorm(4, 70, 10)))
print(r1)

plot(r1, C2, log="y")

Time Varying Covariates

## options(knitr.table.format = "html")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  out.width = "100%"
)
options(width=80, cli.width=80)
Sys.setenv(RSTUDIO_CONSOLE_WIDTH=80)

Covariates are easy to specify in rxode2, you can specify them as a variable. Time-varying covariates, like clock time in a circadian rhythm model, can also be used. Extending the indirect response model already discussed, we have:

library(rxode2)
library(units)

mod4 <- mod3 %>%
  model(d/dt(eff) <-  Kin - Kout*(1-C2/(EC50+C2))*eff) %>%
  model(-Kin) %>%
  model(Kin <- TKin + amp *cos(2*pi*(ctime-Tz)/24), append=C2, cov="ctime")

ev <- et(amountUnits="mg", timeUnits="hours") %>%
    et(amt=10000, cmt=1) %>%
    et(0,48,length.out=100)


## Create data frame of 8 am dosing for the first dose This is done
## with base R but it can be done with dplyr or data.table
ev$ctime <- (ev$time+set_units(8,hr)) %% 24
ev$WT <- 70

Now there is a covariate present in the event dataset, the system can be solved by combining the dataset and the model:

r1 <- solve(mod4, ev, covsInterpolation="linear")
print(r1)

When solving ODE equations, the solver may sample times outside of the data. When this happens, this ODE solver can use linear interpolation between the covariate values. It is equivalent to R's approxfun with method="linear".

plot(r1,C2, ylab="Central Concentration")
plot(r1,eff) + ylab("Effect") + xlab("Time")

Note that the linear approximation in this case leads to some kinks in the solved system at 24-hours where the covariate has a linear interpolation between near 24 and near 0. While linear seems reasonable, cases like clock time make other interpolation methods more attractive.

In rxode2 the default covariate interpolation is be the last observation carried forward (locf), or constant approximation. This is equivalent to R's approxfun with method="constant".

r1 <- solve(mod4, ev,covsInterpolation="locf")
print(r1)

which gives the following plots:

plot(r1,C2, ylab="Central Concentration", xlab="Time")
plot(r1,eff, ylab="Effect", xlab="Time")

In this case, the plots seem to be smoother.

You can also use NONMEM's preferred interpolation style of next observation carried backward (NOCB):

r1 <- solve(mod4, ev,covsInterpolation="nocb")
print(r1)

which gives the following plots:

plot(r1,C2, ylab="Central Concentration", xlab="Time")
plot(r1,eff, ylab="Effect", xlab="Time")


nlmixr2/rxode2 documentation built on Jan. 11, 2025, 8:48 a.m.