---
title: "Noncompartmental Analysis"
author: "Devin Pastoor"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Simple Noncompartmental Analysis}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

For initial analysis, or in times where more complex analysis - such as nonlinear mixed effects
modeling - is unnecessary, noncompartmental analysis is frequently used. While there are R packages, such as `metrumrg` or `PK` packages, the functionality is too complex and does not provide the flexibility to easily perform most of the basic functions needed without massaging or wringing your data into the necessary formats. The NCA functions in `PKPDmisc` are designed to be robust, but generalizable, allowing multiple data designs and software to be easily utilized.

In addition, while it is possible to use these functions in base R, it is highly recommended to use `dplyr` to supplement much of the surrounding data manipulation for both speed and clarity.

While other resources are available to get started with dplyr, briefly, the general workflow will be as follows:

* take raw data
* filter unnecessary data
* group data
* use summarize or mutate for the calculations.

However, there are certain analyses that are frequently used enough that `PKPDmisc` provides wrapper functions to reduce the above steps to a single line of code.

For those getting accustomed to R, keep in mind that there is also simple examples provided with each function that are available by doing `?<function>`, so for example `?auc_partial`.

For the examples, the `sd_oral_richpk` dataset from `PKPDdatasets` will be used.

To install, run `devtools::install_github("dpastoor/PKPDdatasets")`

```{r}
library("PKPDmisc")
library("dplyr")
pkdat <- sd_oral_richpk
head(pkdat)
```

# Common summary functions
For a number of tasks that are commonly run, summary functions reduce the number of steps necessary. While I will not highlight each here, a quick glance at the package source code can show which functions are available. The API follows that functions named `s_<function>` will provide these multiple steps at once. 

In general, each function is designed to take a *grouped dataframe* from `dplyr` to provide the function insight as to how to handle the overall data, so in general the analysis will will look like:

```
df %>% group_by(<grouping var>) %>% s_<function>()
```

The first such function is `s_pauc()`. This function can give multiple partial auc values, with each column being named `pAUC<tfirst>_<tlast>`. Multiple partial AUC slices can be requested by passing a list of requested times.

```{r }
pkdat %>% group_by(ID) %>% 
  s_pauc(Time, Conc, list(c(0, 8), c(0, 24), c(8, 24))) %>%
  head
```

Similar to `dplyr` and `ggplot2` the summary functions use non-standard evaluation (no quotes required), however this is not conducive to programming or for use inside of other functions. As such, a standard evaluation version is provided with the nomenclature `s_<function>_`

```{r}
paucs <- pkdat %>% group_by(ID) %>% 
  s_pauc_("Time", "Conc", list(c(0, 8), c(0, 24), c(8, 24)), digits =1)
head(paucs)
```


Often once AUC or cmax statistics have been calculated, quantiles are calculated to create quantile plots or other summary evaluations. Here, the `s_quantiles` function can be used.


```{r}
paucs %>% ungroup() %>% s_quantiles(pAUC0_24, probs = c(0, 0.25, 0.5, 0.75, 1))
paucs %>% ungroup() %>% s_quantiles_("pAUC0_24", probs = c(0, 0.25, 0.5, 0.75, 1))
```

```{r}
devtools::session_info()
```
