---
title: "Fosamprenavir (Fisher 2008)"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Fosamprenavir (Fisher 2008)}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5)
library(nlmixr2lib)
library(PKNCA)
library(rxode2)
library(dplyr)
library(tidyr)
library(ggplot2)
```

# Model and source

* Citation: `r rxode2::rxode(readModelDb("Fisher_2008_fosamprenavir"))$meta$reference`
* Description: `r rxode2::rxode(readModelDb("Fisher_2008_fosamprenavir"))$meta$description`
* Poster: <https://metrumrg.com/wp-content/uploads/2018/08/acop_2008_fosamprenavir.pdf>

# Population

The model was fit to 1322 plasma amprenavir (APV) concentrations from 137
HIV-1-infected pediatric patients enrolled in three multinational
GlaxoSmithKline studies (APV20002, APV20003, APV29005) and aged 4 weeks to
18 years (baseline age range 0.72-18 years, median 10 years; baseline weight
range 5.9-102.8 kg, median 32.9 kg per Table 1). The race distribution was
White 57.7%, Black 27.0%, Asian 1.5%, Hispanic 5.1%, American Indian 5.8%,
and Other 2.9% (Table 2). 119 of 137 patients (86.9%) received FPV with
concomitant low-dose ritonavir (RTV); the remaining 18 patients (13.1%, all
aged 2-6 years) received FPV alone. The full `population` metadata is
available as
`rxode2::rxode(readModelDb("Fisher_2008_fosamprenavir"))$meta$population`.

# Source trace

The per-parameter origin is recorded as an in-file comment next to each
`ini()` entry in `inst/modeldb/specificDrugs/Fisher_2008_fosamprenavir.R`.
The table below collects the structural model and parameter provenance in
one place.

| Equation / parameter | Value | Source location |
|----------------------|-------|-----------------|
| Two-compartment first-order absorption (`depot` -> `central` <-> `peripheral1`) | n/a | Model section (page 1, "Two-compartment model with first-order absorption and elimination.") |
| Allometric exponents fixed at 0.75 (CL, Q) and 1 (V2/F, V3) | n/a | Methods, Model and Modeling Assumptions |
| Piecewise age maturation on CL/F: f_age = 1 + AMAX*(1 - 0.5*AGE/AG50) for AGE <= 2*AG50; f_age = 1 otherwise | n/a | Model section equations (page 1) |
| Reference subject: WT = 70 kg, AGE > 4 yr, White, Male, AAG = 0.77 g/L, suspension under fed, +RTV | n/a | Figure 3 caption |
| `lka` = log(1.13)                | 1.13 1/h        | Table 3 theta_5 |
| `lcl` = log(84.4)                | 84.4 L/h        | Table 3 theta_6 (CL/F without RTV) |
| `lvc` = log(288)                 | 288 L           | Table 3 theta_2 |
| `lq`  = log(63.5)                | 63.5 L/h        | Table 3 theta_3 |
| `lvp` = log(1630)                | 1630 L          | Table 3 theta_4 |
| `e_conmed_rtv_cl` = -0.5961      | derived         | Table 3 theta_1 / theta_6: (34.1 - 84.4)/84.4 |
| `amax_cl` = 0.789                | 0.789 (unitless)| Table 3 theta_10 |
| `ag50_cl` = 2.05                 | 2.05 yr         | Table 3 theta_11 |
| `e_sexf_cl` = 0.846              | 0.846           | Table 3 theta_12 |
| `e_race_black_cl` = 0.940        | 0.940           | Table 3 theta_13 |
| `e_race_nbnw_cl`  = 1.06         | 1.06            | Table 3 theta_14 |
| `e_aag_cl` = -0.626              | -0.626          | Table 3 theta_15 |
| `e_aag_vc` = -0.369              | -0.369          | Table 3 theta_16 |
| `lftab`     = log(1.09)          | 1.09            | Table 3 theta_7 |
| `lffoodsus` = log(0.87)          | 0.87            | Table 3 theta_8 |
| `etalcl + etalvc` block          | (0.0901, 0.0945, 0.438) | Table 3 Omega_11, Omega_12, Omega_22 |
| `etalq`                          | 0.536           | Table 3 Omega_33 |
| `propSd` = sqrt(0.0828) = 0.2877 | sigma_1^2 = 0.0828 | Table 3 |
| `addSd`  = sqrt(0.0760) = 0.2757 | sigma_2^2 = 0.0760 | Table 3 |

# Virtual cohort

Original observed data are not publicly available. The figures below use a
virtual pediatric population whose covariate distributions approximate the
Methods-described simulation cohort: White race (RACE_BLACK = 0,
RACE_NONBLACK_NONWHITE = 0), AAG fixed at 0.77 g/L (population median per
Figure 3 caption), male (SEXF = 0; arbitrary fixed choice for the
deterministic comparison), suspension under fed conditions (FORM_TABLET = 0,
FED = 1; suspension is the dominant pediatric formulation in this cohort).
Three age strata are simulated -- young (1 year, ~10 kg), preschool (4
years, ~16 kg), and school-age (12 years, ~40 kg) -- matched against the
poster's three pre-specified pediatric dosing regimens (FPV/RTV BID, FPV/RTV
QD, FPV BID). 50 subjects per arm gives a well-resolved median trajectory
while staying well within the 200-per-arm cap.

```{r cohort}
set.seed(20260625L)

make_cohort <- function(n,
                        age, weight, regimen, rtv_status,
                        dose_mg, dose_interval,
                        id_offset = 0L) {
  # Subjects
  subj <- tibble::tibble(
    id          = id_offset + seq_len(n),
    WT          = weight,
    AGE         = age,
    AAG         = 0.77,
    SEXF        = 0L,
    RACE_BLACK  = 0L,
    RACE_NONBLACK_NONWHITE = 0L,
    CONMED_RTV  = rtv_status,
    FORM_TABLET = 0L,
    FED         = 1L,
    regimen     = regimen
  )

  # Steady-state dosing window: 5 days, capture trajectory across the last
  # dosing interval for AUC0-tau computation.
  total_h <- 24 * 5
  dose_times <- seq(0, total_h - dose_interval, by = dose_interval)
  doses <- tidyr::expand_grid(subj, time = dose_times) |>
    dplyr::mutate(amt = dose_mg, evid = 1L, cmt = "depot")

  # Observation grid: every 30 min during the last dosing interval,
  # plus an anchor t = 0 record so PKNCA has a pre-dose Cc = 0.
  ss_start <- total_h - dose_interval
  obs_times <- c(0, seq(ss_start, total_h, by = 0.5))
  obs <- tidyr::expand_grid(subj, time = obs_times) |>
    dplyr::mutate(amt = NA_real_, evid = 0L, cmt = "central")

  dplyr::bind_rows(doses, obs) |>
    dplyr::arrange(id, time, dplyr::desc(evid)) |>
    dplyr::select(id, time, amt, evid, cmt,
                  WT, AGE, AAG, SEXF, RACE_BLACK, RACE_NONBLACK_NONWHITE,
                  CONMED_RTV, FORM_TABLET, FED, regimen)
}

n_per_arm <- 50L

events <- dplyr::bind_rows(
  # FPV/RTV BID (interval 12 h): ages <=2 36 mg/kg; >2-<=6 23 mg/kg; >6 18 mg/kg; cap 700 mg
  make_cohort(n_per_arm, age =  1, weight = 10, regimen = "FPV/RTV BID, <=2 yr",
              rtv_status = 1L, dose_mg = min(36 * 10, 700), dose_interval = 12,
              id_offset = 0L),
  make_cohort(n_per_arm, age =  4, weight = 16, regimen = "FPV/RTV BID, 2-6 yr",
              rtv_status = 1L, dose_mg = min(23 * 16, 700), dose_interval = 12,
              id_offset = 50L),
  make_cohort(n_per_arm, age = 12, weight = 40, regimen = "FPV/RTV BID, >6 yr",
              rtv_status = 1L, dose_mg = min(18 * 40, 700), dose_interval = 12,
              id_offset = 100L),
  # FPV/RTV QD (interval 24 h): cap 1400 mg
  make_cohort(n_per_arm, age =  1, weight = 10, regimen = "FPV/RTV QD, <=2 yr",
              rtv_status = 1L, dose_mg = min(72 * 10, 1400), dose_interval = 24,
              id_offset = 150L),
  make_cohort(n_per_arm, age =  4, weight = 16, regimen = "FPV/RTV QD, 2-6 yr",
              rtv_status = 1L, dose_mg = min(46 * 16, 1400), dose_interval = 24,
              id_offset = 200L),
  make_cohort(n_per_arm, age = 12, weight = 40, regimen = "FPV/RTV QD, >6 yr",
              rtv_status = 1L, dose_mg = min(36 * 40, 1400), dose_interval = 24,
              id_offset = 250L),
  # FPV BID (no RTV, interval 12 h): cap 1400 mg
  make_cohort(n_per_arm, age =  1, weight = 10, regimen = "FPV BID, <=2 yr",
              rtv_status = 0L, dose_mg = min(38 * 10, 1400), dose_interval = 12,
              id_offset = 300L),
  make_cohort(n_per_arm, age =  4, weight = 16, regimen = "FPV BID, 2-6 yr",
              rtv_status = 0L, dose_mg = min(25 * 16, 1400), dose_interval = 12,
              id_offset = 350L),
  make_cohort(n_per_arm, age = 12, weight = 40, regimen = "FPV BID, >6 yr",
              rtv_status = 0L, dose_mg = min(17 * 40, 1400), dose_interval = 12,
              id_offset = 400L)
)

# Cheap regression guard against ID collisions across cohorts.
stopifnot(!anyDuplicated(unique(events[, c("id", "time", "evid")])))
```

# Simulation

```{r simulate}
mod <- readModelDb("Fisher_2008_fosamprenavir")

sim <- rxode2::rxSolve(
  mod, events = events,
  keep = c("regimen", "WT", "AGE", "CONMED_RTV")
) |>
  as.data.frame()
```

# Replicate Figure 4 -- steady-state APV exposure by regimen and age

Figure 4 of the poster summarises the per-age-group, per-regimen
steady-state AUC(0-tau) distribution after the recommended pediatric
dosing. The plot below reproduces the qualitative shape: median (line) and
5th-95th percentile bands of simulated APV concentrations over the last
dosing interval, faceted by regimen and age group, with the geometric-mean
adult AUC target overlaid as a horizontal reference line in the NCA
comparison further below.

```{r figure-4}
sim |>
  dplyr::filter(time >= (5 * 24 - 24)) |>
  dplyr::group_by(regimen, time) |>
  dplyr::summarise(
    Q05 = stats::quantile(Cc, 0.05, na.rm = TRUE),
    Q50 = stats::quantile(Cc, 0.50, na.rm = TRUE),
    Q95 = stats::quantile(Cc, 0.95, na.rm = TRUE),
    .groups = "drop"
  ) |>
  ggplot(aes(time - (5 * 24 - 24), Q50)) +
  geom_ribbon(aes(ymin = Q05, ymax = Q95), alpha = 0.25) +
  geom_line() +
  facet_wrap(~regimen, scales = "free_y", ncol = 3) +
  labs(
    x = "Time within last dosing interval (h)",
    y = "Amprenavir (ug/mL)",
    title = "Replicates Figure 4 of Fisher 2008",
    caption = "Median and 5-95% interval of simulated steady-state APV concentration over the last dosing interval, by recommended pediatric regimen and age group."
  )
```

# PKNCA validation against adult AUC(0-tau) targets

The poster identifies three geometric-mean adult AUC(0-tau) targets that
the pediatric regimens were designed to match (Simulations section): FPV
BID 16.5 ug*h/mL (12-h interval), FPV/RTV BID 37.0 ug*h/mL (12-h interval),
and FPV/RTV QD 67.1 ug*h/mL (24-h interval; the poster's "h*mg/mL" units
are a transcription typo, the consistent reading throughout the source is
h*ug/mL). The Conclusions paragraph reports that the simulation-driven
dosing minimised exposure variability and matched the historical adult
targets per regimen. The PKNCA cross-check below confirms the same
behaviour for the packaged model.

```{r pknca}
tau_by_regimen <- c(
  "FPV/RTV BID, <=2 yr" = 12, "FPV/RTV BID, 2-6 yr" = 12, "FPV/RTV BID, >6 yr" = 12,
  "FPV/RTV QD, <=2 yr"  = 24, "FPV/RTV QD, 2-6 yr"  = 24, "FPV/RTV QD, >6 yr"  = 24,
  "FPV BID, <=2 yr"     = 12, "FPV BID, 2-6 yr"     = 12, "FPV BID, >6 yr"     = 12
)

ss_start <- 5 * 24 - 24   # last dosing interval starts here (h)

sim_nca <- sim |>
  dplyr::filter(!is.na(Cc), time >= ss_start) |>
  dplyr::mutate(time = time - ss_start) |>
  dplyr::select(id, time, Cc, regimen)

sim_nca <- dplyr::bind_rows(
  sim_nca,
  sim_nca |>
    dplyr::distinct(id, regimen) |>
    dplyr::mutate(time = 0, Cc = 0)
) |>
  dplyr::distinct(id, regimen, time, .keep_all = TRUE) |>
  dplyr::arrange(id, regimen, time)

dose_df <- events |>
  dplyr::filter(evid == 1, time == max(time[evid == 1]), .by = id) |>
  dplyr::mutate(time = 0) |>
  dplyr::select(id, time, amt, regimen)

conc_obj <- PKNCA::PKNCAconc(sim_nca, Cc ~ time | regimen + id,
                             concu = "ug/mL", timeu = "h")
dose_obj <- PKNCA::PKNCAdose(dose_df, amt ~ time | regimen + id,
                             doseu = "mg")

intervals_per_regimen <- tibble::tibble(
  regimen = names(tau_by_regimen),
  start   = 0,
  end     = unname(tau_by_regimen),
  cmax    = TRUE,
  tmax    = TRUE,
  cmin    = TRUE,
  auclast = TRUE,
  cav     = TRUE
) |>
  as.data.frame()

nca_data <- PKNCA::PKNCAdata(conc_obj, dose_obj, intervals = intervals_per_regimen)
nca_res  <- PKNCA::pk.nca(nca_data)
```

## Comparison against adult AUC(0-tau) targets

The poster does not tabulate per-age-group simulated NCA values, only the
adult target AUC and a graphical Figure 4. The table below uses
`nlmixr2lib::ncaComparisonTable()` to align the simulated steady-state
AUC0-tau (auclast over the last interval) against the regimen-level adult
target; differences within ~20% of target are considered successful per
the poster's "match historical adult exposure" Conclusions language.

```{r nca_compare}
published <- tibble::tribble(
  ~regimen,                ~auclast,
  "FPV/RTV BID, <=2 yr",   37.0,
  "FPV/RTV BID, 2-6 yr",   37.0,
  "FPV/RTV BID, >6 yr",    37.0,
  "FPV/RTV QD, <=2 yr",    67.1,
  "FPV/RTV QD, 2-6 yr",    67.1,
  "FPV/RTV QD, >6 yr",     67.1,
  "FPV BID, <=2 yr",       16.5,
  "FPV BID, 2-6 yr",       16.5,
  "FPV BID, >6 yr",        16.5
)

cmp <- nlmixr2lib::ncaComparisonTable(
  simulated     = nca_res,
  reference     = published,
  by            = "regimen",
  units         = c(auclast = "ug*h/mL"),
  tolerance_pct = 20
)

knitr::kable(
  cmp,
  caption = "Simulated steady-state AUC0-tau vs the adult AUC(0-tau) target per regimen. * differs from reference by >20%.",
  align   = c("l", "l", "r", "r", "r")
)
```

# Assumptions and deviations

- **Race and AAG fixed in simulations.** Per the poster's Simulations
  section, "Race and AAG were fixed to 1 (Caucasian) and 0.77 g/L
  (population median) values, respectively." The virtual cohort follows
  this convention (`RACE_BLACK = 0`, `RACE_NONBLACK_NONWHITE = 0`,
  `AAG = 0.77`), which lands the simulation at the typical-value
  population-median state. The model can be re-simulated with cohort-
  realistic race and AAG distributions when those are needed.
- **Sex fixed at male in the simulation cohort.** The poster's Figure 4
  reference patient is male and the dosing recommendation does not
  stratify by sex; the simulation cohort follows the reference. Female
  CL/F is 15.4% lower (Table 3 theta_12 = 0.846); rerun with `SEXF = 1`
  to recover the female-only typical-value behaviour.
- **Representative weight per age group.** Each age stratum (1, 4, 12
  years) uses a single representative WT (10, 16, 40 kg) approximating
  the WHO weight-for-age median. The packaged model accepts any
  cohort-realistic WT distribution; the representative-weight choice
  here is for an interpretable Figure-4 reproduction.
- **`e_conmed_rtv_cl = -0.5961` is derived.** The source poster reports
  two CL/F values in Table 3 (theta_1 = 34.1 L/h with RTV; theta_6 =
  84.4 L/h without RTV). The Colombo 2006 atazanavir form
  `cl = exp(lcl) * (1 + e_conmed_rtv_cl * CONMED_RTV)` encodes the
  reduction as a single fractional-change coefficient, computed as
  (34.1 - 84.4) / 84.4 = -0.5961, consistent with the Conclusions
  paragraph "co-administration of RTV was estimated to decrease plasma
  APV CL/F by approximately 60%."
- **Concentration units for the QD target.** The poster's Simulations
  bullet for FPV/RTV QD prints the target unit as "h*mg/mL", which is a
  transcription typo within the source (every other bullet, and the
  paper's Discussion, consistently reads h*ug/mL = ug*h/mL). The unit
  used here is ug*h/mL.
- **Food-intake imputation.** The source dataset has a third Food
  Intake category (Missing, -1, 31.4% of records); the model file's
  `covariateData$FED$notes` documents that this implementation imputes
  Missing as fed (FED = 1, the reference). Downstream users with a
  different imputation should set FED accordingly.
- **Inter-occasion variability on CL/F not encoded.** Fisher 2008 Table
  3 reports omega^2 = 0.114 (~34% CV) for inter-occasion variability
  on CL/F. The packaged model omits this structural element because
  the source poster does not define an operational occasion column
  for downstream simulation, and the nlmixr2lib convention (Brooks
  2021 / Andrews 2017 precedent) is to omit IOV when no occasion
  mapping is defined. Downstream users who need IOV in simulation can
  add an OCC indicator and a per-occasion eta in rxode2.
- **Three FPV BID (unboosted) age strata are extrapolations beyond the
  observed dataset.** Only children aged 2-6 years received FPV BID
  without RTV in the source clinical study (n=18). Simulations of the
  unboosted regimen in younger or older age strata rely on the
  poster's stated assumption (Simulations NOTE) that the covariate
  relationships described in the full model are applicable across the
  entire age range for both boosted and unboosted FPV doses.
