22# ' @title Create survival probabilities
33# ' @name make_surv_methods
44# '
5- # ' @description These function are version of the [survHE::make.surv()] function
6- # ' from \pkg{survHE}. These are needed prior to blending.
5+ # ' @description
6+ # ' A generic S3 function and methods to generate a standardized matrix of survival
7+ # ' probabilities from various fitted survival model objects.
8+ # '
9+ # ' This function standardizes the output from different survival modelling packages
10+ # ' into a consistent format: a matrix where rows represent discrete time points
11+ # ' and columns represent simulations from the model's posterior distribution. This
12+ # ' standardized format is essential for use in downstream evidence blending
13+ # ' functions. The methods are inspired by the [survHE::make.surv()] function.
14+ # '
15+ # ' @param Surv A fitted survival model object or a matrix/vector of survival
16+ # ' probabilities. Supported classes include `survHE`, `flexsurvreg`, `inla`,
17+ # ' `matrix`, and `numeric`.
18+ # ' @param ... Additional arguments passed to specific methods (e.g., `t`, `nsim`).
19+ # ' @return A matrix with `length(t)` rows and `nsim` columns. Each element `[i, j]`
20+ # ' is the survival probability at time `t[i]` for simulation `j`.
21+ # '
22+ # ' @seealso [survHE::make.surv()]
723# '
8- # ' @param Surv survival analysis object
9- # ' @param ... Additional arguments
10- # ' @return Matrix of survival probabilities
1124# ' @export
1225# '
13- # ' @examplesIf rlang::is_installed("survHEhmc")
14- # ' library(survHE)
26+ # ' @examples
27+ # ' # Define common time points and number of simulations for examples
28+ # ' time_points <- 1:100
29+ # ' n_sim <- 50
30+ # '
31+ # ' #--------------------------------------
32+ # ' ## Method for a 'survHE' object
33+ # ' #--------------------------------------
34+ # ' if (rlang::is_installed("survHE") && rlang::is_installed("survival")) {
35+ # ' library(survHE)
36+ # ' library(survival)
37+ # ' data(ovarian)
38+ # '
39+ # ' # Fit a Weibull model using survHE (with MLE for speed)
40+ # ' fit_she <- fit.models(
41+ # ' formula = Surv(futime, fustat) ~ 1,
42+ # ' data = ovarian,
43+ # ' distr = "weibull",
44+ # ' method = "mle"
45+ # ' )
46+ # '
47+ # ' # Generate survival probability matrix
48+ # ' surv_matrix_she <- make_surv(fit_she, t = time_points, nsim = n_sim)
49+ # ' cat("survHE method output dimensions:", dim(surv_matrix_she), "\n")
50+ # ' }
51+ # '
52+ # ' #--------------------------------------
53+ # ' ## Method for a 'flexsurvreg' object
54+ # ' #--------------------------------------
55+ # ' if (rlang::is_installed("flexsurv") && rlang::is_installed("survival")) {
56+ # ' library(flexsurv)
57+ # ' library(survival)
58+ # '
59+ # ' # Fit a log-logistic model using flexsurv
60+ # ' fit_fsr <- flexsurvreg(
61+ # ' formula = Surv(futime, fustat) ~ 1,
62+ # ' data = ovarian,
63+ # ' dist = "llogis"
64+ # ' )
1565# '
16- # ' ## trial data
17- # ' data("TA174_FCR", package = "blendR")
66+ # ' # Generate survival probability matrix
67+ # ' surv_matrix_fsr <- make_surv(fit_fsr, t = time_points, nsim = n_sim)
68+ # ' cat("flexsurvreg method output dimensions:", dim(surv_matrix_fsr), "\n")
69+ # ' }
1870# '
19- # ' ## externally estimated data
20- # ' data_sim <- ext_surv_sim(t_info = 144,
21- # ' S_info = 0.05,
22- # ' T_max = 180)
71+ # ' #--------------------------------------
72+ # ' ## Default method for a numeric vector (e.g., from a Kaplan-Meier curve)
73+ # ' #--------------------------------------
74+ # ' if (rlang::is_installed("survival")) {
75+ # ' library(survival)
76+ # ' km_fit <- survfit(Surv(futime, fustat) ~ 1, data = ovarian)
77+ # ' # Extract survival probabilities at our time points
78+ # ' km_summary <- summary(km_fit, times = time_points)
2379# '
24- # ' ext_Surv <- fit.models(formula = Surv(time, event) ~ 1,
25- # ' data = data_sim,
26- # ' distr = "exponential",
27- # ' method = "hmc")
80+ # ' # Generate matrix by replicating the single survival curve
81+ # ' surv_matrix_vec <- make_surv(km_summary$surv, t = 0:(length(km_summary$surv) - 1), nsim = n_sim)
82+ # ' cat("Default (vector) method output dimensions:", dim(surv_matrix_vec), "\n")
83+ # ' }
2884# '
29- # ' S_ext <- make_surv(ext_Surv, t = 1:100, nsim = 100)
85+ # ' #--------------------------------------
86+ # ' ## Default method for a matrix (pre-simulated curves)
87+ # ' #--------------------------------------
88+ # ' # Create a sample matrix of survival probabilities (500 time points, 50 simulations)
89+ # ' pre_sim_matrix <- sapply(1:n_sim, function(i) 1 - pweibull(1:500, shape = 1.5, scale = 100 + i))
3090# '
91+ # ' # Use make_surv to subset the matrix for our desired time points
92+ # ' surv_matrix_mat <- make_surv(pre_sim_matrix, t = time_points)
93+ # ' cat("Default (matrix) method output dimensions:", dim(surv_matrix_mat), "\n")
94+ # '
95+ # ' #--------------------------------------
96+ # ' ## Method for 'inla' objects (conceptual example)
97+ # ' #--------------------------------------
98+ # ' \dontrun{
99+ # ' if (rlang::is_installed("INLA")) {
100+ # ' # This method requires a fitted 'inla' object, typically from a
101+ # ' # piecewise exponential model (poisson likelihood).
102+ # '
103+ # ' # Assuming 'fit_inla' is a valid model object from INLA:
104+ # ' # surv_matrix_inla <- make_surv(fit_inla, t = time_points, nsim = n_sim)
105+ # ' # print(dim(surv_matrix_inla))
106+ # ' }
107+ # ' }
31108make_surv <- function (Surv , ... )
32109 UseMethod(" make_surv" , Surv )
33110
34111
35112# ' @rdname make_surv_methods
36- # ' @param t Time
37- # ' @param nsim Number of simulations
113+ # ' @param t A numeric vector of time points at which to calculate survival
114+ # ' probabilities. The behaviour for `NULL` varies by method.
115+ # ' @param nsim The number of simulations to generate from the model's posterior
116+ # ' distribution. Defaults to 100.
38117# ' @importFrom survHE make.surv
39118# ' @export
40119# '
@@ -45,8 +124,10 @@ make_surv.survHE <- function(Surv, t, nsim = 100, ...) {
45124
46125
47126# ' @rdname make_surv_methods
48- # ' @param t Time
49- # ' @param nsim Number of simulations
127+ # ' @details For `flexsurvreg` objects, parameters are sampled from the asymptotic
128+ # ' normal distribution of the maximum likelihood estimates using
129+ # ' `flexsurv::normboot.flexsurvreg()`. If `t` is `NULL`, the unique
130+ # ' event/censoring times from the model's source data are used.
50131# ' @importFrom survHE make.surv
51132# ' @importFrom flexsurv normboot.flexsurvreg
52133# ' @export
@@ -67,8 +148,18 @@ make_surv.flexsurvreg <- function(Surv, t = NULL, nsim = 100, ...) {
67148
68149
69150# ' @rdname make_surv_methods
70- # ' @param t Time points; vector
71- # ' @param nsim Number of simulations; integer
151+ # '
152+ # ' @details
153+ # ' ### INLA Method
154+ # ' The `inla` method requires the **INLA** package. As it is not available on CRAN,
155+ # ' you must install it from its own repository:
156+ # ' `install.packages("INLA", repos = c(getOption("repos"), INLA = "https://inla.r-inla-download.org/R/stable"), dep = TRUE)`
157+ # '
158+ # ' This method is designed for `inla` objects fitted with a `poisson` likelihood
159+ # ' for piecewise exponential models. It samples from the joint posterior of the
160+ # ' baseline hazard to calculate survival probabilities. If `t` is `NULL`, the
161+ # ' interval cut-points for the baseline hazard from the model are used.
162+ # '
72163# ' @import sn
73164# ' @importFrom tibble as_tibble
74165# ' @importFrom dplyr select contains
@@ -144,6 +235,19 @@ make_surv.inla <- function(Surv, t = NULL, nsim = 100, ...) {
144235
145236# ' @rdname make_surv_methods
146237# '
238+ # ' @details
239+ # ' ### Default Method
240+ # ' The default method handles pre-computed survival probabilities.
241+ # ' - If `Surv` is a **vector**, it is treated as a single survival curve (e.g.,
242+ # ' from a Kaplan-Meier estimate). The function replicates this curve `nsim`
243+ # ' times to form the output matrix.
244+ # ' - If `Surv` is a **matrix**, it is assumed to already be in the desired
245+ # ' (time x simulations) format. The function will simply subset rows based on `t`.
246+ # '
247+ # ' If `t` is `NULL`, a sequence `0, 1, 2, ...` is generated based on the length
248+ # ' or number of rows of `Surv`. Note that time points are used as 1-based indices,
249+ # ' so `t = 0` corresponds to the first row/element.
250+ # '
147251# ' @export
148252make_surv.default <- function (Surv ,
149253 t = NULL ,
0 commit comments