| Title: | Bayesian Relative Mixture Cure Modelling |
|---|---|
| Description: | Jointly models two event time distributions (e.g. time to disease progression and time to death) within a Bayesian relative survival mixture cure model framework, using the "Stan" engine called from 'R'. It extends current methods to account for the dependence between event times to leverage additional information to make better inferences and decisions. |
| Authors: | Nathan Green [aut, cre] (ORCID: <https://orcid.org/0000-0003-2745-1736>, ROR: <https://ror.org/052gg0110>) |
| Maintainer: | Nathan Green <n.green@ucl.ac.uk> |
| License: | MIT + file LICENSE |
| Version: | 0.0.1 |
| Built: | 2026-10-11 09:15:20 UTC |
| Source: | https://github.com/statisticshealtheconomics/multimcm |
The .default() function is used internally to mark certain values as
default, so that the user may be notified when default values are being used.
For example, choosing a default reference treatment for a network, or using
default prior distributions. The function .is_default() checks whether an
argument/object is set to a default value. Neither of these functions are
intended to be called by the user.
.default(x = list()) .is_default(x).default(x = list()) .is_default(x)
x |
An object |
For .default(), an identical object with additional attribute
.default. For .is_default(), a logical value (TRUE or FALSE).
Jointly models two or more event time distributions within a Bayesian relative survival mixture cure model framework. The function generates custom Stan code on-the-fly (or uses precompiled models) and estimates treatment effects simultaneously for both the cure fraction and the latent survival distributions. It allows for complex hierarchical structures in the cure fraction, background mortality adjustment, and multiple parametric families for the latent survival times (e.g., exponential, Weibull, Gompertz).
bmcm_stan( input_data, formula, cureformula = ~1, endpoint_var = NULL, family_latent = "exponential", prior_latent = .default(NA), prior_cure = .default(list()), centre_coefs = FALSE, bg_model = c("bg_distn", "bg_fixed"), bg_varname = "bg_rate", bg_hr = 1, t_max = 70, save_stan_code = FALSE, read_stan_code = FALSE, precompiled_model_path = NA, use_cmdstanr = FALSE, ... )bmcm_stan( input_data, formula, cureformula = ~1, endpoint_var = NULL, family_latent = "exponential", prior_latent = .default(NA), prior_cure = .default(list()), centre_coefs = FALSE, bg_model = c("bg_distn", "bg_fixed"), bg_varname = "bg_rate", bg_hr = 1, t_max = 70, save_stan_code = FALSE, read_stan_code = FALSE, precompiled_model_path = NA, use_cmdstanr = FALSE, ... )
input_data |
A long-format data frame containing the input data. |
formula |
An R formula object specifying the latent model component. |
cureformula |
An R formula object specifying the cure fraction model component. Default is |
endpoint_var |
A string specifying the column in |
family_latent |
A string or vector specifying the distribution(s) for the latent model component.
Supported values are |
prior_latent |
An optional prior specification for the latent model component. Default is |
prior_cure |
An optional list specifying priors for the cure fraction model component. Default is an empty list. |
centre_coefs |
Logical. If |
bg_model |
A string specifying the background model type. Supported values are |
bg_varname |
A string specifying the background variable name in |
bg_hr |
A numeric value for the background hazard ratio adjustment. Default is |
t_max |
A numeric value specifying the maximum time horizon for the model. Default is |
save_stan_code |
Logical. If |
read_stan_code |
Logical. If |
precompiled_model_path |
A string specifying the path to a precompiled model. Default is |
use_cmdstanr |
Logical. If |
... |
Additional parameters to pass to the Stan sampler. |
An object of class bmcm, which is a list containing the following components:
output: The fitted Stan model object (either a stanfit object from rstan or a CmdStanMCMC object from cmdstanr).
mcmc_params: A list of the MCMC sampling parameters used (e.g., iterations, warmup, chains).
call: The matched call to the function.
distns: A character vector of the latent survival distributions used.
inputs: A list of the formatted data inputs passed directly to the Stan model.
input_data: The original input_data data frame provided.
formula: A list containing the parsed cure and latent model formulas.
## Not run: data("surv_input_data", package = "multimcm") out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA + (1 | center_id)", endpoint_var = "center_id", family_latent = "exponential", bg_model = "bg_fixed", bg_varname = "rate", t_max = 400 ) ## End(Not run)## Not run: data("surv_input_data", package = "multimcm") out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA + (1 | center_id)", endpoint_var = "center_id", family_latent = "exponential", bg_model = "bg_fixed", bg_varname = "rate", t_max = 400 ) ## End(Not run)
Generates a forest plot of the cure fractions for different treatments using Stan model outputs fitted with specific time cut-points. Useful for comparing cure probabilities across interventions.
cf_forest_cutpoint( distns = list(c("exp", "exp"), c("lognormal", "lognormal")), folder = NA, save_name = c("_12", "_30", "_100"), is_hier = TRUE )cf_forest_cutpoint( distns = list(c("exp", "exp"), c("lognormal", "lognormal")), folder = NA, save_name = c("_12", "_30", "_100"), is_hier = TRUE )
distns |
A list or character vector of distribution names used in the model fits. |
folder |
A string specifying the directory path containing the saved model output files. |
save_name |
A string or vector of text strings appended to the file names (e.g., specific cut-points). |
is_hier |
Logical. Is it a hierarchical model? If |
A ggplot2 object showing the cure fraction forest plot.
## Not run: # independent model fp_sep <- cf_forest_cutpoint(folder = "data/dbl_cut/separate", save_name = c("_30", "_12", "_100")) #ggsave(fp_sep, filename = "plots/forest_plot_cf_sep_cpt.png", # dpi = 640, width = 16, height = 14) # hierarchical model fp_hier <- cf_forest_cutpoint(folder = "data/dbl_cut/hier", save_name = c("_30", "_12", "_100")) #ggsave(fp_hier, filename = "plots/forest_plot_cf_hier_cpt.png", # dpi = 640, width = 16, height = 14) ## End(Not run)## Not run: # independent model fp_sep <- cf_forest_cutpoint(folder = "data/dbl_cut/separate", save_name = c("_30", "_12", "_100")) #ggsave(fp_sep, filename = "plots/forest_plot_cf_sep_cpt.png", # dpi = 640, width = 16, height = 14) # hierarchical model fp_hier <- cf_forest_cutpoint(folder = "data/dbl_cut/hier", save_name = c("_30", "_12", "_100")) #ggsave(fp_hier, filename = "plots/forest_plot_cf_hier_cpt.png", # dpi = 640, width = 16, height = 14) ## End(Not run)
Generates a forest plot displaying the estimated cure fractions across all treatments derived from the joint relative survival mixture cure model output.
cf_forest_plot(folder = "data/")cf_forest_plot(folder = "data/")
folder |
A string specifying the directory path containing the saved Stan model output files. |
A ggplot2 object showing the cure fraction forest plot.
## Not run: # independent model fp_sep <- cf_forest_plot("data/separate/") #ggsave(fp_sep, filename = "plots/forest_plot_sep_multimcm.png", dpi = 640, width = 12, height = 8) # hierarchical model fp_hier <- cf_forest_plot("data/") #ggsave(fp_hier, filename = "plots/forest_plot_multimcm.png", dpi = 640, width = 12, height = 8) ## End(Not run)## Not run: # independent model fp_sep <- cf_forest_plot("data/separate/") #ggsave(fp_sep, filename = "plots/forest_plot_sep_multimcm.png", dpi = 640, width = 12, height = 8) # hierarchical model fp_hier <- cf_forest_plot("data/") #ggsave(fp_hier, filename = "plots/forest_plot_multimcm.png", dpi = 640, width = 12, height = 8) ## End(Not run)
Geom for Kaplan-Meier ggplot
geom_kaplan_meier(out_dat, col = "black", add_marks = TRUE)geom_kaplan_meier(out_dat, col = "black", add_marks = TRUE)
out_dat |
bmcm class output list |
col |
colour string |
add_marks |
logical |
Generates a plot of the survival curves for the joint relative survival mixture cure model, overlaying the predicted survival probabilities and (optionally) the original Kaplan-Meier curves for all treatments.
## S3 method for class 'bmcm' plot(x, ...) plot_S_joint( bmcm_out, facet = TRUE, annot_cf = FALSE, add_km = FALSE, add_marks = TRUE, ... )## S3 method for class 'bmcm' plot(x, ...) plot_S_joint( bmcm_out, facet = TRUE, annot_cf = FALSE, add_km = FALSE, add_marks = TRUE, ... )
x |
An object of class |
... |
Additional parameters passed to the plotting function. |
bmcm_out |
bmcm class output list, as returned by |
facet |
Logical. Should the plots for each endpoint be separated into facets? Default is |
annot_cf |
Logical. Annotate the plot with the cure fractions? Default is |
add_km |
Logical. Include an overlaid Kaplan-Meier curve of the raw data? Default is |
add_marks |
Logical. Include Kaplan-Meier censoring marks? Default is |
Use results of running Stan with bmcm_stan()
relative survival joint mixture cure model.
A ggplot2 object showing the survival curves.
## Not run: data("surv_input_data", package = "multimcm") out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA + (1 | center_id)", family_latent = "exponential", bg_model = "bg_fixed", bg_varname = "rate" ) # Generate the survival plot, facetting by endpoint surv_plot <- plot_S_joint(out, facet = TRUE, add_km = TRUE) print(surv_plot) ## End(Not run)## Not run: data("surv_input_data", package = "multimcm") out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA + (1 | center_id)", family_latent = "exponential", bg_model = "bg_fixed", bg_varname = "rate" ) # Generate the survival plot, facetting by endpoint surv_plot <- plot_S_joint(out, facet = TRUE, add_km = TRUE) print(surv_plot) ## End(Not run)
Precompiles the Stan model code generated by create_stancode for a given model specification.
This is useful when you want to avoid recompiling the model multiple times during repeated analyses or simulations.
precompile_bmcm_model( input_data, family_latent = "exponential", cureformula = ~1, model_name = NA, use_cmdstanr = FALSE, file_path = NA )precompile_bmcm_model( input_data, family_latent = "exponential", cureformula = ~1, model_name = NA, use_cmdstanr = FALSE, file_path = NA )
input_data |
Data frame |
family_latent |
String specifying latent distribution family |
cureformula |
Formula for cure fraction |
model_name |
Optional string for model name |
use_cmdstanr |
Logical. If TRUE, use cmdstanr to compile the model. Default is FALSE. |
file_path |
Optional string for file path to save compiled model |
A compiled stan model object
## Not run: data("surv_input_data", package = "multimcm") # Precompile the model compiled_model <- precompile_bmcm_model( input_data = surv_input_data, family_latent = "exponential", cureformula = "~ TRTA + (1 | center_id)", model_name = "my_precompiled_model" ) # Pass the compiled model file path to bmcm_stan to avoid recompilation out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA + (1 | center_id)", family_latent = "exponential", precompiled_model_path = "./my_precompiled_model.RDS" ) ## End(Not run)## Not run: data("surv_input_data", package = "multimcm") # Precompile the model compiled_model <- precompile_bmcm_model( input_data = surv_input_data, family_latent = "exponential", cureformula = "~ TRTA + (1 | center_id)", model_name = "my_precompiled_model" ) # Pass the compiled model file path to bmcm_stan to avoid recompilation out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA + (1 | center_id)", family_latent = "exponential", precompiled_model_path = "./my_precompiled_model.RDS" ) ## End(Not run)
Background hazard point values.
prep_bg_data(input_data, bg_varname, formula_cure, event_type, suffix = TRUE)prep_bg_data(input_data, bg_varname, formula_cure, event_type, suffix = TRUE)
input_data |
Data frame |
bg_varname |
String for background variable name |
formula_cure |
Parsed formula list |
event_type |
String specifying event type |
suffix |
Logical indicating whether to append suffix |
A list with background data
Data specific to end type for Stan input.
prep_latent_data( input_data, formula_cure, formula_latent, event_type, centre_coefs = FALSE, suffix = TRUE )prep_latent_data( input_data, formula_cure, formula_latent, event_type, centre_coefs = FALSE, suffix = TRUE )
input_data |
Data frame |
formula_cure |
parsed formula |
formula_latent |
parsed formula |
event_type |
cluster/group |
centre_coefs |
Logical |
suffix |
Logical |
List consisting of: sample size, times, censoring indicator, number of covariates, covariates
Prepare posterior survival data for plotting
prep_S_data( stan_extract, event_type = NA, CI_probs = c(0.025, 0.5, 0.975), tx_idx = NA )prep_S_data( stan_extract, event_type = NA, CI_probs = c(0.025, 0.5, 0.975), tx_idx = NA )
stan_extract |
List of stan output |
event_type |
String specifying event type |
CI_probs |
Numeric vector of credible interval probabilities |
tx_idx |
Numeric vector of treatment indices |
dataframe
Prepare data for survival plot
prep_S_joint_data(bmcm_out, ...)prep_S_joint_data(bmcm_out, ...)
bmcm_out |
Output of Stan model |
... |
Additional arguments |
An internal/helper function used to extract and format survival predictions from a fitted bmcm model.
It generates a long-format data frame containing the mean survival estimates and their credible intervals across time points.
## S3 method for class 'bmcm' prep_S_joint_data(bmcm_out, ...)## S3 method for class 'bmcm' prep_S_joint_data(bmcm_out, ...)
bmcm_out |
Output list from the |
... |
Additional arguments passed to methods. |
A data frame containing the summarized survival predictions.
## Not run: data("surv_input_data", package = "multimcm") out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA", family_latent = "exponential", bg_model = "bg_fixed", bg_varname = "rate" ) plot_data <- prep_S_joint_data(out) head(plot_data) ## End(Not run)## Not run: data("surv_input_data", package = "multimcm") out <- bmcm_stan( input_data = surv_input_data, formula = "Surv(time=os, event=os_event) ~ 1", cureformula = "~ TRTA", family_latent = "exponential", bg_model = "bg_fixed", bg_varname = "rate" ) plot_data <- prep_S_joint_data(out) head(plot_data) ## End(Not run)
already extracted matrix
## Default S3 method: prep_S_joint_data(bmcm_out, ...)## Default S3 method: prep_S_joint_data(bmcm_out, ...)
bmcm_out |
Output of Stan model |
... |
Additional arguments |
Default parameters for survival model priors if not supplied.
default_prior_latent(formula_latent, formula_cure)default_prior_latent(formula_latent, formula_cure)
formula_latent |
Formula for latent |
formula_cure |
Formula for cure fraction |
hyper-parameters