-
Notifications
You must be signed in to change notification settings - Fork 92
WIP: adding new prediction types for Survnip #359
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2fbc7b0
setup predict_time.model_fit
EmilHvitfeldt 63e96b2
add format_time
EmilHvitfeldt a8231f8
add predict_survival
EmilHvitfeldt 35ce9b2
"risk prediction" to "censored regression"
EmilHvitfeldt 075183c
allow any length of .time in predict_survival
EmilHvitfeldt 634a419
add predict_linear_pred()
EmilHvitfeldt ebe0a5e
add censored regression mode
EmilHvitfeldt 2b6f06f
dont register surv_reg
EmilHvitfeldt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,7 @@ predict.model_fit <- function(object, new_data, type = NULL, opts = list(), ...) | |
check_installs(object$spec) | ||
load_libs(object$spec, quiet = TRUE) | ||
|
||
other_args <- c("level", "std_error", "quantile") # "time" for survival probs later | ||
other_args <- c("level", "std_error", "quantile", ".time") | ||
is_pred_arg <- names(the_dots) %in% other_args | ||
if (any(!is_pred_arg)) { | ||
bad_args <- names(the_dots)[!is_pred_arg] | ||
|
@@ -138,21 +138,27 @@ predict.model_fit <- function(object, new_data, type = NULL, opts = list(), ...) | |
rlang::warn("`opts` is only used with `type = 'raw'` and was ignored.") | ||
res <- switch( | ||
type, | ||
numeric = predict_numeric(object = object, new_data = new_data, ...), | ||
class = predict_class(object = object, new_data = new_data, ...), | ||
prob = predict_classprob(object = object, new_data = new_data, ...), | ||
conf_int = predict_confint(object = object, new_data = new_data, ...), | ||
pred_int = predict_predint(object = object, new_data = new_data, ...), | ||
quantile = predict_quantile(object = object, new_data = new_data, ...), | ||
raw = predict_raw(object = object, new_data = new_data, opts = opts, ...), | ||
numeric = predict_numeric(object = object, new_data = new_data, ...), | ||
class = predict_class(object = object, new_data = new_data, ...), | ||
prob = predict_classprob(object = object, new_data = new_data, ...), | ||
conf_int = predict_confint(object = object, new_data = new_data, ...), | ||
pred_int = predict_predint(object = object, new_data = new_data, ...), | ||
quantile = predict_quantile(object = object, new_data = new_data, ...), | ||
time = predict_time(object = object, new_data = new_data, ...), | ||
survival = predict_survival(object = object, new_data = new_data, ...), | ||
linear_pred = predict_linear_pred(object = object, new_data = new_data, ...), | ||
raw = predict_raw(object = object, new_data = new_data, opts = opts, ...), | ||
rlang::abort(glue::glue("I don't know about type = '{type}'")) | ||
) | ||
if (!inherits(res, "tbl_spark")) { | ||
res <- switch( | ||
type, | ||
numeric = format_num(res), | ||
class = format_class(res), | ||
prob = format_classprobs(res), | ||
numeric = format_num(res), | ||
class = format_class(res), | ||
prob = format_classprobs(res), | ||
time = format_time(res), | ||
survival = format_survival(res), | ||
linear_pred = format_linear_pred(res), | ||
res | ||
) | ||
} | ||
|
@@ -166,6 +172,7 @@ check_pred_type <- function(object, type) { | |
switch(object$spec$mode, | ||
regression = "numeric", | ||
classification = "class", | ||
"censored regression" = "time", | ||
rlang::abort("`type` should be 'regression' or 'classification'.")) | ||
} | ||
if (!(type %in% pred_types)) | ||
|
@@ -216,6 +223,54 @@ format_classprobs <- function(x) { | |
x | ||
} | ||
|
||
format_time <- function(x) { | ||
if (inherits(x, "tbl_spark")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove the spark stuff from the survival bits |
||
return(x) | ||
|
||
if (isTRUE(ncol(x) > 1) | is.data.frame(x)) { | ||
x <- as_tibble(x, .name_repair = "minimal") | ||
if (!any(grepl("^\\.time", names(x)))) { | ||
names(x) <- paste0(".time_", names(x)) | ||
} | ||
} else { | ||
x <- tibble(.pred_time = unname(x)) | ||
} | ||
|
||
x | ||
} | ||
|
||
format_survival <- function(x) { | ||
if (inherits(x, "tbl_spark")) | ||
return(x) | ||
|
||
if (isTRUE(ncol(x) > 1) | is.data.frame(x)) { | ||
x <- as_tibble(x, .name_repair = "minimal") | ||
if (!any(grepl("^\\.time", names(x)))) { | ||
names(x) <- paste0(".time_", names(x)) | ||
} | ||
} else { | ||
x <- tibble(.pred_survival = unname(x)) | ||
} | ||
|
||
x | ||
} | ||
|
||
format_linear_pred <- function(x) { | ||
if (inherits(x, "tbl_spark")) | ||
return(x) | ||
|
||
if (isTRUE(ncol(x) > 1) | is.data.frame(x)) { | ||
x <- as_tibble(x, .name_repair = "minimal") | ||
if (!any(grepl("^\\.time", names(x)))) { | ||
names(x) <- paste0(".time_", names(x)) | ||
} | ||
} else { | ||
x <- tibble(.pred_linear_pred = unname(x)) | ||
} | ||
|
||
x | ||
} | ||
|
||
make_pred_call <- function(x) { | ||
if ("pkg" %in% names(x$func)) | ||
cl <- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' @keywords internal | ||
#' @rdname other_predict | ||
#' @inheritParams predict.model_fit | ||
#' @method predict_linear_pred model_fit | ||
#' @export predict_linear_pred.model_fit | ||
#' @export | ||
predict_linear_pred.model_fit <- function(object, new_data, ...) { | ||
if (object$spec$mode != "censored regression") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd remove this check. We can do these types of predictions for all types of models. |
||
rlang::abort(glue::glue("`predict_linear_pred()` is for predicting linear predictors. ", | ||
"Use `predict_class()` or `predict_classprob()` for ", | ||
"classification models.")) | ||
|
||
if (!any(names(object$spec$method$pred) == "linear_pred")) | ||
rlang::abort("No prediction module defined for this model.") | ||
|
||
if (inherits(object$fit, "try-error")) { | ||
rlang::warn("Model fit failed; cannot make predictions.") | ||
return(NULL) | ||
} | ||
|
||
new_data <- prepare_data(object, new_data) | ||
|
||
# preprocess data | ||
if (!is.null(object$spec$method$pred$linear_pred$pre)) | ||
new_data <- object$spec$method$pred$linear_pred$pre(new_data, object) | ||
|
||
# create prediction call | ||
pred_call <- make_pred_call(object$spec$method$pred$linear_pred) | ||
|
||
res <- eval_tidy(pred_call) | ||
# post-process the predictions | ||
|
||
if (!is.null(object$spec$method$pred$linear_pred$post)) { | ||
res <- object$spec$method$pred$linear_pred$post(res, object) | ||
} | ||
|
||
if (is.vector(res)) { | ||
res <- unname(res) | ||
} else { | ||
if (!inherits(res, "tbl_spark")) | ||
res <- as.data.frame(res) | ||
} | ||
res | ||
} | ||
|
||
|
||
#' @export | ||
#' @keywords internal | ||
#' @rdname other_predict | ||
#' @inheritParams predict_linear_pred.model_fit | ||
predict_linear_pred <- function(object, ...) | ||
UseMethod("predict_linear_pred") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' @keywords internal | ||
#' @rdname other_predict | ||
#' @inheritParams predict.model_fit | ||
#' @method predict_survival model_fit | ||
#' @export predict_survival.model_fit | ||
#' @export | ||
predict_survival.model_fit <- | ||
function(object, new_data, .time, ...) { | ||
if (object$spec$mode != "censored regression") | ||
rlang::abort(glue::glue( | ||
"`predict_survival()` is for predicting survival probabilities. ", | ||
"Use `predict_class()` or `predict_classprob()` for ", | ||
"classification models." | ||
)) | ||
|
||
if (is.null(object$spec$method$pred$survival)) | ||
rlang::abort("No survival prediction method defined for this engine.") | ||
|
||
if (inherits(object$fit, "try-error")) { | ||
rlang::warn("Model fit failed; cannot make predictions.") | ||
return(NULL) | ||
} | ||
|
||
new_data <- prepare_data(object, new_data) | ||
|
||
# preprocess data | ||
if (!is.null(object$spec$method$pred$survival$pre)) | ||
new_data <- object$spec$method$pred$survival$pre(new_data, object) | ||
|
||
# Pass some extra arguments to be used in post-processor | ||
object$spec$method$pred$survival$args$.time <- .time | ||
pred_call <- make_pred_call(object$spec$method$pred$survival) | ||
|
||
res <- eval_tidy(pred_call) | ||
|
||
# post-process the predictions | ||
if(!is.null(object$spec$method$pred$survival$post)) { | ||
res <- object$spec$method$pred$survival$post(res, object) | ||
} | ||
|
||
res | ||
} | ||
|
||
# @export | ||
# @keywords internal | ||
# @rdname other_predict | ||
# @inheritParams predict.model_fit | ||
predict_survival <- function (object, ...) | ||
UseMethod("predict_survival") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' @keywords internal | ||
#' @rdname other_predict | ||
#' @inheritParams predict.model_fit | ||
#' @method predict_time model_fit | ||
#' @export predict_time.model_fit | ||
#' @export | ||
predict_time.model_fit <- function(object, new_data, ...) { | ||
if (object$spec$mode != "censored regression") | ||
rlang::abort(glue::glue("`predict_time()` is for predicting time outcomes. ", | ||
"Use `predict_class()` or `predict_classprob()` for ", | ||
"classification models.")) | ||
|
||
if (!any(names(object$spec$method$pred) == "time")) | ||
rlang::abort("No prediction module defined for this model.") | ||
|
||
if (inherits(object$fit, "try-error")) { | ||
rlang::warn("Model fit failed; cannot make predictions.") | ||
return(NULL) | ||
} | ||
|
||
new_data <- prepare_data(object, new_data) | ||
|
||
# preprocess data | ||
if (!is.null(object$spec$method$pred$time$pre)) | ||
new_data <- object$spec$method$pred$time$pre(new_data, object) | ||
|
||
# create prediction call | ||
pred_call <- make_pred_call(object$spec$method$pred$time) | ||
|
||
res <- eval_tidy(pred_call) | ||
# post-process the predictions | ||
|
||
if (!is.null(object$spec$method$pred$time$post)) { | ||
res <- object$spec$method$pred$time$post(res, object) | ||
} | ||
|
||
if (is.vector(res)) { | ||
res <- unname(res) | ||
} else { | ||
if (!inherits(res, "tbl_spark")) | ||
res <- as.data.frame(res) | ||
} | ||
res | ||
} | ||
|
||
|
||
#' @export | ||
#' @keywords internal | ||
#' @rdname other_predict | ||
#' @inheritParams predict_time.model_fit | ||
predict_time <- function(object, ...) | ||
UseMethod("predict_time") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the infrastructure for a "hazard" type?