# Noncompartmental Analysis

#### Devin Pastoor

#### 2020-04-03

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.     386.     181.
    ## 2     2    523.    1203.     680.
    ## 3     3    316.     466.     150.
    ## 4     4    564.    1220.     655.
    ## 5     5    302.     488.     186.
    ## 6     6    154.     295.     141.

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.     386.     181.
    ## 2     2    523.    1204.     680.
    ## 3     3    316.     466.     150.
    ## 4     4    564.    1220.     656.
    ## 5     5    302.     488.     186.
    ## 6     6    154      295.     141.

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.         457.         590.         887.         1377.

    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.         457.         590.         887.         1377.

    sessioninfo::session_info()

    ## ─ Session info ───────────────────────────────────────────────────────────────
    ##  setting  value
    ##  version  R version 3.6.2 (2019-12-12)
    ##  os       macOS Catalina 10.15.3
    ##  system   x86_64, darwin15.6.0
    ##  ui       X11
    ##  language (EN)
    ##  collate  en_US.UTF-8
    ##  ctype    en_US.UTF-8
    ##  tz       America/New_York
    ##  date     2020-04-03
    ##
    ## ─ Packages ───────────────────────────────────────────────────────────────────
    ##  package     * version date       lib source
    ##  assertthat    0.2.1   2019-03-21 [3] CRAN (R 3.6.0)
    ##  cli           2.0.1   2020-01-08 [3] CRAN (R 3.6.0)
    ##  colorspace    1.4-1   2019-03-18 [3] CRAN (R 3.6.0)
    ##  crayon        1.3.4   2017-09-16 [3] CRAN (R 3.6.0)
    ##  digest        0.6.25  2020-02-23 [3] CRAN (R 3.6.0)
    ##  dplyr       * 0.8.4   2020-01-31 [3] CRAN (R 3.6.0)
    ##  evaluate      0.14    2019-05-28 [3] CRAN (R 3.6.0)
    ##  fansi         0.4.1   2020-01-08 [3] CRAN (R 3.6.0)
    ##  farver        2.0.3   2020-01-16 [3] CRAN (R 3.6.0)
    ##  ggplot2     * 3.2.1   2019-08-10 [3] CRAN (R 3.6.0)
    ##  glue          1.3.1   2019-03-12 [3] CRAN (R 3.6.0)
    ##  gtable        0.3.0   2019-03-25 [3] CRAN (R 3.6.0)
    ##  htmltools     0.4.0   2019-10-04 [3] CRAN (R 3.6.0)
    ##  knitr         1.28    2020-02-06 [3] CRAN (R 3.6.0)
    ##  labeling      0.3     2014-08-23 [3] CRAN (R 3.6.0)
    ##  lazyeval      0.2.2   2019-03-15 [3] CRAN (R 3.6.0)
    ##  lifecycle     0.1.0   2019-08-01 [3] CRAN (R 3.6.0)
    ##  magrittr      1.5     2014-11-22 [3] CRAN (R 3.6.0)
    ##  munsell       0.5.0   2018-06-12 [3] CRAN (R 3.6.0)
    ##  pillar        1.4.3   2019-12-20 [3] CRAN (R 3.6.0)
    ##  pkgconfig     2.0.3   2019-09-22 [3] CRAN (R 3.6.0)
    ##  PKPDmisc    * 2.1.2   2020-04-03 [1] local
    ##  purrr       * 0.3.3   2019-10-18 [3] CRAN (R 3.6.0)
    ##  R6            2.4.1   2019-11-12 [3] CRAN (R 3.6.0)
    ##  Rcpp          1.0.3   2019-11-08 [3] CRAN (R 3.6.0)
    ##  rlang         0.4.4   2020-01-28 [3] CRAN (R 3.6.0)
    ##  rmarkdown     2.1     2020-01-20 [3] CRAN (R 3.6.0)
    ##  scales        1.1.0   2019-11-18 [3] CRAN (R 3.6.0)
    ##  sessioninfo   1.1.1   2018-11-05 [3] CRAN (R 3.6.0)
    ##  stringi       1.4.6   2020-02-17 [3] CRAN (R 3.6.0)
    ##  stringr       1.4.0   2019-02-10 [3] CRAN (R 3.6.0)
    ##  tibble        2.1.3   2019-06-06 [3] CRAN (R 3.6.0)
    ##  tidyselect    1.0.0   2020-01-27 [3] CRAN (R 3.6.0)
    ##  utf8          1.1.4   2018-05-24 [3] CRAN (R 3.6.0)
    ##  vctrs         0.2.3   2020-02-20 [3] CRAN (R 3.6.0)
    ##  withr         2.1.2   2018-03-15 [3] CRAN (R 3.6.0)
    ##  xfun          0.12    2020-01-13 [3] CRAN (R 3.6.0)
    ##  yaml          2.2.1   2020-02-01 [3] CRAN (R 3.6.0)
    ##
    ## [1] /private/var/folders/bf/1cwy7ctd2k1dj13rc3v8znbh0000gn/T/RtmpFJrJZ4/Rinst1231a5b34581c
    ## [2] /private/var/folders/bf/1cwy7ctd2k1dj13rc3v8znbh0000gn/T/Rtmpj8bi6z/temp_libpath12292484b69c1
    ## [3] /Users/devinp/rpkgs/3.6
    ## [4] /Library/Frameworks/R.framework/Versions/3.6/Resources/library