---
title: "Methods Used for Dose-Aware Concentration Interpolation/Extrapolation"
author: "Bill Denney"
output:
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 6
vignette: >
  %\VignetteIndexEntry{Methods Used for Dose-Aware Concentration Interpolation/Extrapolation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(PKNCA)
library(dplyr)
```

# Introduction

Interpolation and extrapolation with awareness of doses occurring before, at the same time of, or after the requested interpolation time point must account for many interactions.  To ensure clarity in the interpolation/extrapolation methods and the decisions made by the algorithm, each potential choice is listed below with its accompanying calculation method.  The code used to generate the table is the same as the code within the function.

```{r generatecombs, echo=FALSE}
method.choices <- names(PKNCA:::interp.extrap.conc.dose.select)
method.choices <-
  factor(method.choices, levels=method.choices, ordered=TRUE)
         
all_combs <-
  expand.grid(event_before=setdiff(unlist(PKNCA:::event_choices_interp.extrap.conc.dose), "output_only"),
              event=setdiff(unlist(PKNCA:::event_choices_interp.extrap.conc.dose), "none"),
              event_after=setdiff(unlist(PKNCA:::event_choices_interp.extrap.conc.dose), "output_only"),
              Method="",
              stringsAsFactors=FALSE)

for (n in method.choices) {
  mask <-
    do.call(PKNCA:::interp.extrap.conc.dose.select[[n]]$select,
            list(x=all_combs),
            envir=environment(pk.nca))
  all_combs$Method[mask] <- n
}

all_combs <-
  all_combs[do.call(order,
                    args=append(as.list(all_combs), list(na.last=FALSE))),]
```

# Methods

The method list below is described and sorted in order of how many scenarios the method is applied to in the list.

For each of the summary tables below, the column headers are as follows:

* **Event Before**: The type of event before the time of the requested output may be one of the following
* **Event At**: The equivalent to "Event Before" but for the event occurring at the requested output time.
* **Event After**: The equivalent to "Event Before" but for the next event occurring after the requested output time.

```{r methodsummary, results="asis", echo=FALSE}
methodorder <- names(sort(summary(factor(all_combs$Method)), decreasing=TRUE))

for (n in methodorder) {
  cat("## ", n, "\n\n", sep="")
  cat(PKNCA:::interp.extrap.conc.dose.select[[n]]$description, "\n\n", sep="")
  print(knitr::kable(
    all_combs[all_combs$Method %in% n,
              c("event_before","event", "event_after")],
    row.names=FALSE,
    col.names=c("Event Before", "Event At", "Event After")))
  cat("\n")
}
```

# Appendix: Complete Methods Table

```{r methodlisting, echo=FALSE}
knitr::kable(
  all_combs[,c("event_before", "event", "event_after", "Method")],
    row.names=FALSE,
    col.names=c("Event Before", "Event At", "Event After", "Method Used"))
```
