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
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type: software
title: >-
R Code for An Introductory Tutorial on Cohort State-Transition Models
in R Using a Cost-Effectiveness Analysis Example
version: 0.3.1
version: 0.3.2
date-released: "2026-11-10"
license: MIT
repository-code: "https://github.com/DARTH-git/cohort-modeling-tutorial-intro"
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Project
Package: cohortModelingTutorialIntro
Title: R Code for An Introductory Tutorial on Cohort State-Transition Models in R
Version: 0.3.1
Version: 0.3.2
Date: 2026-11-10
Authors@R: c(
person("Fernando", "Alarid-Escudero", email = "falarid@stanford.edu",
Expand Down
15 changes: 14 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# cohort-modeling-tutorial-intro (development version)
# cohort-modeling-tutorial-intro 0.3.2 (2026-11-10)

## Bug fixes

* `check_sum_of_transition_array()` (`R/Functions.R`) compared the sum of each
cycle's transition probabilities in a transition *array* with the number of
states using exact equality, so rounding error could make it reject a valid
array. It now uses a numerical tolerance (`all.equal()`), as in darthtools.
This tutorial only checks transition matrices, so its results are not
affected; the same bug stopped the PSA of the state-residence model in
[cohort-modeling-tutorial-timedep](https://github.com/DARTH-git/cohort-modeling-tutorial-timedep)
(fixed there in v0.3.0).

## Documentation

Expand All @@ -13,6 +24,8 @@
* Added `tests/testthat/test-psa-params.R`, which checks that every PSA
distribution is centred on its base-case value (within 2%), so the base
case and the PSA cannot drift apart.
* Added `tests/testthat/test-transition-checks.R` for the transition matrix
and array checks.

# cohort-modeling-tutorial-intro 0.3.1 (2026-11-10)

Expand Down
7 changes: 6 additions & 1 deletion R/Functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,12 @@ check_sum_of_transition_array <- function(a_P,
}
} else {
# For array
valid <- (apply(a_P, d, function(x) sum(rowSums(x))) == n_states)
# Compare with a numerical tolerance: with many states, the sum of each
# cycle's transition probabilities can differ from n_states by rounding
# error (~1e-14), which an exact `==` comparison wrongly rejects.
# Same fix as in darthtools.
valid <- apply(a_P, d, function(x) isTRUE(all.equal(sum(rowSums(x)),
n_states)))
if (!isTRUE(all.equal(as.numeric(sum(valid)), as.numeric(n_cycles)))) {
if(err_stop) {
stop("This is not a valid transition array")
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ You can cite the R code in this repository like this: "we based our analysis on

> Alarid-Escudero F, Krijkamp EM, Enns EA, Yang A, Hunink MGM, Pechlivanoglou P, Jalal H. An Introductory Tutorial on Cohort State-Transition Models in R Using a Cost-Effectiveness Analysis Example. Medical Decision Making, 2023;43(1):3-20. https://doi.org/10.1177/0272989X221103163

> Alarid-Escudero F, Krijkamp EM, Enns EA, Yang A, Hunink MGM, Pechlivanoglou P, Jalal H. R Code for An Introductory Tutorial on Cohort State-Transition Models in R Using a Cost-Effectiveness Analysis Example (Version v0.3.1). Zenodo. [10.5281/zenodo.5223092](https://doi.org/10.5281/zenodo.5223092). Last accessed DD Month YYYY.
> Alarid-Escudero F, Krijkamp EM, Enns EA, Yang A, Hunink MGM, Pechlivanoglou P, Jalal H. R Code for An Introductory Tutorial on Cohort State-Transition Models in R Using a Cost-Effectiveness Analysis Example (Version v0.3.2). Zenodo. [10.5281/zenodo.5223092](https://doi.org/10.5281/zenodo.5223092). Last accessed DD Month YYYY.

If you adapted the code, write "Adapted from:" or "Based on" so readers know you modified it. For more on how and when to cite computer code, see [Writing Code (from the MIT Academic Integrity Handbook)](https://integrity.mit.edu/handbook/writing-code).

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ this repository” button gives the metadata from
> Alarid-Escudero F, Krijkamp EM, Enns EA, Yang A, Hunink MGM,
> Pechlivanoglou P, Jalal H. R Code for An Introductory Tutorial on
> Cohort State-Transition Models in R Using a Cost-Effectiveness
> Analysis Example (Version v0.3.1). Zenodo.
> Analysis Example (Version v0.3.2). Zenodo.
> [10.5281/zenodo.5223092](https://doi.org/10.5281/zenodo.5223092). Last
> accessed DD Month YYYY.

Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-transition-checks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# check_sum_of_transition_array() must accept valid arrays whose cycle sums
# differ from n_states only by floating-point rounding, and reject invalid
# ones. (The exact `==` comparison used up to v0.3.1 made the PSA of the
# state-residence model in cohort-modeling-tutorial-timedep fail.)

test_that("transition array check tolerates floating-point rounding", {
# Rounding-sized error added explicitly: whether a given set of
# probabilities sums exactly to n_states depends on the platform
a_P <- array(rep(c(0.5, 0.3, 0.2), each = 3), dim = c(3, 3, 2))
a_P[1, 1, ] <- a_P[1, 1, ] + 1e-13
expect_false(sum(rowSums(a_P[, , 1])) == 3)
expect_no_error(check_sum_of_transition_array(a_P, n_states = 3, n_cycles = 2,
verbose = FALSE))
# An invalid array is still rejected
a_P[1, 1, 2] <- 0.9
expect_error(check_sum_of_transition_array(a_P, n_states = 3, n_cycles = 2,
verbose = FALSE),
"not a valid transition array")
})

test_that("transition matrix check rejects rows that do not sum to one", {
m_P <- matrix(rep(c(0.5, 0.3, 0.2), each = 3), nrow = 3)
expect_no_error(check_sum_of_transition_array(m_P, n_states = 3, n_cycles = 1,
verbose = FALSE))
m_P[1, 1] <- 0.9
expect_error(check_sum_of_transition_array(m_P, n_states = 3, n_cycles = 1,
verbose = FALSE),
"not a valid transition matrix")
})
Loading