Skip to content

Commit 22510a2

Browse files
committed
model spec for survival_reg
1 parent 110257f commit 22510a2

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

R/survival_reg.R

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
#' Also, for the `flexsurv::flexsurvfit` engine, the typical
31+
#' `strata` function cannot be used. To achieve the same effect,
32+
#' the extra parameter roles can be used (as described above).
33+
#'
34+
#' The model can be created using the `fit()` function using the
35+
#' following _engines_:
36+
#' \itemize{
37+
#' \item \pkg{R}: `"flexsurv"`, `"survival"` (the default)
38+
#' }
39+
#'
40+
#' @seealso [fit()], [survival::Surv()]
41+
#' @references Jackson, C. (2016). `flexsurv`: A Platform for Parametric Survival
42+
#' Modeling in R. _Journal of Statistical Software_, 70(8), 1 - 33.
43+
#' @examples
44+
#' survival_reg()
45+
#' # Parameters can be represented by a placeholder:
46+
#' survival_reg(dist = varying())
47+
#'
48+
#' @export
49+
survival_reg <- function(mode = "censored regression", dist = NULL) {
50+
51+
args <- list(
52+
dist = enquo(dist)
53+
)
54+
55+
new_model_spec(
56+
"survival_reg",
57+
args = args,
58+
eng_args = NULL,
59+
mode = mode,
60+
method = NULL,
61+
engine = NULL
62+
)
63+
}
64+
65+
#' @export
66+
print.survival_reg <- function(x, ...) {
67+
cat("Parametric Survival Regression Model Specification (", x$mode, ")\n\n", sep = "")
68+
model_printer(x, ...)
69+
70+
if (!is.null(x$method$fit$args)) {
71+
cat("Model fit template:\n")
72+
print(show_call(x))
73+
}
74+
75+
invisible(x)
76+
}
77+
78+
# ------------------------------------------------------------------------------
79+
80+
#' Update a Parametric Survival Regression Specification
81+
#'
82+
#' If parameters need to be modified, this function can be used
83+
#' in lieu of recreating the object from scratch.
84+
#'
85+
#' @inheritParams update.boost_tree
86+
#' @param object A survival regression model specification.
87+
#' @examples
88+
#' model <- survival_reg(dist = "weibull")
89+
#' model
90+
#' update(model, dist = "lnorm")
91+
#' @method update survival_reg
92+
#' @rdname survival_reg
93+
#' @export
94+
update.survival_reg <- function(object, parameters = NULL, dist = NULL, fresh = FALSE, ...) {
95+
96+
eng_args <- update_engine_parameters(object$eng_args, ...)
97+
98+
if (!is.null(parameters)) {
99+
parameters <- check_final_param(parameters)
100+
}
101+
102+
args <- list(
103+
dist = enquo(dist)
104+
)
105+
106+
args <- update_main_parameters(args, parameters)
107+
108+
if (fresh) {
109+
object$args <- args
110+
object$eng_args <- eng_args
111+
} else {
112+
null_args <- map_lgl(args, null_value)
113+
if (any(null_args))
114+
args <- args[!null_args]
115+
if (length(args) > 0)
116+
object$args[names(args)] <- args
117+
if (length(eng_args) > 0)
118+
object$eng_args[names(eng_args)] <- eng_args
119+
}
120+
121+
new_model_spec(
122+
"survival_reg",
123+
args = object$args,
124+
eng_args = object$eng_args,
125+
mode = object$mode,
126+
method = NULL,
127+
engine = object$engine
128+
)
129+
}

R/survival_reg_data.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
set_new_model("survival_reg")
3+
set_model_mode("survival_reg", "censored regression")
4+
5+
# ------------------------------------------------------------------------------
6+
7+
# parnip just contains the model specification, the engines are the censored package.

0 commit comments

Comments
 (0)