diff --git a/DESCRIPTION b/DESCRIPTION index 5bc1bd0..a1fd7a1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bmstate Type: Package Title: Bayesian multistate modeling -Version: 0.2.4 +Version: 0.2.5 Authors@R: c(person(given = "Juho", family = "Timonen", 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..3493760 100644 --- a/R/PathData.R +++ b/R/PathData.R @@ -527,6 +527,26 @@ 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") { + 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_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 #' #' @export 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) { 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-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) |> 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")