Skip to content

v0.3.0: fix gen_wcc() Simpson weights and state-residence PSA; add tests, CI and renv - #1

Merged
feralaes merged 7 commits into
mainfrom
fix/gen-wcc-simpson
Nov 10, 2026
Merged

feralaes merged 7 commits into
mainfrom
fix/gen-wcc-simpson

Conversation

@feralaes

Copy link
Copy Markdown
Contributor

Summary

Fixes the Simpson's 1/3 within-cycle correction (WCC) bug that was reported in the introductory tutorial (DARTH-git/cohort-modeling-tutorial-intro#2) and shared by this repository. The PR also fixes several bugs found along the way, and adds tests, CI, renv and citation metadata, matching cohort-modeling-tutorial-intro v0.3.0.

Results change slightly. Conclusions don't.

Model ICER, Strategy B (old → new) ICER, Strategy AB (old → new)
Simulation-time $65,288 → $65,611 $104,461 → $104,125
State-residence $75,678 → $76,060 $88,668 → $88,477

QALYs rise by about 0.32 per strategy. The efficient frontier (SoC, B, AB) and the dominance of Strategy A are unchanged. The full before/after table is in NEWS.md. The old code reproduced the published values exactly.

Bug fixes

  • gen_wcc() now assigns weights by time t = 0, …, n instead of by 1-based vector position. For odd n (the tutorial uses 75), it uses Simpson's 3/8 rule over the last three cycles. This is the same implementation as the intro tutorial and darthtools v0.4.1.
  • The state-residence PSA failed on main. It stopped at parameter set 220, because check_sum_of_transition_array() used exact == on floating-point sums. It now uses all.equal(), as darthtools does. The script also used patchwork without loading it. Both analysis scripts now run end to end.
  • Simulation-time calculate_ce_out() swapped the cost and QALY discount rates. The analysis scripts were fixed in 2024; the PSA function wasn't. This has no effect at 3%/3%.
  • The PSA functions depended on global objects. State-residence calculate_ce_out() read v_wcc, v_dwc and v_dwe from the global environment, and both models read the strategy and state names from there. The functions now define them.

Infrastructure and docs

  • testthat tests (180 expectations): gen_wcc(), base-case results of both models, PSA wrapper consistency, PSA input means and transition-array check tolerance.
  • A CI workflow running the tests and both analyses on Ubuntu and macOS.
  • renv.lock, which pins MethylCapSig 1.0.1 from the CRAN archive. It has been removed from CRAN.
  • New DESCRIPTION and CITATION.cff.
  • NEWS.md and a README rewrite.
  • manuscript/README.md documents two errata: the WCC weights, and Table 2 listing c_trtB as $12,000 instead of $13,000 (reported in Parameter table prints c_trtB = $12,000; the analysis code and the published results use $13,000 cohort-modeling-tutorial-intro#1). The manuscript files themselves stay archival.

Checks

  • testthat::test_dir("tests/testthat"): all pass, locally and with renv active.
  • Both analysis scripts run end to end locally. The full 1,000-sample state-residence PSA runs in about 4 minutes.

gen_wcc(method = "Simpson1/3") assigned the 4/3 and 2/3 weights by position
in the 1-based R vector instead of by time t = 0, ..., n, returning
1/3, 2/3, 4/3, ..., 1/3 instead of 1/3, 4/3, 2/3, ..., 1/3.

- Weights are now assigned by time.
- For an odd number of cycles (the tutorial uses 75), apply the composite
  Simpson's 1/3 rule over the first n - 3 cycles and Simpson's 3/8 rule over
  the last three, so polynomials up to degree three are integrated exactly.
- Validate that n_cycles is a single positive integer (and >= 2 for Simpson).
- Same implementation as cohort-modeling-tutorial-intro v0.3.0 and
  darthtools v0.4.1.

Also: remove a stray anonymous function after gen_wcc(), and replace
deprecated ggplot2 arguments (geom_line(size =) -> linewidth, ..density.. ->
after_stat(density)).

Reported independently by Lauren N. Steimle and by GitHub user ambtnhlr
(DARTH-git/cohort-modeling-tutorial-intro#2).
- Simulation-time model: calculate_ce_out() discounted costs with d_e and
  QALYs with d_c. No numerical effect in the tutorial (both are 3%).
- State-residence model: calculate_ce_out() used v_wcc, v_dwc and v_dwe from
  the global environment instead of computing them, so the PSA only worked
  after running the deterministic part of the analysis script. They are now
  computed inside the function, using the corrected gen_wcc().
- Both models: decision_model(), calculate_ce_out() and
  generate_epi_measures_SoC() read the strategy and state names from the
  global environment. They now define them themselves.
- gen_wcc(): exact weights and exact integration of polynomials up to degree three (same tests as the intro tutorial).
- Base-case costs, QALYs, ICERs and dominance of both models.
- The PSA wrappers apply the corrected WCC, do not depend on global objects, and do not swap discount rates.
- The PSA distributions are centred on the base-case values (catches the c_trtB table erratum).
- check_sum_of_transition_array() compared each cycle's sum of transition
  probabilities with n_states using exact equality, so rounding error
  (~1e-14) in a valid array stopped the PSA at parameter set 220. Use
  all.equal(), as in darthtools.
- analysis/cSTM_time_dep_state_residence.R combined the PSA figures with
  patchwork without loading it.
- Add regression tests for both.
- DESCRIPTION records the project version (0.3.0) and dependencies.
- CITATION.cff gives citation metadata for the code (Zenodo concept DOI 10.5281/zenodo.5338818) and the article.
- renv.lock pins package versions, including MethylCapSig 1.0.1 from the CRAN archive.
- CI runs the tests and both analysis scripts on Ubuntu and macOS.
- NEWS.md with a before/after table of base-case results for both models.
- README: quick start with renv, repository structure, corrections, contributing, acknowledgements.
- manuscript/README.md documents the WCC and c_trtB parameter-table errata; the manuscript files stay archival.
Whether three given probabilities sum exactly to 3 depends on floating-point accumulation (it did on Ubuntu x86_64 but not on macOS arm64). Add an explicit 1e-13 error instead.
@feralaes
feralaes merged commit 88c0d0e into main Nov 10, 2026
2 checks passed
@feralaes
feralaes deleted the fix/gen-wcc-simpson branch November 10, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant