Skip to contents

This function checks whether flexsurvspline models converge, by fitting the models via fit.models in the form survival::Surv(time,event) ~ 1 for each strata, and for those models without warnings/errors, it returns fit information and plots.

Usage

quick_fit_splines(
  data,
  time,
  event,
  strata,
  dists,
  times = NULL,
  strata_labels = NULL,
  formula = NULL,
  weights = NULL,
  xlab = "Time",
  font.family = "Roboto Condensed",
  plot.theme = theme_easysurv(),
  add_interactive_plots = FALSE,
  plot_predictions = "flexsurv",
  include_ci = TRUE,
  get_mean = FALSE
)

Arguments

data

A tibble or data frame containing the survival data with columns for time, event and strata.

time

The name of the time variable in data

event

The name of the event variable in data

strata

The name of the strata variable in data

dists

A data frame with columns knots and scale specifying the number of knots to be used and the type of spline model to be executed respectively. The possible models are: Proportional hazards model (scale = "hazard"), probit model (scale = "normal"), proportional odds model (scale = "odds").

times

Optional. A set of times at which to calculate predicted survival. The default is seq(from = 0, to = ceiling(max(data$time)*2.5), length.out = 200)

strata_labels

Optional. A character vector containing the names of the stratas (default is NULL). Provide in a consistent order with levels(as.factor(data$strata)).

formula

Optional. Surv() formula. Default is survival::Surv(time,event) ~ 1

weights

Optional for case weights. The function expects a string corresponding to a variable name within the data.

xlab

The x-axis label for plots. Default is "Time".

font.family

The name of the font for the plots. Default is "Roboto Condensed".

plot.theme

ggplot2 theme for the plots. Default is theme_easysurv().

add_interactive_plots

Optional. Whether to include plotly outputs for hazard and fit plots. Default is FALSE.

plot_predictions

Optional. Whether to plot the predictions using the flexsurv package or survHE package. Default is "flexsurv".

include_ci

Logical indicating whether to include confidence intervals in the survival probability predictions. Default is TRUE.

get_mean

Optional. Whether to attempt to calculate mean survival times for the fit_averages object. Defaults to FALSE.

Value

An object of class quick_fit_spline containing the following elements:

converged

A list of the distributions that converged

fits

The survHE::fit.models output

hazard_plots

Smoothed hazard plots for each strata

hazard_plotly

Interactive hazard plots (if add_interactive_plots = TRUE)

fit_plots

Plot of extrapolations over the specified times parameter

fit_plotly

Interactive fit plots (if add_interactive_plots = TRUE)

goodness_of_fit

AIC and BIC output, alongside ranks, for each distribution

surv_params

The flexsurv parameters for each model and their vcov matrix

fit_averages

The median, mean and restricted mean survival times for each distribution

predicted_fits

Predicted survival proportions over the times parameter, if provided

Examples

if (FALSE) {
# Load required packages
library(survival)

input_data <- survival::lung

surv_data <- tibble(
  time = input_data$time,
  event = input_data$status - 1,
  strata = as.factor(input_data$sex)
)

# Define distributions
spline_dists <- tibble(
  "knots" = c(1, 2, 3),
  "scale" = c("odds", "hazard", "normal")
)

fit_check_splines <- quick_fit_splines(
  data = surv_data,
  time = "time",
  event = "event",
  strata = "strata",
  dists = spline_dists
)
}