diff --git a/CITATION.cff b/CITATION.cff index 1cb663c..4f9748a 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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" diff --git a/DESCRIPTION b/DESCRIPTION index c338cb0..9e70e06 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index 3dd1ca2..038e7e9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 @@ -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) diff --git a/R/Functions.R b/R/Functions.R index 94db172..ea53871 100644 --- a/R/Functions.R +++ b/R/Functions.R @@ -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") diff --git a/README.Rmd b/README.Rmd index b11caec..4384237 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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). diff --git a/README.md b/README.md index 8d6b9cc..a64b8eb 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tests/testthat/test-transition-checks.R b/tests/testthat/test-transition-checks.R new file mode 100644 index 0000000..1e253d8 --- /dev/null +++ b/tests/testthat/test-transition-checks.R @@ -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") +})