Skip to content

Commit 2092f5f

Browse files
committed
improved help documentation
1 parent 58c7856 commit 2092f5f

File tree

10 files changed

+453
-113
lines changed

10 files changed

+453
-113
lines changed

R/blendsurv.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#' @param obs_Surv,ext_Surv Observed and external data survival curves.
1010
#' These can come from \pkg{survHE}, \pkg{INLA} or \pkg{flexsurv} fits.
1111
#' @param blend_interv Maximum and minimum values for the blending interval.
12-
#' @param beta_params coefficients of a beta distribution
12+
#' @param beta_params Coefficients of a beta distribution.
1313
#' @param times A vector of times for which the survival curves
14-
#' are to be computed; optional
14+
#' are to be computed; optional.
1515
#' @param nsim The number of simulations from the distribution of
16-
#' the survival curves; default 100
16+
#' the survival curves; default 100.
1717
#'
18-
#' @return List of S for observed, external and blended curves.
18+
#' @return List of survival probabilities for observed, external and blended curves, with other relevant data.
1919
#' @importFrom stats pbeta
2020
#' @export
2121
#'
@@ -30,22 +30,25 @@
3030
#' data_sim <- ext_surv_sim(t_info = 144,
3131
#' S_info = 0.05,
3232
#' T_max = 180)
33-
#'
33+
#' # observed survival times
3434
#' obs_Surv <- fit.models(formula = Surv(death_t, death) ~ 1,
3535
#' data = dat_FCR,
3636
#' distr = "exponential",
3737
#' method = "hmc")
3838
#'
39+
#' # external survival times
3940
#' ext_Surv <- fit.models(formula = Surv(time, event) ~ 1,
4041
#' data = data_sim,
4142
#' distr = "exponential",
4243
#' method = "hmc")
4344
#'
45+
#' # blending parameter values
4446
#' blend_interv <- list(min = 48, max = 150)
4547
#' beta_params <- list(alpha = 3, beta = 3)
4648
#'
4749
#' ble_Surv <- blendsurv(obs_Surv, ext_Surv, blend_interv, beta_params)
4850
#'
51+
#' # all survival curves
4952
#' plot(ble_Surv)
5053
#'
5154
blendsurv <- function(obs_Surv, ext_Surv,

R/ext_surv_sim.R

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11

22
#' Create an external survival data based on expert opinion
33
#'
4-
#' Generally, the sampling is done is two steps
4+
#' @description
5+
#' Generates an individual patient-level survival dataset from aggregate survival
6+
#' probabilities that might be elicited from expert opinion.
7+
#'
8+
#' The function creates a synthetic dataset of event times for `n` patients,
9+
#' consistent with a piecewise-constant hazard rate implied by the expert-provided
10+
#' survival probabilities.
11+
#'
12+
#' @details
13+
#' The simulation uses a two-step sampling process based on partitioning the time
14+
#' horizon from `0` to `T_max`. First, the total number of patients `n` is
15+
#' allocated to different time intervals `[t_i, t_{i+1}]`. The probability of an
16+
#' event occurring in an interval is derived from the drop in the survival curve
17+
#' over that interval. This allocation is done via a multinomial distribution:
518
#' \deqn{
6-
#' p(T) = p(T | interval i) p(interval i)
19+
#' i \sim \text{Multinomial}(\pi)
720
#' }
21+
#' where $\pi_i = S(t_{i-1}) - S(t_i)$ is the probability of an event in interval `i`.
822
#'
9-
#' In particular
10-
#' \eqn{T ~ U(x_{i}, x_{i+1})}
11-
#' \eqn{i ~ multinomial(\hat{\pi})}
23+
#' Second, for the patients assigned to each interval, a specific event time is
24+
#' simulated from a uniform distribution covering that interval:
25+
#' \deqn{
26+
#' T | i \sim U(t_{i-1}, t_i)
27+
#' }
1228
#'
13-
#' @param t_info A vector of times for which expert opinion is elicited
14-
#' @param S_info A vector of mean survival probabilities estimated by experts
15-
#' corresponding to time points in `t_info`
16-
#' @param T_max The maximum survival time to be used
17-
#' @param n The number of patients to construct the artificial external data set; default 100
29+
#' @param t_info A numeric vector of time points for which expert opinion is elicited.
30+
#' @param S_info A numeric vector of mean survival probabilities estimated by experts
31+
#' corresponding to time points in `t_info`.
32+
#' @param T_max The maximum survival time for the simulation, at which the
33+
#' survival probability is assumed to be 0.
34+
#' @param n The total number of patients to construct the artificial external data set; default 100
1835
#' @importFrom stats runif rmultinom
19-
#' @return Dataframe of times and censoring status
36+
#'
37+
#' @return A data frame with `n` rows and two columns:
38+
#' \itemize{
39+
#' \item **time**: The simulated event time for each patient.
40+
#' \item **event**: The event indicator, which is always `1` as this function does not simulate censoring.
41+
#' }
42+
#'
2043
#' @export
2144
#'
2245
#' @examples
2346
#' dat <- ext_surv_sim(t_info = c(10,20,50),
2447
#' S_info = c(0.9, 0.8, 0.2),
2548
#' T_max = 100, n = 100)
2649
#' if (require(survival)) {
50+
#' # Kaplan-Meier curve
2751
#' km_fit <- survfit(Surv(time, event) ~ 1, data = dat)
2852
#' plot(km_fit)
2953
#' }

R/fit_inla_pw.R

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11

2-
#' Generate survival estimates with a piecewise exponential Cox model (using INLA)
2+
#' Fit a Piecewise Exponential Survival Model using INLA
33
#'
4-
#' @param inla.formula The formula for PEM which must be an `inla.surv` object
5-
#' @param data A dataframe for survival data with time (`death_t`) and
6-
#' event (`death`)
7-
#' @param cutpoints A sequence of cut points for intervals in the baseline hazard
8-
#' @param nsim The number of simulations from posteriors; default 100
9-
#' @param ... Additional arguments
4+
#' @description
5+
#' A convenience wrapper to fit a Bayesian piecewise exponential model (PEM) for
6+
#' survival data using the `INLA` package. This function simplifies the process by
7+
#' automatically handling the necessary data transformation from a standard
8+
#' survival format to the Poisson regression format that `INLA` requires.
9+
#'
10+
#' @details
11+
#' This function models the baseline hazard rate as a piecewise constant function.
12+
#' The `cutpoints` define the time intervals within which the hazard is assumed
13+
#' to be constant.
14+
#'
15+
#' Internally, the function first uses `INLA::inla.coxph` to convert the survival
16+
#' data into the long format suitable for a Poisson generalized linear model (GLM).
17+
#' It then fits this model using `INLA::inla`, applying a random walk of order 1
18+
#' (`rw1`) prior to the log-hazards for each interval. This prior provides a
19+
#' flexible and smoothed estimate of the baseline hazard over time.
20+
#'
21+
#' @param inla.formula A formula specifying the survival model. The response must
22+
#' be an `INLA::inla.surv` object (e.g., `inla.surv(time, event)`). The right-hand
23+
#' side specifies the linear predictor. The default fits a baseline-hazard-only model.
24+
#' @param data A data frame containing the time-to-event data, including the
25+
#' variables named in the `inla.formula`.
26+
#' @param cutpoints A numeric vector of cut points used to partition the time
27+
#' axis into intervals for the piecewise constant hazard.
28+
#' @param ... Additional arguments to be passed directly to the `INLA::inla`
29+
#' function (e.g., `control.predictor`, `control.family`).
30+
#'
31+
#' @return An object of class `inla`, which contains the full results of the
32+
#' fitted Bayesian model.
1033
#'
11-
#' @return INLA object
1234
#' @export
1335
#'
1436
#' @examples
1537
#' \donttest{
38+
#' # INLA may require configuration on your system.
39+
#' # See: https://www.r-inla.org/download-install
1640
#' if (requireNamespace("INLA", quietly = TRUE)) {
1741
#' data("TA174_FCR", package = "blendR")
1842
#' head(dat_FCR)
43+
#'
44+
#' # Fit a simple piecewise model with intervals every 5 time units
1945
#' obs_Surv <- fit_inla_pw(data = dat_FCR, cutpoints = seq(0, 180, by = 5))
46+
#'
47+
#' # summary(obs_Surv)
2048
#' }
2149
#' }
2250
#'

R/make_surv.R

Lines changed: 128 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,118 @@
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+
#' }
31108
make_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
148252
make_surv.default <- function(Surv,
149253
t = NULL,

R/plot.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11

2-
#' Blended survival curve based on short-term data and external information
2+
#' @title Plot Blended Survival Curves
3+
#'
4+
#' @description
5+
#' S3 method for plotting objects of class `blended`. This function generates a
6+
#' `ggplot2` visualization that displays the mean survival curves and 95%
7+
#' credible intervals for three components:
8+
#'
9+
#' 1. The curve fitted to the original trial data (`Data fitting`).
10+
#' 2. The curve representing external information (`External info`).
11+
#' 3. The final, combined blended curve (`Blended curve`).
12+
#'
13+
#' @param x An object of class `blended`, typically the output of [blendsurv()].
14+
#' @param alpha A numeric vector of length two specifying the opacity for the
15+
#' credible interval ribbons. The first value (`alpha[1]`) is for the blended
16+
#' curve, and the second (`alpha[2]`) is for the trial and external curves.
17+
#' @param ... Additional graphical arguments passed to the plot, such as `xlim`,
18+
#' `xlab`, or `ylab`.
319
#'
4-
#' @param x A blended survival curve object obtain from [blendsurv()]
5-
#' @param alpha A vector specifying the opacity of ribbon for the blended curve and other curves
6-
#' @param ... Additional arguments
720
#' @import ggplot2
821
#' @importFrom stats quantile
922
#'
10-
#' @return A \pkg{ggplot2} object
11-
#' @seealso [blendsurv()]
23+
#' @return A `ggplot` object representing the survival curves.
24+
#' @seealso [blendsurv()], [weightplot()]
1225
#' @method plot blended
1326
#' @export
1427
#'

0 commit comments

Comments
 (0)