Skip to contents

Individual Covariates

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

## rxode2 2.0.13.9000 using 1 threads (see ?getRxThreads)
##   no cache: create with `rxCreateCache()`
## udunits database from /usr/share/xml/udunits/udunits2.xml
library(xgxr)

mod3 <- rxode2({
    KA=2.94E-01;
    ## Clearance with individuals
    CL=1.86E+01 * (WT / 70) ^ 0.75;
    V2=4.02E+01;
    Q=1.05E+01;
    V3=2.97E+02;
    Kin=1;
    Kout=1;
    EC50=200;
    ## The linCmt() picks up the variables from above
    C2   = linCmt();
    Tz= 8
    amp=0.1
    eff(0) = 1  ## This specifies that the effect compartment starts at 1.
    d/dt(eff) =  Kin - Kout*(1-C2/(EC50+C2))*eff;
})
## using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
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)
## ── Solved rxode2 object ──
## ── Parameters ($params): ──
##      KA      V2       Q      V3     Kin    Kout    EC50      Tz     amp 
##   0.294  40.200  10.500 297.000   1.000   1.000 200.000   8.000   0.100 
## ── Initial Conditions ($inits): ──
## eff 
##   1 
## ── First part of data (object): ──
## # A tibble: 400 × 6
##      id  time    CL    C2   eff    WT
##   <int>   [h] <dbl> <dbl> <dbl> <dbl>
## 1     1 0      18.6 249.      1  70.2
## 2     1 0.485  18.6 175.      1  70.2
## 3     1 0.970  18.6 124.      1  70.2
## 4     1 1.45   18.6  87.9     1  70.2
## 5     1 1.94   18.6  62.7     1  70.2
## 6     1 2.42   18.6  45.1     1  70.2
## # ℹ 394 more rows
plot(r1, C2, log="y")

Time Varying Covariates

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)

mod3 <- rxode2({
    KA=2.94E-01;
    CL=1.86E+01;
    V2=4.02E+01;
    Q=1.05E+01;
    V3=2.97E+02;
    Kin0=1;
    Kout=1;
    EC50=200;
    ## The linCmt() picks up the variables from above
    C2   = linCmt();
    Tz= 8
    amp=0.1
    eff(0) = 1  ## This specifies that the effect compartment starts at 1.
    ## Kin changes based on time of day (like cortosol)
    Kin =   Kin0 +amp *cos(2*pi*(ctime-Tz)/24)
    d/dt(eff) =  Kin - Kout*(1-C2/(EC50+C2))*eff;
})


ev <- eventTable(amount.units="mg", time.units="hours") %>%
    add.dosing(dose=10000, nbr.doses=1, dosing.to=1) %>%
    add.sampling(seq(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

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

r1 <- solve(mod3, ev, covsInterpolation="linear")
print(r1)
#> ── Solved rxode2 object ──
#> ── Parameters ($params): ──
#>         KA         CL         V2          Q         V3       Kin0       Kout 
#>   0.294000  18.600000  40.200000  10.500000 297.000000   1.000000   1.000000 
#>       EC50         Tz        amp         pi 
#> 200.000000   8.000000   0.100000   3.141593 
#> ── Initial Conditions ($inits): ──
#> eff 
#>   1 
#> ── First part of data (object): ──
#> # A tibble: 100 × 5
#>    time    C2   Kin   eff ctime
#>     [h] <dbl> <dbl> <dbl>   [h]
#> 1 0     249.   1.1   1     8   
#> 2 0.485 175.   1.10  1.04  8.48
#> 3 0.970 124.   1.10  1.06  8.97
#> 4 1.45   88.0  1.09  1.07  9.45
#> 5 1.94   62.9  1.09  1.08  9.94
#> 6 2.42   45.2  1.08  1.08 10.4 
#> # ℹ 94 more rows

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(mod3, ev,covsInterpolation="locf")
print(r1)
#> ── Solved rxode2 object ──
#> ── Parameters ($params): ──
#>         KA         CL         V2          Q         V3       Kin0       Kout 
#>   0.294000  18.600000  40.200000  10.500000 297.000000   1.000000   1.000000 
#>       EC50         Tz        amp         pi 
#> 200.000000   8.000000   0.100000   3.141593 
#> ── Initial Conditions ($inits): ──
#> eff 
#>   1 
#> ── First part of data (object): ──
#> # A tibble: 100 × 5
#>    time    C2   Kin   eff ctime
#>     [h] <dbl> <dbl> <dbl>   [h]
#> 1 0     249.   1.1   1     8   
#> 2 0.485 175.   1.10  1.04  8.48
#> 3 0.970 124.   1.10  1.06  8.97
#> 4 1.45   88.0  1.09  1.08  9.45
#> 5 1.94   62.9  1.09  1.08  9.94
#> 6 2.42   45.2  1.08  1.08 10.4 
#> # ℹ 94 more rows

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(mod3, ev,covsInterpolation="nocb")
print(r1)
#> ── Solved rxode2 object ──
#> ── Parameters ($params): ──
#>         KA         CL         V2          Q         V3       Kin0       Kout 
#>   0.294000  18.600000  40.200000  10.500000 297.000000   1.000000   1.000000 
#>       EC50         Tz        amp         pi 
#> 200.000000   8.000000   0.100000   3.141593 
#> ── Initial Conditions ($inits): ──
#> eff 
#>   1 
#> ── First part of data (object): ──
#> # A tibble: 100 × 5
#>    time    C2   Kin   eff ctime
#>     [h] <dbl> <dbl> <dbl>   [h]
#> 1 0     249.   1.1   1     8   
#> 2 0.485 175.   1.10  1.04  8.48
#> 3 0.970 124.   1.10  1.06  8.97
#> 4 1.45   88.0  1.09  1.07  9.45
#> 5 1.94   62.9  1.09  1.08  9.94
#> 6 2.42   45.2  1.08  1.08 10.4 
#> # ℹ 94 more rows

which gives the following plots:

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

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