---
title: "Selection of Calculation Intervals"
author: "Bill Denney"
output:
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 6
vignette: >
  %\VignetteIndexEntry{Selection of Calculation Intervals}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE,
                      fig.width=6,
                      fig.height=4)
library(cowplot)
library(PKNCA)
library(knitr)
library(ggplot2)
source("../tests/testthat/helper-generate_data.R")
scale_colour_discrete <- scale_colour_hue
scale_fill_discrete <- scale_fill_hue
```

```{r check-ggplot, include=!requireNamespace("ggplot2"), results="asis"}
cat("ggplot2 is required for this vignette to work correctly.  Please install the ggplot2 library and retry building the vignette.")
```

# Introduction

PKNCA considers two types of data grouping within data sets:  the group and the interval.  A group typically identifies a single subject given a single intervention type (a "treatment") with a single analyte.  An interval subsets a group by times within the group, and primary noncompartmental analysis (NCA) calculations are performed within an interval.

As a concrete example, consider the figure below shows the concentration-time profile of a study subject in a multiple-dose study.  The group is all points in the figure, and the interval for the last day (144 to 168 hr) is the area with blue shading.

```{r intro_interval_plot, echo=FALSE}
# Simulate concentration-time data and setup the PKNCAconc object
d_conc <-
  PKNCAconc(
    generate.conc(nsub=1,
                  ntreat=1,
                  time.points=c(0, 1, 2, 4, 6, 8, 12, 24, 36, 48),
                  nstudies=1,
                  nanalytes=1,
                  resid=0),
    conc~time|treatment+ID)
print(d_conc)
# Use superposition to simulate multiple doses
d_conc_multi <-
  superposition(d_conc,
                tau=168,
                dose.times=seq(0, 168-24, by=24),
                n.tau=1)
```
```{r intro_interval_plot-visualization, eval=requireNamespace("ggplot2")}
# Plot the concentration-time data and the interval
ggplot(d_conc_multi, aes(x=time, y=conc)) +
  geom_ribbon(data=d_conc_multi[d_conc_multi$time >= 144,],
              aes(ymax=conc, ymin=0),
              fill="skyblue") +
  geom_point() + geom_line() +
  scale_x_continuous(breaks=seq(0, 168, by=12)) +
  scale_y_continuous(limits=c(0, NA)) +
  labs(x="Time Since First Dose (hr)",
       y="Concentration\n(arbitrary units)")
```
```{r intro_interval_spec}
intervals_manual <- data.frame(start=144, end=168, auclast=TRUE)
knitr::kable(intervals_manual)

PKNCAdata(d_conc, intervals=intervals_manual)
```

# Group Matching

Group matching occurs by matching all overlapping column names between the groups and the interval data.frame.  (Note that grouping columns cannot be the word `start`, `end`, or share a name with an NCA parameter.)

## Selecting the Subjects for an Interval

The groups for an interval prepare for summarization.  Typically the groups will take a structure similar to the preferred summarization structure with groups nested in the logical method for summary.  As an example, the group structure may be: study, treatment, day, analyte, and subject.  The grouping names for an interval must be the same as or a subset of the grouping names used for the concentration data.

As the matching occurs with all available columns, the grouping columns names are only required to the level of specificity for the calculations desired.  As an example, if you want AUC~inf,obs~ in subjects who received single doses and AUC~last~ on days 1 (0 to 24 hours) and 10 (216 to 240 hours) in subjects who received multiple doses, with treatment defined as "Drug 1 Single" or "Drug 1 Multiple", the intervals could be defined as below.

```{r select_group}
intervals_manual <-
  data.frame(
    treatment=c("Drug 1 Single", "Drug 1 Multiple", "Drug 1 Multiple"),
    start=c(0, 0, 216),
    end=c(Inf, 24, 240),
    aucinf.obs=c(TRUE, FALSE, FALSE),
    auclast=c(FALSE, TRUE, TRUE)
  )
knitr::kable(intervals_manual)
```

# Intervals

Intervals are defined by `data.frame`s with one row per interval, zero or more columns to match the groups from the `PKNCAdata` object, and one or more NCA parameters to calculate.

Selection of points within an interval occurs by choosing any point at or after the `start` and at or before the `end`.

## To Infinity

The end of an interval may be infinity.  An interval to infinity works the same as any other interval in that points are selected by being at or after the `start` and at or before the `end` of the interval.  Selecting `Inf` or any value at or after the maximum time yields no difference in effect, but `Inf` is simpler when scripting to ensure that all points are selected.

```{r infinity_interval_plot, echo=FALSE}
# Simulate concentration-time data and setup the PKNCAconc object
d_conc <-
  PKNCAconc(
    generate.conc(nsub=1,
                  ntreat=1,
                  time.points=c(0, 1, 2, 4, 6, 8, 12, 24, 36, 48),
                  nstudies=1,
                  nanalytes=1,
                  resid=0),
    conc~time|treatment+ID)
print(d_conc)
```
```{r infinity_interval_plot-visualization, eval=requireNamespace("ggplot2")}
# Use superposition to simulate multiple doses
ggplot(as.data.frame(d_conc)[as.data.frame(d_conc)$time <= 48,], aes(x=time, y=conc)) +
  geom_ribbon(data=as.data.frame(d_conc),
              aes(ymax=conc, ymin=0),
              fill="skyblue") +
  geom_point() + geom_line() +
  scale_x_continuous(breaks=seq(0, 72, by=12)) +
  scale_y_continuous(limits=c(0, NA)) +
  labs(x="Time Since First Dose (hr)",
       y="Concentration\n(arbitrary units)")
```
```{r infinity_interval_spec}
intervals_manual <-
  data.frame(
    start=0,
    end=Inf,
    auclast=TRUE,
    aucinf.obs=TRUE
  )
print(intervals_manual)

my.data <- PKNCAdata(d_conc, intervals=intervals_manual)
```

## Multiple Intervals

More than one interval may be specified for the same subject or group of subjects by providing more than one row of interval specifications.  In the figure below, the blue and green shaded regions indicate the first and second rows of the intervals, respectively.

```{r multiple_intervals_plot, echo=FALSE}
# Simulate concentration-time data and setup the PKNCAconc object
d_conc <-
  PKNCAconc(
    generate.conc(nsub=1,
                  ntreat=1,
                  time.points=c(0, 1, 2, 4, 6, 8, 12, 24, 36, 48),
                  nstudies=1,
                  nanalytes=1,
                  resid=0),
    conc~time|treatment+ID)
print(d_conc)
# Use superposition to simulate multiple doses
d_conc_multi <-
  superposition(d_conc,
                tau=168,
                dose.times=seq(0, 168-24, by=24),
                n.tau=1)
```
```{r multiple_intervals_plot-visualization, eval=requireNamespace("ggplot2")}
# Plot the concentration-time data and the interval
ggplot(d_conc_multi, aes(x=time, y=conc)) +
  geom_ribbon(data=d_conc_multi[d_conc_multi$time <= 24,],
              aes(ymax=conc, ymin=0),
              fill="skyblue") +
  geom_ribbon(data=d_conc_multi[d_conc_multi$time >= 144,],
              aes(ymax=conc, ymin=0),
              fill="lightgreen") +
  geom_point() + geom_line() +
  scale_x_continuous(breaks=seq(0, 168, by=12)) +
  scale_y_continuous(limits=c(0, NA)) +
  labs(x="Time Since First Dose (hr)",
       y="Concentration\n(arbitrary units)")
```
```{r multiple_intervals_spec}
intervals_manual <-
  data.frame(
    start=c(0, 144),
    end=c(24, 168),
    auclast=TRUE
  )
knitr::kable(intervals_manual)

my.data <- PKNCAdata(d_conc, intervals=intervals_manual)
```

# Overlapping Intervals and Different Calculations by Interval

In some scenarios, multiple intervals may be needed where some intervals overlap.  There is no issue with an interval specification that has two rows with overlapping times; the rows are considered separately.  In the example below, the 0-24 interval is shared between both the first and second (shaded blue-green).

The example of overlapping intervals also illustrates that different calculations can be performed in different intervals.  In this case, `auclast` is calculated in both intervals while `aucinf.obs` is only calculated in the 0-Inf interval.

```{r overlapping_intervals_plot, echo=FALSE}
# Simulate concentration-time data and setup the PKNCAconc object
d_conc <-
  PKNCAconc(
    generate.conc(nsub=1,
                  ntreat=1,
                  time.points=c(0, 1, 2, 4, 6, 8, 12, 24, 36, 48),
                  nstudies=1,
                  nanalytes=1,
                  resid=0),
    conc~time|treatment+ID)
print(d_conc)
```
```{r overlapping_intervals_plot-visualization, eval=requireNamespace("ggplot2")}
# Use superposition to simulate multiple doses
ggplot(as.data.frame(d_conc), aes(x=time, y=conc)) +
  geom_ribbon(data=as.data.frame(d_conc),
              aes(ymax=conc, ymin=0),
              fill="lightgreen",
              alpha=0.5) +
  geom_ribbon(data=as.data.frame(d_conc)[as.data.frame(d_conc)$time <= 24,],
              aes(ymax=conc, ymin=0),
              fill="skyblue",
              alpha=0.5) +
  geom_point() + geom_line() +
  scale_x_continuous(breaks=seq(0, 168, by=12)) +
  scale_y_continuous(limits=c(0, NA)) +
  labs(x="Time Since First Dose (hr)",
       y="Concentration\n(arbitrary units)")
```
```{r overlapping_intervals_spec}
intervals_manual <-
  data.frame(
    start=0,
    end=c(24, Inf),
    auclast=TRUE,
    aucinf.obs=c(FALSE, TRUE)
  )
knitr::kable(intervals_manual)

my.data <- PKNCAdata(d_conc, intervals=intervals_manual)
```

# Intervals with Duration

Some events have durations of times rather than instants in time associated with them.  Two typical examples of duration data in NCA are intravenous infusions and urine or fecal sample collections.  Inform PKNCA of durations with the `duration` argument to the `PKNCAdose` and `PKNCAconc` functions.

Durations data are selected based on both the beginning and ending of the duration existing within the interval.

```{r interval_yes_no, echo=FALSE, eval=requireNamespace("ggplot2")}
ggplot_intervals <- function(definition, intervals) {
  intervals$within <-
    c("No", "Yes")[intervals$interval.start >= min(definition$start.x) &
                     intervals$interval.end >= max(definition$start.x)]
  intervals$within <-
    factor(intervals$within, levels=c("Yes", "No"))
  ggplot(intervals,
         aes(x=interval.start,
             xend=interval.end,
             y=y,
             yend=y,
             linetype=within)) +
    geom_segment() +
    geom_segment(data=definition,
                 mapping=aes(x=start.x, xend=start.x, y=start.y, yend=end.y),
                 arrow=arrow(),
                 inherit.aes=FALSE) +
    geom_point(aes(x=interval.start, y=1),
               shape="|",
               size=4) +
    geom_point(aes(x=max(interval.end), y=1),
               shape="|",
               size=4) +
    scale_y_continuous(breaks=NULL) +
    labs(x="Time",
         y=NULL)
}

interval_definition <-
  data.frame(start.x=c(0, 24),
             start.y=0.5,
             end.y=0.9)
show_intervals <-
  data.frame(interval.start=c(0, 4, 12, 24),
             interval.end=c(4, 12, 24, 48),
             y=1)
ggplot_intervals(interval_definition,
                 show_intervals)

interval_definition <-
  data.frame(start.x=c(0, 16),
             start.y=0.5,
             end.y=0.9)
show_intervals <-
  data.frame(interval.start=c(0, 4, 12, 24),
             interval.end=c(4, 12, 24, 48),
             y=1)
ggplot_intervals(interval_definition,
                 show_intervals)

```

# Parameters Available for Calculation in an Interval

The following parameters are available in an interval.  For more information about the parameter, see the documentation for the function.

```{r parameters-available, echo=FALSE}
interval_spec <- get.interval.cols()
interval_spec <- interval_spec[sort(names(interval_spec))]

get_function_for_calc <- function(x) {
  if (is.na(x$FUN)) {
    if (is.null(x$depends)) {
      "(none)"
    } else {
      paste("See the parameter name", x$depends)
    }
  } else {
    x$FUN
  }
}

kable(
  data.frame(
    `Parameter Name`=names(interval_spec),
    `Unit Type`=vapply(X = interval_spec, FUN = "[[", "unit_type", FUN.VALUE = ""),
    `Parameter Description`=vapply(X = interval_spec, FUN = "[[", "desc", FUN.VALUE = ""),
    `Function for Calculation`=vapply(X = interval_spec, FUN = get_function_for_calc, FUN.VALUE = ""),
    check.names=FALSE,
    stringsAsFactors=FALSE
  ),
  row.names=FALSE
)
```
