20.11 Data-driven $PK logic and variable persistence
$PK can read any dataset column in nested IF/ELSEIF/ELSE, single-line IF and expressions. Supported comparisons include Fortran and Fortran-90 forms, logical operators, DO WHILE, and documented intrinsic/protective functions.
A variable assigned in only some branches retains its previous-record value and begins at zero for a new subject, matching NONMEM behavior. This persistence can implement time-varying states but also creates subtle bugs when a branch fails to assign/reset.
Test every branch with records constructed to cross thresholds and switch mid-profile. Decimal equality, dropped text columns and repeated IDs deserve explicit cases. Covariates already used are recognized by inspect/validate.
Test covariate timing explicitly
Separate baseline covariates from time-varying covariates in the specification. For baseline values, verify consistency within subject or define the intended resolution rule. For time-varying values, test update timing relative to dose and observation records, interpolation or carry-forward assumptions, and behavior before the first measured value.
Branching logic should cover every record. Conditions based on study, formulation, renal function or occasion need explicit default behavior and tests at category and numeric boundaries. Missing covariates should fail or follow a documented imputation rule; they should not silently choose a branch because of parser behavior.
Variable persistence affects reproducibility across records and subjects. Initialize accumulators and flags deliberately, verify subject resets, and avoid depending on an evaluation order not guaranteed by the model semantics. A simple two-subject test with different branches can reveal leakage of state from one subject to another.
Common mistakes
Do not assume variables reset each record, compare floating values with equality casually, or leave an ELSE path undefined. Avoid branch validation based only on final parameter estimates.
Checklist
- Enumerate branch conditions and assignments.
- Test persistence/reset per subject.
- Exercise mid-profile changes.
- Validate logical/intrinsic semantics.
- Compare fixed predictions by branch.
Further reading
See docs/FITTING.md data logic, Chapter 12 control-stream QC, and cross-software reproduction.