Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
any::ggplot2
any::conflicted
any::visNetwork
any::tidyr
github::humanpred/pknca

- name: Set up Quarto
Expand Down
7 changes: 6 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This file provides guidance to Claude Code when working in this repository.
## What This Is

A Quarto book documenting the [PKNCA](https://github.com/humanpred/pknca) R package (≥ 0.12.2).
Every function, option, and parameter is covered with runnable examples verified against live PKNCA output.
Every parameter, option, and user-facing function is covered with runnable examples verified against live PKNCA output; the `pk.calc.*` calculation functions are indexed in the Developer Reference.

**Book repo:** https://github.com/humanpred/pknca-book
**Rendered book:** https://humanpred.github.io/pknca-book/
Expand Down Expand Up @@ -43,6 +43,11 @@ _book/ # rendered output (gitignored)
- **No `suppressWarnings()`** hiding real issues — fix root causes
- **Each page is self-contained:** load packages at the top of every `.qmd`
- **New features** are tagged with `(≥ X.Y.Z)` to indicate the minimum version
- **US English spelling** (PKNCA declares `Language: en-US`); ggplot2's `colour =` argument spelling in code is acceptable
- **Headings:** sentence case, no manual numbering; page titles are Title Case
- **Chapter footers:** every chapter ends with a `:::callout-note` labeled `**pkgdown reference:**` linking each function the chapter demonstrates (plain link text, no backticks); verify new URLs resolve
- **Cross-linking:** link the corresponding package vignette (`https://humanpred.github.io/pknca/articles/`) where a chapter covers the same topic, and sibling chapters instead of re-explaining their material
- **Naming in examples:** `d_*` for raw data frames, `o_*` for PKNCA objects; "timepoint" (one word); the integration option string is written exactly `"lin up/log down"`

## Adding or Updating Content

Expand Down
18 changes: 9 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ For each item, classify it:
| New NCA parameter | new `add.interval.col()` entry |
| New option | new key in `PKNCA.options()` |
| New function | e.g. `normalize()` in 0.12.2 |
| Changed behaviour | existing function works differently |
| Changed behavior | existing function works differently |
| Bug fix | may invalidate an existing example |

Cross-check the parameter count and options list:
Expand Down Expand Up @@ -102,7 +102,7 @@ as.data.frame(o_nca) |>

**Known gotchas:**

- **Tobit half-life** — `pk.nca()` does not wire `lloq` through to `pk.calc.half.life()`. Use `pk.calc.half.life()` directly and add a note in the docs.
- **Tobit half-life** — set `lloq` on `PKNCAconc()`; in PKNCA ≥ 0.12.2 it is passed through to `pk.calc.half.life()` automatically, so the full `pk.nca()` pipeline works with `options = list(hl_method = "tobit")`. (Older versions required calling `pk.calc.half.life()` directly.)
- **Sparse AUMC** — only set `sparse_aumclast = TRUE`. Explicitly requesting `sparse_auc_se = TRUE` alongside it triggers an internal type error.
- **`normalize()`** — replaces the original parameters (does not append). To keep both, `bind_rows()` before and after.
- **New option defaults** — always verify with `PKNCA.options("option_name")` before documenting the default.
Expand All @@ -113,7 +113,7 @@ as.data.frame(o_nca) |>

| Changed item | Page(s) to update |
|---|---|
| New NCA parameter | `user/intervals.qmd` (catalogue + count) + thematic page |
| New NCA parameter | `user/intervals.qmd` (catalog + count) + thematic page |
| New option | `user/workflow.qmd` (options table) |
| New post-processing function | `user/postprocessing.qmd` |
| New sparse parameter | `user/sparse.qmd` |
Expand Down Expand Up @@ -153,7 +153,7 @@ open _book/index.html
Confirm:
- Output matches expected values
- No unexpected `NA` results
- Warnings are expected (e.g., half-life not calculable for some subjects)
- Warnings are expected (e.g. half-life not calculable for some subjects)

---

Expand Down Expand Up @@ -192,7 +192,7 @@ Template:
[ ] Update index.qmd "What's new" section
[ ] Update README.md version highlights
[ ] Update user/workflow.qmd options table (if options changed)
[ ] Update user/intervals.qmd parameter count and catalogue
[ ] Update user/intervals.qmd parameter count and catalog
[ ] Update thematic pages (auc-methods, halflife, sparse, urine, etc.)
[ ] Verify all packages are listed in .github/workflows/book.yml — audit at once with:
grep -rh "^library(" user/ dev/ index.qmd | sort -u
Expand All @@ -212,16 +212,16 @@ _quarto.yml # book config (title, author, chapters)
index.qmd # introduction + What's new
user/ # 19 User Guide chapters
workflow.qmd # PKNCAconc → PKNCAdose → PKNCAdata → pk.nca; all options
intervals.qmd # interval data frame; full parameter catalogue
intervals.qmd # interval data frame; full parameter catalog
auc-methods.qmd # AUC types; integration methods; AUMC; aucabove; time_above
halflife.qmd # λz regression; Tobit; quality filters; lambda.z.corrxy
extravascular.qmd # oral/SC; BLQ; tmin; lag time
intravascular.qmd # IV bolus; C0; IV infusion; ceoi; MRT
multiple-dose.qmd # steady-state parameters; tau detection
urine-excretion.qmd# ae; fe; clr; volpk; ermax; ertmax; ertlst; clr.*.dn
sparse.qmd # sparse_auclast; sparse AUMC; derived sparse params
superposition.qmd # predicting multiple-dose from single-dose
tss.qmd # time to steady state
urine-excretion.qmd# ae; fe; clr; volpk; ermax; ertmax; ertlst; clr.*.dn
sparse.qmd # sparse_auclast; sparse AUMC; derived sparse params
postprocessing.qmd # exclude(); normalize(); .dn parameters; PKNCA.set.summary
units.qmd # pknca_units_table(); preferred units; conversions
imputation.qmd # built-in imputation methods; custom methods
Expand All @@ -231,6 +231,6 @@ user/ # 19 User Guide chapters
regulatory.qmd # PP domain; PPEXCLFL; summary() for CSR tables
validation.qmd # testthat suite; version pinning; spot-checks
dev/ # Developer Reference
function-deps.qmd # parameter dependency graph
architecture.qmd # class hierarchy; interval column system; formalsmap
function-deps.qmd # parameter dependency graph
```
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Render and deploy book](https://github.com/humanpred/pknca-book/actions/workflows/book.yml/badge.svg)](https://github.com/humanpred/pknca-book/actions/workflows/book.yml)

A comprehensive **Quarto book** documenting the [PKNCA](https://github.com/humanpred/pknca) R package (≥ 0.12.2) with runnable examples throughout. Every function, option, and parameter is verified against live PKNCA output.
A comprehensive **Quarto book** documenting the [PKNCA](https://github.com/humanpred/pknca) R package (≥ 0.12.2) with runnable examples throughout. Every parameter, option, and user-facing function is covered and verified against live PKNCA output; the underlying `pk.calc.*` calculation functions are indexed in the Developer Reference.

**Live book:** https://humanpred.github.io/pknca-book/

Expand All @@ -27,31 +27,31 @@ open _book/index.html
| Chapter | Topics |
|---|---|
| [Workflow](user/workflow.qmd) | PKNCAconc → PKNCAdose → PKNCAdata → pk.nca; all 21 options with defaults |
| [Interval selection & parameter catalogue](user/intervals.qmd) | Interval data frame structure; group matching; all 143 calculable parameters |
| [Interval selection & parameter catalog](user/intervals.qmd) | Interval data frame structure; group matching; the full parameter catalog, rendered live from the registry |
| [AUC types & integration methods](user/auc-methods.qmd) | auclast/aucall/aucinf; partial AUC; lin up/log down vs linear vs lin-log; aucabove; time_above; AUMC; cav.int |
| [Half-life & terminal phase](user/halflife.qmd) | Curve stripping; adj.r.squared.factor; quality filters; Tobit regression; lambda.z.corrxy |
| [Extravascular (oral/SC)](user/extravascular.qmd) | BLQ handling; imputation strategies; tmin; lag time; multiple peaks |
| [Intravascular (IV)](user/intravascular.qmd) | IV bolus NCA; C0 back-extrapolation; IV infusion with ceoi; MRT correction |
| [Multiple-dose / steady-state](user/multiple-dose.qmd) | deg.fluc; swing; PTR; Cav; tau detection |
| [Urine excretion](user/urine-excretion.qmd) | ae; fe; clr.*; volpk; ermax/ertmax/ertlst; clr.*.dn |
| [Sparse PK sampling](user/sparse.qmd) | sparse_auclast + SE + df; sparse AUMC; mrt/cl/kel.sparse.last |
| [Superposition](user/superposition.qmd) | Predicting multiple-dose profiles from single-dose data |
| [Time to steady state](user/tss.qmd) | pk.tss.monoexponential; pk.tss.stepwise.linear; pk.tss |
| [Urine excretion](user/urine-excretion.qmd) | ae; fe; clr.*; volpk; ermax/ertmax/ertlst; clr.*.dn |
| [Sparse PK sampling](user/sparse.qmd) | sparse_auclast + SE + df; sparse AUMC; mrt/cl/kel.sparse.last |
| [Post-processing](user/postprocessing.qmd) | exclude(); normalize(); 17 .dn parameters; PKNCA.set.summary; get_halflife_points |
| [Units](user/units.qmd) | pknca_units_table() S3 generic; PKNCAdata method; preferred units; conversions |
| [Imputation](user/imputation.qmd) | start_conc0; start_predose; start_cmin; custom imputation methods |
| [Dose-aware interpolation](user/dose-aware-interpolation.qmd) | interp.extrap.conc.dose; .dose AUCint variants |
| [Custom parameters](user/custom-parameters.qmd) | add.interval.col; registering new NCA parameters |
| [Utility functions](user/utilities.qmd) | geomean; geocv; clean.conc.*; assert_conc_time |
| [Regulatory & CDISC](user/regulatory.qmd) | PP domain column mapping; exclude → PPEXCLFL; summary() for CSR tables |
| [Regulatory & CDISC](user/regulatory.qmd) | PP domain column mapping; CDISC output format; exclusion tracking; summary() for CSR tables |
| [Validation & testing](user/validation.qmd) | testthat suite; version pinning; manual AUClast spot-check |

### Developer Reference (2 chapters)

| Chapter | Topics |
|---|---|
| [Function dependencies](dev/function-deps.qmd) | Parameter dependency graph |
| [Architecture](dev/architecture.qmd) | Internal structure of PKNCA |
| [Function dependencies](dev/function-deps.qmd) | Parameter dependency graph |

## What's new in PKNCA 0.12.2

Expand Down
8 changes: 5 additions & 3 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ book:
right:
- text: "Function Reference"
href: https://humanpred.github.io/pknca/reference/index.html
- text: "Vignettes"
href: https://humanpred.github.io/pknca/articles/index.html
- text: "pkgdown site"
href: https://humanpred.github.io/pknca/
chapters:
Expand All @@ -30,10 +32,10 @@ book:
- user/extravascular.qmd
- user/intravascular.qmd
- user/multiple-dose.qmd
- user/urine-excretion.qmd
- user/sparse.qmd
- user/superposition.qmd
- user/tss.qmd
- user/urine-excretion.qmd
- user/sparse.qmd
- user/postprocessing.qmd
- user/units.qmd
- user/imputation.qmd
Expand All @@ -44,8 +46,8 @@ book:
- user/validation.qmd
- part: "Developer Reference"
chapters:
- dev/function-deps.qmd
- dev/architecture.qmd
- dev/function-deps.qmd

format:
html:
Expand Down
84 changes: 37 additions & 47 deletions dev/architecture.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ conflicted::conflicts_prefer(dplyr::filter, dplyr::select, dplyr::rename, .quiet

## Class hierarchy

PKNCA uses four S3 classes that form a strict pipeline. Each wraps the previous one:
PKNCA uses four S3 classes: `PKNCAconc` and `PKNCAdose` feed `PKNCAdata`, and `pk.nca()` wraps the result into `PKNCAresults`:

```{mermaid}
classDiagram
Expand Down Expand Up @@ -61,12 +61,17 @@ Each entry contains:

| Field | Purpose |
|---|---|
| `FUN` | The R function that computes this parameter |
| `FUN` | Name of the function that computes this parameter (character string, or `NA` for parameters populated by another function's multi-column result) |
| `values` | Allowed values in the interval column (typically `c(FALSE, TRUE)`) |
| `unit_type` | Unit category used for unit assignment and conversion |
| `pretty_name` | Human-readable name used in summary tables |
| `depends` | Character vector of parameters that must be computed first |
| `desc` | Human-readable description |
| `formalsmap` | Maps function arguments to interval/group/options data |
| `value.ok` | Validation function for the computed result |
| `sparse` | Whether this parameter works for sparse PK |
| `formalsmap` | Maps function arguments to interval/group/options data |
| `datatype` | Calculation type; only `"interval"` is currently supported |
| `pptestcd_cdisc` | CDISC PPTESTCD code (≥ 0.12.2) |
| `pptest_cdisc` | CDISC PPTEST label (≥ 0.12.2) |

## formalsmap

Expand All @@ -77,57 +82,38 @@ Each entry contains:
| Interval column | `conc`, `time` | Concentrations/times for the current interval |
| Other parameters | `auclast = "auclast"` | A previously computed NCA result |
| Interval bounds | `start`, `end` | The interval start/end times |
| Options | `auc.method` | Value from `PKNCA.options()` |
| Options | `options` | The full analysis options list |

Individual option names (e.g. `auc.method`) cannot be mapped through `formalsmap`; only the whole `options` list can, and parameter functions read specific settings from it via `PKNCA.choose.option()`.

## Writing a custom parameter

To add a new NCA parameter:
Registering a new parameter is a single `add.interval.col()` call that names the computing function; PKNCA then derives execution order from `depends`, assigns units from `unit_type`, and collects results automatically. A minimal toy registration:

```{r}
# Example: compute the "exposure ratio" — AUCinf / AUClast
pk.calc.exposure.ratio <- function(aucinf.obs, auclast) {
aucinf.obs / auclast
# Toy illustration: ratio of two built-in parameters
pk.calc.demo.ratio <- function(cmax, cmin) {
cmax / cmin
}

add.interval.col(
name = "exposure.ratio",
FUN = "pk.calc.exposure.ratio", # must be a character string, not the function object
unit_type = "unitless",
pretty_name = "Exposure Ratio (AUCinf/AUClast)",
desc = "Ratio of AUCinf to AUClast; >1 indicates extrapolation",
depends = c("aucinf.obs", "auclast"),
formalsmap = list(aucinf.obs = "aucinf.obs", auclast = "auclast")
)

# Now it can be requested in intervals
d_conc <- as.data.frame(Theoph) |> rename(time = Time, subject = Subject)
d_dose <- Theoph |> as.data.frame() |>
dplyr::group_by(Subject) |>
dplyr::summarise(dose = Dose[1] * Wt[1], .groups = "drop") |>
dplyr::rename(subject = Subject) |>
dplyr::mutate(time = 0)

o_conc <- PKNCAconc(d_conc, conc ~ time | subject)
o_dose <- PKNCAdose(d_dose, dose ~ time | subject, route = "extravascular")

custom_interval <- data.frame(
start = 0, end = Inf,
auclast = TRUE, aucinf.obs = TRUE,
exposure.ratio = TRUE
name = "demo.ratio",
FUN = "pk.calc.demo.ratio", # must be a character string, not the function object
unit_type = "unitless",
pretty_name = "Demo ratio (Cmax/Cmin)",
desc = "Toy example: Cmax divided by Cmin",
depends = c("cmax", "cmin")
)

o_data <- PKNCAdata(o_conc, o_dose, intervals = custom_interval)
o_nca <- pk.nca(o_data)

as.data.frame(o_nca) |>
dplyr::filter(PPTESTCD %in% c("auclast", "aucinf.obs", "exposure.ratio")) |>
dplyr::select(subject, PPTESTCD, PPORRES) |>
dplyr::arrange(subject, PPTESTCD)
# The registry now contains it alongside the built-in parameters
str(get.interval.cols()[["demo.ratio"]])
```

For the full walkthrough — parameters computed from raw concentration-time data, multi-output functions, and custom summary statistics — see the [Writing Custom Parameters chapter](../user/custom-parameters.qmd) and the package vignette [Writing PKNCA Parameter Functions](https://humanpred.github.io/pknca/articles/v80-writing-parameter-functions.html).

## Options system

`PKNCA.options()` is a named list stored as a package-level environment. It is accessed in three ways:
PKNCA's options are stored in a package-level environment; `PKNCA.options()` returns them as a named list. It is accessed in several ways:

```{r}
# 1. Read all options
Expand All @@ -150,32 +136,36 @@ PKNCA.options("min.hl.points")

## Sparse PK

Sparse PK (one or two samples per subject, common in preclinical or paediatric studies) is handled via a parallel code path:
Sparse PK (one or two samples per subject, common in preclinical or pediatric studies) is handled via a parallel code path:

- Set `sparse = TRUE` in `PKNCAconc()`
- Concentrations are pooled across subjects per timepoint
- Mean concentration-time profile is computed, then NCA is run on that profile
- Supported parameters are flagged with `sparse = TRUE` in their `add.interval.col()` registration

## Key internal functions
For the full workflow, see the [Sparse NCA chapter](../user/sparse.qmd) and the vignette [Sparse NCA Calculations](https://humanpred.github.io/pknca/articles/v04-sparse.html).

## Key functions

All of these are exported except where marked internal:

| Function | Purpose |
|---|---|
| `get.interval.cols()` | Return full parameter registry |
| `PKNCA:::sort.interval.cols()` | Topological sort → execution order |
| `PKNCA:::pk.nca.interval()` | Compute all parameters for one interval/group |
| `PKNCA:::sort_interval_cols()` | Topological sort → execution order (internal, unexported) |
| `pk.nca.interval()` | Compute all parameters for one interval/group |
| `pknca_units_table()` | Build unit assignment/conversion table |
| `PKNCA_impute_method_*()` | Built-in imputation methods |
| `pk.calc.auc.last()` | Core AUC calculation (trapezoidal) |
| `pk.calc.half.life()` | λz regression with best-fit selection |

---

> This overview covers the surface. The formalsmap system, business rules (`002-pk.business.rules.R`), and the validation framework (`v60-PKNCA-validation.Rmd`) are richer topics worth exploring further.
> This overview covers the surface. The business rules (`002-pk.business.rules.R`) and the validation framework ([`v60-PKNCA-validation.Rmd`](https://humanpred.github.io/pknca/articles/v60-PKNCA-validation.html)) in the PKNCA sources are richer topics worth exploring further.


---

::: {.callout-note icon=false appearance="minimal"}
**pkgdown reference:** [PKNCAconc()](https://humanpred.github.io/pknca/reference/PKNCAconc.html) · [PKNCAdose()](https://humanpred.github.io/pknca/reference/PKNCAdose.html) · [PKNCAdata()](https://humanpred.github.io/pknca/reference/PKNCAdata.html) · [pk.nca()](https://humanpred.github.io/pknca/reference/pk.nca.html) · [add.interval.col()](https://humanpred.github.io/pknca/reference/add.interval.col.html) · [get.interval.cols()](https://humanpred.github.io/pknca/reference/get.interval.cols.html) · [PKNCA.options()](https://humanpred.github.io/pknca/reference/PKNCA.options.html) · [pknca_units_table()](https://humanpred.github.io/pknca/reference/pknca_units_table.html)
**pkgdown reference:** [PKNCAconc()](https://humanpred.github.io/pknca/reference/PKNCAconc.html) · [PKNCAdose()](https://humanpred.github.io/pknca/reference/PKNCAdose.html) · [PKNCAdata()](https://humanpred.github.io/pknca/reference/PKNCAdata.html) · [pk.nca()](https://humanpred.github.io/pknca/reference/pk.nca.html) · [add.interval.col()](https://humanpred.github.io/pknca/reference/add.interval.col.html) · [get.interval.cols()](https://humanpred.github.io/pknca/reference/get.interval.cols.html) · [PKNCA.options()](https://humanpred.github.io/pknca/reference/PKNCA.options.html) · [pknca_units_table()](https://humanpred.github.io/pknca/reference/pknca_units_table.html) · [pk.nca.interval()](https://humanpred.github.io/pknca/reference/pk.nca.interval.html) · [PKNCA.choose.option()](https://humanpred.github.io/pknca/reference/PKNCA.choose.option.html) · [pk.calc.half.life()](https://humanpred.github.io/pknca/reference/pk.calc.half.life.html)
:::
Loading
Loading