From 002acc2a9808d5aff3ec7d8d8d710b0f933d93ce Mon Sep 17 00:00:00 2001 From: jtimonen Date: Thu, 20 Nov 2025 10:28:44 +0200 Subject: [PATCH 1/5] Increment version number to 0.2.4.9000 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5bc1bd0..644d76b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bmstate Type: Package Title: Bayesian multistate modeling -Version: 0.2.4 +Version: 0.2.4.9000 Authors@R: c(person(given = "Juho", family = "Timonen", From 7453e8e226a8b28d9a4927c39db91a85c66ce538 Mon Sep 17 00:00:00 2001 From: jtimonen Date: Thu, 20 Nov 2025 16:38:49 +0200 Subject: [PATCH 2/5] add time info to fit --- R/stan.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/stan.R b/R/stan.R index 7ffcd80..5e16d78 100644 --- a/R/stan.R +++ b/R/stan.R @@ -138,7 +138,8 @@ fit_stan <- function(model, data, info <- list( diag = diag, runset = stan_fit$runset, - summary = smr$result + summary = smr$result, + time = stan_fit$time() ) fit <- MultistateModelFit$new(data, sd, model, draws, info) if (!return_stanfit) { From 43e13f1c8a16680f38357693da5d76c3521a9380 Mon Sep 17 00:00:00 2001 From: jtimonen Date: Thu, 20 Nov 2025 17:00:52 +0200 Subject: [PATCH 3/5] add the as_any_event method --- NAMESPACE | 1 + R/PathData.R | 19 +++++++++++++++++++ man/as_any_event.Rd | 19 +++++++++++++++++++ tests/testthat/test-workflow.R | 4 ++++ 4 files changed, 43 insertions(+) create mode 100644 man/as_any_event.Rd diff --git a/NAMESPACE b/NAMESPACE index b60b1ee..ba15d71 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(PKModel) export(PSSDosingData) export(PathData) export(TransitionMatrix) +export(as_any_event) export(as_single_event) export(as_survival) export(c_index) diff --git a/R/PathData.R b/R/PathData.R index 98f5309..fbbf591 100644 --- a/R/PathData.R +++ b/R/PathData.R @@ -527,6 +527,25 @@ potential_covariates <- function(pd, possible = NULL, ...) { df } +#' PathData to time-to-event data format for any state other than null state +#' +#' @export +#' @inheritParams as_single_event +#' @return A \code{\link{PathData}} object +as_any_event <- function(pd, null_state = "Randomization") { + df <- pd$path_df + idx <- which(pd$transmat$states == null_state) + if (length(idx) != 1) { + stop("error") + } + df$state[which(df$state == idx)] <- 1 + df$state[which(df$state != 1)] <- 2 + tm <- transmat_survival(state_names = c(null_state, "Any event")) + pd$path_df <- df + pd$transmat <- tm + as_single_event(pd, "Any event", null_state) +} + #' PathData to time-to-event data format with a single event #' #' @export diff --git a/man/as_any_event.Rd b/man/as_any_event.Rd new file mode 100644 index 0000000..ddebc59 --- /dev/null +++ b/man/as_any_event.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PathData.R +\name{as_any_event} +\alias{as_any_event} +\title{PathData to time-to-event data format for any state other than null state} +\usage{ +as_any_event(pd, null_state = "Randomization") +} +\arguments{ +\item{pd}{A \code{\link{PathData}} object} + +\item{null_state}{Name of the base state} +} +\value{ +A \code{\link{PathData}} object +} +\description{ +PathData to time-to-event data format for any state other than null state +} diff --git a/tests/testthat/test-workflow.R b/tests/testthat/test-workflow.R index f933c14..859e973 100644 --- a/tests/testthat/test-workflow.R +++ b/tests/testthat/test-workflow.R @@ -40,6 +40,10 @@ test_that("entire workflow works", { fit <- a$fit expect_true(inherits(fit, "MultistateModelFit")) + # Test + a <- as_any_event(jd$train$paths, "Healthy") + checkmate::assert_true(all(a$state_names() == c("Healthy", "Any event"))) + # Print expect_output(print(fit), "A MultistateModelFit with 30 draws") From bfd0878d43d3f2c888d10c22f8ab48c70ca169a5 Mon Sep 17 00:00:00 2001 From: jtimonen Date: Thu, 20 Nov 2025 17:14:35 +0200 Subject: [PATCH 4/5] fix as_any_event() --- R/PathData.R | 11 ++++++----- tests/testthat/test-cav.R | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/R/PathData.R b/R/PathData.R index fbbf591..3493760 100644 --- a/R/PathData.R +++ b/R/PathData.R @@ -533,17 +533,18 @@ potential_covariates <- function(pd, possible = NULL, ...) { #' @inheritParams as_single_event #' @return A \code{\link{PathData}} object as_any_event <- function(pd, null_state = "Randomization") { - df <- pd$path_df - idx <- which(pd$transmat$states == null_state) + pd_new <- pd$clone(deep = TRUE) + df <- pd_new$path_df + idx <- which(pd_new$transmat$states == null_state) if (length(idx) != 1) { stop("error") } df$state[which(df$state == idx)] <- 1 df$state[which(df$state != 1)] <- 2 tm <- transmat_survival(state_names = c(null_state, "Any event")) - pd$path_df <- df - pd$transmat <- tm - as_single_event(pd, "Any event", null_state) + pd_new$path_df <- df + pd_new$transmat <- tm + as_single_event(pd_new, "Any event", null_state) } #' PathData to time-to-event data format with a single event diff --git a/tests/testthat/test-cav.R b/tests/testthat/test-cav.R index 864238d..3a50401 100644 --- a/tests/testthat/test-cav.R +++ b/tests/testthat/test-cav.R @@ -1,7 +1,7 @@ -library(ggplot2) -library(dplyr) - test_that("cav data analysis works", { + library(ggplot2) + library(dplyr) + # Modify original data like in # https://rviews.rstudio.com/2023/04/19/multistate-models-for-medical-applications/ df_full <- as_tibble(msm::cav) |> From f5a6d2c40a4f60232b38c777f44f5bfeba2788eb Mon Sep 17 00:00:00 2001 From: jtimonen Date: Thu, 20 Nov 2025 17:18:43 +0200 Subject: [PATCH 5/5] Increment version number to 0.2.5 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 644d76b..a1fd7a1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bmstate Type: Package Title: Bayesian multistate modeling -Version: 0.2.4.9000 +Version: 0.2.5 Authors@R: c(person(given = "Juho", family = "Timonen",