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

Fix output format #284

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(survival_prob_coxph)
export(survival_prob_mboost)
export(survival_prob_orsf)
export(survival_prob_partykit)
export(survival_prob_pecRpart)
export(survival_prob_survbagg)
export(survival_prob_survreg)
export(survival_time_coxnet)
Expand Down
15 changes: 0 additions & 15 deletions R/aaa_survival_prob.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,6 @@ predict_survival_na <- function(eval_time, interval = "none", penalty = NULL) {
ret
}

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

# This function takes a matrix and turns it into list of nested tibbles
# suitable for predict_survival
matrix_to_nested_tibbles_survival <- function(x, eval_time) {
res <- tibble(
.row = rep(seq_len(nrow(x)), each = ncol(x)),
.eval_time = rep(eval_time, nrow(x)),
.pred_survival = as.numeric(t(x))
)

dplyr::group_nest(res, .row, .key = ".pred")$.pred
}


# summary_survfit helpers -------------------------------------------------

survfit_summary_typestable <- function(object) {
Expand Down
5 changes: 1 addition & 4 deletions R/boost_tree-mboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ survival_prob_mboost <- function(object, new_data, eval_time, time = deprecated(
survival_prob = survival_curve$surv
)

# survival_prob is length(eval_time) x nrow(new_data) and
# `matrix_to_nested_tibbles_survival()` expects the transpose of that (and
# then does another t() inside).
# this version doesn't need to transpose the matrix at all
# survival_prob is length(eval_time) x nrow(new_data)
n_obs <- ncol(survival_prob)
ret <- tibble::tibble(
.row = rep(seq_len(n_obs), each = length(eval_time)),
Expand Down
16 changes: 5 additions & 11 deletions R/decision_tree-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,13 @@ make_decision_tree_rpart <- function() {
type = "survival",
value = list(
pre = NULL,
post = function(x, object) {
eval_time <- object$spec$method$pred$survival$args$eval_time
if (!is.matrix(x)) {
x <- matrix(x, nrow = 1)
}
matrix_to_nested_tibbles_survival(x, eval_time)
},
func = c(pkg = "pec", fun = "predictSurvProb"),
post = NULL,
func = c(pkg = "censored", fun = "survival_prob_pecRpart"),
args =
list(
object = quote(object$fit),
newdata = quote(new_data),
times = rlang::expr(eval_time)
object = rlang::expr(object),
new_data = rlang::expr(new_data),
eval_time = rlang::expr(eval_time)
)
)
)
Expand Down
33 changes: 33 additions & 0 deletions R/decision_tree-rpart.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' A wrapper for survival probabilities with pecRpart models
#' @param object A fitted `_pecRpart` object.
#' @param new_data Data for prediction.
#' @param eval_time A vector of integers for prediction times.
#' @return A tibble with a list column of nested tibbles.
#' @keywords internal
#' @export
#' @examples
#' mod <- decision_tree() %>%
#' set_mode("censored regression") %>%
#' set_engine("rpart") %>%
#' fit(Surv(time, status) ~ ., data = lung)
#' survival_prob_pecRpart(mod, new_data = lung[1:3, ], eval_time = 300)
survival_prob_pecRpart <- function(object, new_data, eval_time) {
n_obs <- nrow(new_data)
n_eval_time <- length(eval_time)

pred <- pec::predictSurvProb(object$fit, newdata = new_data, times = eval_time)

if (n_obs < 2) {
pred <- matrix(pred, nrow = 1)
}

res <- data.frame(
.row = rep(seq_len(n_obs), times = n_eval_time),
.eval_time = rep(eval_time, each = n_obs),
.pred_survival = as.numeric(pred)
) %>%
tidyr::nest(.pred = c(-.row)) %>%
dplyr::select(-.row)

res
}
16 changes: 12 additions & 4 deletions R/rand_forest-aorsf.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ survival_prob_orsf <- function(object, new_data, eval_time, time = deprecated())
# argument `eval_time` to the prediction call and `aorsf::predict.orsf_fit()`
# expects empty dots, i.e. no `eval_time` argument.

res <- predict(
pred <- predict(
object,
new_data = new_data,
pred_horizon = eval_time,
Expand All @@ -34,8 +34,16 @@ survival_prob_orsf <- function(object, new_data, eval_time, time = deprecated())
boundary_checks = FALSE
)

res <- matrix_to_nested_tibbles_survival(res, eval_time)
n_obs <- nrow(new_data)
n_eval_time <- length(eval_time)

# return a tibble
tibble(.pred = res)
res <- data.frame(
.row = rep(seq_len(n_obs), times = n_eval_time),
.eval_time = rep(eval_time, each = n_obs),
.pred_survival = as.numeric(pred)
) %>%
tidyr::nest(.pred = c(-.row)) %>%
dplyr::select(-.row)

res
}
29 changes: 29 additions & 0 deletions man/survival_prob_pecRpart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading