|
25 | 25 | #' `strata` function cannot be used. To achieve the same effect, |
26 | 26 | #' the extra parameter roles can be used (as described above). |
27 | 27 | #' |
28 | | -#' The model can be created using the `fit()` function using the |
29 | | -#' following _engines_: |
30 | | -#' \itemize{ |
31 | | -#' \item \pkg{R}: `"flexsurv"` |
32 | | -#' } |
33 | 28 | #' @inheritParams boost_tree |
34 | 29 | #' @param mode A single character string for the type of model. |
35 | 30 | #' The only possible value for this model is "regression". |
36 | 31 | #' @param dist A character string for the outcome distribution. "weibull" is |
37 | 32 | #' the default. |
| 33 | +#' @details |
| 34 | +#' For `surv_reg`, the mode will always be "regression". |
| 35 | +#' |
| 36 | +#' The model can be created using the `fit()` function using the |
| 37 | +#' following _engines_: |
| 38 | +#' \itemize{ |
| 39 | +#' \item \pkg{R}: `"flexsurv"`, `"survreg"` |
| 40 | +#' } |
| 41 | +#' |
| 42 | +#' @section Engine Details: |
| 43 | +#' |
| 44 | +#' Engines may have pre-set default arguments when executing the |
| 45 | +#' model fit call. These can be changed by using the `...` |
| 46 | +#' argument to pass in the preferred values. For this type of |
| 47 | +#' model, the template of the fit calls are: |
| 48 | +#' |
| 49 | +#' \pkg{flexsurv} |
| 50 | +#' |
| 51 | +#' \Sexpr[results=rd]{parsnip:::show_fit(parsnip:::surv_reg(), "flexsurv")} |
| 52 | +#' |
| 53 | +#' \pkg{survreg} |
| 54 | +#' |
| 55 | +#' \Sexpr[results=rd]{parsnip:::show_fit(parsnip:::surv_reg(), "survreg")} |
| 56 | +#' |
| 57 | +#' Note that `model = TRUE` is needed to produce quantile |
| 58 | +#' predictions when there is a stratification variable and can be |
| 59 | +#' overridden in other cases. |
| 60 | +#' |
38 | 61 | #' @seealso [varying()], [fit()], [survival::Surv()] |
39 | 62 | #' @references Jackson, C. (2016). `flexsurv`: A Platform for Parametric Survival |
40 | 63 | #' Modeling in R. _Journal of Statistical Software_, 70(8), 1 - 33. |
@@ -160,3 +183,51 @@ check_args.surv_reg <- function(object) { |
160 | 183 |
|
161 | 184 | invisible(object) |
162 | 185 | } |
| 186 | + |
| 187 | +# ------------------------------------------------------------------------------ |
| 188 | + |
| 189 | +#' @importFrom stats setNames |
| 190 | +#' @importFrom dplyr mutate |
| 191 | +survreg_quant <- function(results, object) { |
| 192 | + pctl <- object$spec$method$quantile$args$p |
| 193 | + n <- nrow(results) |
| 194 | + p <- ncol(results) |
| 195 | + results <- |
| 196 | + results %>% |
| 197 | + as_tibble() %>% |
| 198 | + setNames(names0(p)) %>% |
| 199 | + mutate(.row = 1:n) %>% |
| 200 | + gather(.label, .pred, -.row) %>% |
| 201 | + arrange(.row, .label) %>% |
| 202 | + mutate(.quantile = rep(pctl, n)) %>% |
| 203 | + dplyr::select(-.label) |
| 204 | + .row <- results[[".row"]] |
| 205 | + results <- |
| 206 | + results %>% |
| 207 | + dplyr::select(-.row) |
| 208 | + results <- split(results, .row) |
| 209 | + names(results) <- NULL |
| 210 | + tibble(.pred = results) |
| 211 | +} |
| 212 | + |
| 213 | +# ------------------------------------------------------------------------------ |
| 214 | + |
| 215 | +#' @importFrom dplyr bind_rows |
| 216 | +flexsurv_mean <- function(results, object) { |
| 217 | + results <- unclass(results) |
| 218 | + results <- bind_rows(results) |
| 219 | + results$est |
| 220 | +} |
| 221 | + |
| 222 | +#' @importFrom stats setNames |
| 223 | +flexsurv_quant <- function(results, object) { |
| 224 | + results <- map(results, as_tibble) |
| 225 | + names(results) <- NULL |
| 226 | + results <- map(results, setNames, c(".quantile", ".pred", ".pred_lower", ".pred_upper")) |
| 227 | +} |
| 228 | + |
| 229 | +# ------------------------------------------------------------------------------ |
| 230 | + |
| 231 | +#' @importFrom utils globalVariables |
| 232 | +utils::globalVariables(".label") |
| 233 | + |
0 commit comments