|
| 1 | +#' General Interface for Parametric Survival Models |
| 2 | +#' |
| 3 | +#' `survival_reg()` is a way to generate a _specification_ of a model |
| 4 | +#' before fitting and allows the model to be created using |
| 5 | +#' R. The main argument for the |
| 6 | +#' model is: |
| 7 | +#' \itemize{ |
| 8 | +#' \item \code{dist}: The probability distribution of the outcome. |
| 9 | +#' } |
| 10 | +#' This argument is converted to its specific names at the |
| 11 | +#' time that the model is fit. Other options and argument can be |
| 12 | +#' set using `set_engine()`. If left to its default |
| 13 | +#' here (`NULL`), the value is taken from the underlying model |
| 14 | +#' functions. |
| 15 | +#' |
| 16 | +#' @inheritParams boost_tree |
| 17 | +#' @param mode A single character string for the type of model. |
| 18 | +#' The only possible value for this model is "censored regression". |
| 19 | +#' @param dist A character string for the outcome distribution. "weibull" is |
| 20 | +#' the default. |
| 21 | +#' @details |
| 22 | +#' The data given to the function are not saved and are only used |
| 23 | +#' to determine the _mode_ of the model. For `survival_reg()`,the |
| 24 | +#' mode will always be "censored regression". |
| 25 | +#' |
| 26 | +#' Since survival models typically involve censoring (and require the use of |
| 27 | +#' [survival::Surv()] objects), the [fit()] function will require that the |
| 28 | +#' survival model be specified via the formula interface. |
| 29 | +#' |
| 30 | +#' @seealso [fit()], [survival::Surv()] |
| 31 | +#' @examples |
| 32 | +#' survival_reg() |
| 33 | +#' # Parameters can be represented by a placeholder: |
| 34 | +#' survival_reg(dist = varying()) |
| 35 | +#' |
| 36 | +#' @export |
| 37 | +survival_reg <- function(mode = "censored regression", dist = NULL) { |
| 38 | + |
| 39 | + args <- list( |
| 40 | + dist = enquo(dist) |
| 41 | + ) |
| 42 | + |
| 43 | + new_model_spec( |
| 44 | + "survival_reg", |
| 45 | + args = args, |
| 46 | + eng_args = NULL, |
| 47 | + mode = mode, |
| 48 | + method = NULL, |
| 49 | + engine = NULL |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +#' @export |
| 54 | +print.survival_reg <- function(x, ...) { |
| 55 | + cat("Parametric Survival Regression Model Specification (", x$mode, ")\n\n", sep = "") |
| 56 | + model_printer(x, ...) |
| 57 | + |
| 58 | + if (!is.null(x$method$fit$args)) { |
| 59 | + cat("Model fit template:\n") |
| 60 | + print(show_call(x)) |
| 61 | + } |
| 62 | + |
| 63 | + invisible(x) |
| 64 | +} |
| 65 | + |
| 66 | +# ------------------------------------------------------------------------------ |
| 67 | + |
| 68 | +#' Update a Parametric Survival Regression Specification |
| 69 | +#' |
| 70 | +#' If parameters need to be modified, this function can be used |
| 71 | +#' in lieu of recreating the object from scratch. |
| 72 | +#' |
| 73 | +#' @inheritParams update.boost_tree |
| 74 | +#' @param object A survival regression model specification. |
| 75 | +#' @examples |
| 76 | +#' model <- survival_reg(dist = "weibull") |
| 77 | +#' model |
| 78 | +#' update(model, dist = "lnorm") |
| 79 | +#' @method update survival_reg |
| 80 | +#' @rdname survival_reg |
| 81 | +#' @export |
| 82 | +update.survival_reg <- function(object, parameters = NULL, dist = NULL, fresh = FALSE, ...) { |
| 83 | + |
| 84 | + eng_args <- update_engine_parameters(object$eng_args, ...) |
| 85 | + |
| 86 | + if (!is.null(parameters)) { |
| 87 | + parameters <- check_final_param(parameters) |
| 88 | + } |
| 89 | + |
| 90 | + args <- list( |
| 91 | + dist = enquo(dist) |
| 92 | + ) |
| 93 | + |
| 94 | + args <- update_main_parameters(args, parameters) |
| 95 | + |
| 96 | + if (fresh) { |
| 97 | + object$args <- args |
| 98 | + object$eng_args <- eng_args |
| 99 | + } else { |
| 100 | + null_args <- map_lgl(args, null_value) |
| 101 | + if (any(null_args)) |
| 102 | + args <- args[!null_args] |
| 103 | + if (length(args) > 0) |
| 104 | + object$args[names(args)] <- args |
| 105 | + if (length(eng_args) > 0) |
| 106 | + object$eng_args[names(eng_args)] <- eng_args |
| 107 | + } |
| 108 | + |
| 109 | + new_model_spec( |
| 110 | + "survival_reg", |
| 111 | + args = object$args, |
| 112 | + eng_args = object$eng_args, |
| 113 | + mode = object$mode, |
| 114 | + method = NULL, |
| 115 | + engine = object$engine |
| 116 | + ) |
| 117 | +} |
0 commit comments