---
title: "RxODE Covariates"
author: "Matthew L. Fidler, Melissa Hallow, and Wenping Wang"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{RxODE Covariates}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo=FALSE}
options(cli.unicode=FALSE, crayon.enabled=FALSE);
options(knitr.table.format = "html")
htmltools::img(src = knitr::image_uri("logo.png"), 
               alt = 'RxODE', 
               style = 'position:absolute; top:0; right:0; padding:10px; border: 0;')
```

Covariates are easy to specify in RxODE, 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:


```{r}
library(RxODE)
mod3 <- RxODE({
    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=2) %>%
    add.sampling(seq(0,48,length.out=100));


 ## Create data frame of  8 am dosing for the first dose
cov.df  <- data.frame(ctime =(seq(0,48,length.out=100)+8) %% 24);

```

Now there is a covariate present, the system can be solved using the cov option

```{r,results="asis"}

r1 <- solve(mod3, ev, covs=cov.df,covs_interpolation="linear")
rxHtml(r1)
```

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

```{r}
library(ggplot2)
ggplot(r1,aes(time,C2)) + geom_line() + ylab("Central Concentration") + xlab("Time");
```

```{r}
ggplot(r1,aes(time,eff)) + geom_line() + 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.

In RxODE, covariate interpolation can also be the last observation
carried forward, or constant approximation.  This is equivalent to R's
`approxfun` with `method="constant"`.


r```{r,results="asis"}
r2 <- solve(mod3, ev, covs=cov.df,covs_interpolation="constant")
rxHtml(r2)
```

which gives the following plots:

```{r}
ggplot(r1,aes(time,C2)) + geom_line() + ylab("Central Concentration") + xlab("Time");
```

```{r}
ggplot(r1,aes(time,eff)) + geom_line() + 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):
eeee
```{r,results="asis"}
r2 <- solve(mod3, ev, covs=cov.df,covs_interpolation="nocb")
rxHtml(r2)
```

which gives the following plots:

```{r}
ggplot(r1,aes(time,C2)) + geom_line() + ylab("Central Concentration") + xlab("Time");
```

```{r}
ggplot(r1,aes(time,eff)) + geom_line() + ylab("Effect") + xlab("Time");
```


