# Noncompartmental Analysis

#### Devin Pastoor

#### 2017-12-17

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")`

    library("PKPDmisc")
    library("dplyr")
    pkdat <- sd_oral_richpk
    head(pkdat)

    ##   ID Time  Amt      Conc      Age   Weight Gender     Race Dose
    ## 1  1 0.00 5000  0.000000 56.09591 94.19649   Male Hispanic 5000
    ## 2  1 0.25    0  8.612809 56.09591 94.19649   Male Hispanic 5000
    ## 3  1 0.50    0 19.436818 56.09591 94.19649   Male Hispanic 5000
    ## 4  1 1.00    0 34.006699 56.09591 94.19649   Male Hispanic 5000
    ## 5  1 2.00    0 30.228800 56.09591 94.19649   Male Hispanic 5000
    ## 6  1 3.00    0 31.299610 56.09591 94.19649   Male Hispanic 5000

# 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.

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

    ## # A tibble: 6 x 4
    ## # Groups:   ID [6]
    ##      ID  pAUC0_8  pAUC0_24 pAUC8_24
    ##   <int>    <dbl>     <dbl>    <dbl>
    ## 1     1 204.2096  385.5759 181.3663
    ## 2     2 523.2355 1203.4948 680.2593
    ## 3     3 315.8860  466.3116 150.4255
    ## 4     4 564.1666 1219.6364 655.4698
    ## 5     5 301.9122  487.5681 185.6559
    ## 6     6 154.0215  295.2480 141.2265

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>_`

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

    ## # A tibble: 6 x 4
    ## # Groups:   ID [6]
    ##      ID pAUC0_8 pAUC0_24 pAUC8_24
    ##   <int>   <dbl>    <dbl>    <dbl>
    ## 1     1   204.2    385.6    181.4
    ## 2     2   523.2   1203.5    680.3
    ## 3     3   315.9    466.3    150.4
    ## 4     4   564.2   1219.6    655.5
    ## 5     5   301.9    487.6    185.7
    ## 6     6   154.0    295.2    141.2

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.

    paucs %>% ungroup() %>% s_quantiles(pAUC0_24, probs = c(0, 0.25, 0.5, 0.75, 1))

    ## # A tibble: 1 x 5
    ##   pAUC0_24_q0 pAUC0_24_q25 pAUC0_24_q50 pAUC0_24_q75 pAUC0_24_q100
    ##         <dbl>        <dbl>        <dbl>        <dbl>         <dbl>
    ## 1       161.4       456.65        590.3      886.975        1376.9

    paucs %>% ungroup() %>% s_quantiles_("pAUC0_24", probs = c(0, 0.25, 0.5, 0.75, 1))

    ## # A tibble: 1 x 5
    ##   pAUC0_24_q0 pAUC0_24_q25 pAUC0_24_q50 pAUC0_24_q75 pAUC0_24_q100
    ##         <dbl>        <dbl>        <dbl>        <dbl>         <dbl>
    ## 1       161.4       456.65        590.3      886.975        1376.9

    devtools::session_info()

    ## Session info -------------------------------------------------------------

    ##  setting  value
    ##  version  R version 3.4.0 (2017-04-21)
    ##  system   x86_64, darwin16.5.0
    ##  ui       unknown
    ##  language (EN)
    ##  collate  C
    ##  tz       America/New_York
    ##  date     2017-12-17

    ## Packages -----------------------------------------------------------------

    ##  package    * version    date       source
    ##  assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)
    ##  backports    1.1.0      2017-05-22 CRAN (R 3.4.0)
    ##  base       * 3.4.0      2017-05-04 local
    ##  bindr        0.1        2016-11-13 cran (@0.1)
    ##  bindrcpp   * 0.2        2017-06-17 cran (@0.2)
    ##  colorspace   1.3-2      2016-12-14 CRAN (R 3.4.0)
    ##  compiler     3.4.0      2017-05-04 local
    ##  datasets   * 3.4.0      2017-05-04 local
    ##  devtools     1.13.2     2017-06-02 CRAN (R 3.4.0)
    ##  digest       0.6.12     2017-01-27 CRAN (R 3.4.0)
    ##  dplyr      * 0.7.3      2017-09-09 CRAN (R 3.4.0)
    ##  evaluate     0.10.1     2017-06-24 CRAN (R 3.4.0)
    ##  ggplot2    * 2.2.1      2016-12-30 CRAN (R 3.4.0)
    ##  glue         1.1.1      2017-06-21 cran (@1.1.1)
    ##  graphics   * 3.4.0      2017-05-04 local
    ##  grDevices  * 3.4.0      2017-05-04 local
    ##  grid         3.4.0      2017-05-04 local
    ##  gtable       0.2.0      2016-02-26 CRAN (R 3.4.0)
    ##  htmltools    0.3.6      2017-04-28 CRAN (R 3.4.0)
    ##  knitr        1.17       2017-08-10 CRAN (R 3.4.0)
    ##  labeling     0.3        2014-08-23 CRAN (R 3.4.0)
    ##  lazyeval     0.2.0      2016-06-12 CRAN (R 3.4.0)
    ##  magrittr     1.5        2014-11-22 CRAN (R 3.4.0)
    ##  memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)
    ##  methods    * 3.4.0      2017-05-04 local
    ##  munsell      0.4.3      2016-02-13 CRAN (R 3.4.0)
    ##  pkgconfig    2.0.1      2017-03-21 cran (@2.0.1)
    ##  PKPDmisc   * 2.1.1      2017-12-17 local
    ##  plyr         1.8.4      2016-06-08 CRAN (R 3.4.0)
    ##  purrr      * 0.2.3      2017-08-02 CRAN (R 3.4.0)
    ##  R6           2.2.2.9000 2017-11-21 Github (r-lib/R6@589c60e)
    ##  Rcpp         0.12.12    2017-07-15 CRAN (R 3.4.0)
    ##  rlang        0.1.2.9000 2017-09-11 Github (tidyverse/rlang@d165894)
    ##  rmarkdown    1.6        2017-06-15 CRAN (R 3.4.0)
    ##  rprojroot    1.2        2017-01-16 CRAN (R 3.4.0)
    ##  rstudioapi   0.7.0-9000 2017-09-11 Github (rstudio/rstudioapi@8e8bfb0)
    ##  scales       0.4.1      2016-11-09 CRAN (R 3.4.0)
    ##  stats      * 3.4.0      2017-05-04 local
    ##  stringi      1.1.5      2017-04-07 CRAN (R 3.4.0)
    ##  stringr      1.2.0      2017-02-18 CRAN (R 3.4.0)
    ##  tibble       1.3.4      2017-08-22 cran (@1.3.4)
    ##  tools        3.4.0      2017-05-04 local
    ##  utils      * 3.4.0      2017-05-04 local
    ##  withr        2.0.0      2017-07-28 cran (@2.0.0)
    ##  yaml         2.1.14     2016-11-12 CRAN (R 3.4.0)