---
title: "Computing NCA Parameters for Theophylline"
author: "Bill Denney"
output:
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 6
vignette: >
  %\VignetteIndexEntry{Computing NCA Parameters for Theophylline}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, echo=FALSE, include=FALSE}
library(PKNCA)
library(knitr)
```

Examples simplify understanding.  Below is an example of how to use the theophylline dataset to generate NCA parameters.

# Load the data

```{r showtheoph}
## It is always a good idea to look at the data
knitr::kable(head(datasets::Theoph))
```

The columns that we will be interested in for our analysis are conc, Time, and Subject in the concentration data set and Dose, Time, and Subject for the dosing data set.

```{r setupconcdose}
## By default it is groupedData; convert it to a data frame for use
conc_obj <- PKNCAconc(as.data.frame(datasets::Theoph), conc~Time|Subject)

## Dosing data needs to only have one row per dose, so subset for
## that first.
d_dose <- unique(datasets::Theoph[datasets::Theoph$Time == 0,
                                  c("Dose", "Time", "Subject")])
knitr::kable(d_dose,
             caption="Example dosing data extracted from theophylline data set")
dose_obj <- PKNCAdose(d_dose, Dose~Time|Subject)
```

# Merge the Concentration and Dose

After loading the data, they must be combined to prepare for parameter calculation.  Intervals for calculation will automatically be selected based on the `single.dose.aucs setting` in `PKNCA.options`

```{r autointervals}
data_obj_automatic <- PKNCAdata(conc_obj, dose_obj)
knitr::kable(PKNCA.options("single.dose.aucs"))
knitr::kable(data_obj_automatic$intervals)
```

Intervals for calculation can also be specified manually.  Manual specification requires at least columns for `start` time, `end` time, and the parameters requested.  The manual specification can also include any grouping factors from the concentration data set.  Column order of the intervals is not important.  When intervals are manually specified, they are expanded to the full interval set when added to a PKNCAdata object (in other words, a column is created for each parameter.  Also, PKNCA automatically calculates parameters required for the NCA, so while lambda.z is required for calculating AUC~0-$\infty$~, you do not have to specify it in the parameters requested.

```{r manualintervals}
intervals_manual <- data.frame(start=0,
                               end=Inf,
                               cmax=TRUE,
                               tmax=TRUE,
                               aucinf.obs=TRUE,
                               auclast=TRUE)
data_obj_manual <- PKNCAdata(conc_obj, dose_obj,
                             intervals=intervals_manual)
knitr::kable(data_obj_manual$intervals)
```

# Compute the parameters

Parameter calculation will automatically split the data by the grouping factor(s), subset by the interval, calculate all required parameters, record all options used for the calculations, and include data provenance to show that the calculation was performed as described.  For all this, just call the `pk.nca` function with your PKNCAdata object.

```{r calculationauto}
results_obj_automatic <- pk.nca(data_obj_automatic)
knitr::kable(head(as.data.frame(results_obj_automatic)))
```
```{r calculationautoshow, eval=FALSE}
summary(results_obj_automatic)
```
```{r calculationautoshowpretty, echo=FALSE}
## Make a pretty table instead of the data.frame preformatted printout
knitr::kable(summary(results_obj_automatic))
```

```{r calculationmanual}
results_obj_manual <- pk.nca(data_obj_manual)
knitr::kable(head(as.data.frame(results_obj_manual)))
```
```{r calculationmanualshow, eval=FALSE}
summary(results_obj_manual)
```
```{r calculationmanualshowpretty, echo=FALSE}
## Make a pretty table instead of the data.frame preformatted printout
knitr::kable(summary(results_obj_manual))
```

# Multiple Dose Example

Assessing multiple dose pharmacokinetics is conceptually the same as single-dose in PKNCA.

To assess multiple dose PK, the theophylline data will be extended from single to multiple doses using superposition (see the [superposition](v20-superposition.html) vignette for more information).

```{r superposition}
d_conc <- PKNCAconc(as.data.frame(Theoph), conc~Time|Subject)
conc_obj_multi <-
  PKNCAconc(
    superposition(d_conc,
                  tau=168,
                  dose.times=seq(0, 144, by=24),
                  n.tau=1,
                  check.blq=FALSE),
    conc~time|Subject)
conc_obj_multi
dose_obj_multi <- PKNCAdose(expand.grid(Subject=unique(as.data.frame(conc_obj_multi)$Subject),
                                      time=seq(0, 144, by=24)),
                          ~time|Subject)
dose_obj_multi
```

The superposition-simulated scenario is not especially realistic as it includes dense sampling on every day.  With this scenario, the intervals automatically selected have an interval for every subject on every day.

```{r multi_auto_choose_intervals}
data_obj <- PKNCAdata(conc_obj_multi, dose_obj_multi)
data_obj$intervals[,c("Subject", "start", "end")]
```

In a more realistic scenario, dense PK sampling occurs for every subject on the first and last days.  To select those intervals manually, specify the intervals of interest in the `intervals` argument to the PKNCAdata function call.  The intervals are automatically expanded not to calculate anything that was not requested.

```{r multi_manual_choose_intervals}
intervals_manual <- data.frame(start=c(0, 144),
                               end=c(24, 168),
                               cmax=TRUE,
                               auclast=TRUE)
data_obj <- PKNCAdata(conc_obj_multi, dose_obj_multi,
                      intervals=intervals_manual)
data_obj$intervals
```

After the data is ready, the calculations and summary can progress.

```{r multi_calc}
results_obj <- pk.nca(data_obj)
print(results_obj)

summary(results_obj)
```
