Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for flexsurvspline engine for survival_reg model spec #831

Merged
merged 13 commits into from
Nov 3, 2022
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# parsnip (development version)

* Adds support for `flexsurvspline` engine for the `survival_reg()` model specification from the `censored` package (#831)

* The matrix interface for fitting `fit_xy()` now works for the `"censored regression"` mode (#829).

# parsnip 1.0.2
Expand Down
11 changes: 11 additions & 0 deletions R/surv_reg_flexsurvspline.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' Flexible parametric survival regression
#'
#' [flexsurv::flexsurvspline()] fits a flexible parametric survival model.
#'
#' @includeRmd man/rmd/surv_reg_flexsurvspline.md details
#'
#' @name details_surv_reg_flexsurvspline
#' @keywords internal
NULL

# See inst/README-DOCS.md for a description of how these files are processed
11 changes: 11 additions & 0 deletions R/survival_reg_flexsurvspline.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' Flexible parametric survival regression
#'
#' [flexsurv::flexsurvspline()] fits a flexible parametric survival model.
#'
#' @includeRmd man/rmd/survival_reg_flexsurvspline.md details
#'
#' @name details_survival_reg_flexsurvspline
#' @keywords internal
NULL

# See inst/README-DOCS.md for a description of how these files are processed
22 changes: 22 additions & 0 deletions R/tunable.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ brulee_multinomial_engine_args <-
brulee_mlp_engine_args %>%
dplyr::filter(name %in% c("momentum", "batch_size", "stop_iter", "class_weights"))

flexsurvspline_engine_args <-
tibble::tibble(
name = c("num_knots"),
call_info = list(
list(pkg = "dials", fun = "num_knots")
),
source = "model_spec",
component = "survival_reg",
component_id = "engine"
)

# ------------------------------------------------------------------------------

# Lazily registered in .onLoad()
Expand Down Expand Up @@ -310,5 +321,16 @@ tunable_mlp <- function(x, ...) {
res
}

# Lazily registered in .onLoad()
tunable_survival_reg <- function(x, ...) {
res <- NextMethod()
if (x$engine == "flexsurvspline") {
res <- add_engine_parameters(res, flexsurvspline_engine_args)
}
res
}

tunable_surv_reg <- tunable_survival_reg

# nocov end

55 changes: 55 additions & 0 deletions man/rmd/surv_reg_flexsurvspline.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```{r, child = "aaa.Rmd", include = FALSE}
```

`r descr_models("surv_reg", "flexsurvspline")`

## Tuning Parameters

```{r flexsurvspline-param-info, echo = FALSE}
defaults <-
tibble::tibble(parsnip = c("num_knots"), default = 1L)

param <-
surv_reg() %>%
set_engine("flexsurvspline") %>%
set_mode("censored regression") %>%
make_parameter_list(defaults)
```

This model has `r nrow(param)` tuning parameters:

```{r flexsurvspline-param-list, echo = FALSE, results = "asis"}
param$item
```

## Translation from parsnip to the original package

`r uses_extension("surv_reg", "flexsurvspline", "censored regression")`

```{r flexsurvspline-creg}
library(censored)

surv_reg() %>%
set_engine("flexsurvspline", num_knots = 1L) %>%
set_mode("censored regression") %>%
translate()
```

## Other details

The main interface for this model uses the formula method since the model specification typically involved the use of [survival::Surv()].

For this engine, stratification cannot be specified via [`strata()`], please see [flexsurv::flexsurvspline()] for alternative specifications.

```{r child = "template-survival-mean.Rmd"}
```

## Saving fitted model objects

```{r child = "template-butcher.Rmd"}
```


## References

- Jackson, C. 2016. `flexsurv`: A Platform for Parametric Survival Modeling in R. _Journal of Statistical Software_, 70(8), 1 - 33.
55 changes: 55 additions & 0 deletions man/rmd/survival_reg_flexsurvspline.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```{r, child = "aaa.Rmd", include = FALSE}
```

`r descr_models("survival_reg", "flexsurvspline")`

## Tuning Parameters

```{r flexsurvspline-param-info, echo = FALSE}
defaults <-
tibble::tibble(parsnip = c("num_knots"), default = 1L)

param <-
survival_reg() %>%
set_engine("flexsurvspline") %>%
set_mode("censored regression") %>%
make_parameter_list(defaults)
```

This model has `r nrow(param)` tuning parameters:

```{r flexsurvspline-param-list, echo = FALSE, results = "asis"}
param$item
```

## Translation from parsnip to the original package

`r uses_extension("survival_reg", "flexsurvspline", "censored regression")`

```{r flexsurvspline-creg}
library(censored)

survival_reg() %>%
set_engine("flexsurvspline", num_knots = 1L) %>%
set_mode("censored regression") %>%
translate()
```

## Other details

The main interface for this model uses the formula method since the model specification typically involved the use of [survival::Surv()].

For this engine, stratification cannot be specified via [`strata()`], please see [flexsurv::flexsurvspline()] for alternative specifications.

```{r child = "template-survival-mean.Rmd"}
hfrick marked this conversation as resolved.
Show resolved Hide resolved
```

## Saving fitted model objects

```{r child = "template-butcher.Rmd"}
```


## References

- Jackson, C. 2016. `flexsurv`: A Platform for Parametric Survival Modeling in R. _Journal of Statistical Software_, 70(8), 1 - 33.