Skip to content

Be more verbose about prediction types in check_pred_type() #899

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

Merged
merged 3 commits into from
Mar 14, 2023
Merged
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
35 changes: 25 additions & 10 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ predict.model_fit <- function(object, new_data, type = NULL, opts = list(), ...)
res
}

surv_types <- c("time", "survival", "hazard")

check_pred_type <- function(object, type, ...) {
if (is.null(type)) {
type <-
Expand All @@ -197,14 +195,31 @@ check_pred_type <- function(object, type, ...) {
glue_collapse(pred_types, sep = ", ", last = " and ")
)
)
if (type == "numeric" & object$spec$mode != "regression")
rlang::abort("For numeric predictions, the object should be a regression model.")
if (type == "class" & object$spec$mode != "classification")
rlang::abort("For class predictions, the object should be a classification model.")
if (type == "prob" & object$spec$mode != "classification")
rlang::abort("For probability predictions, the object should be a classification model.")
if (type %in% surv_types & object$spec$mode != "censored regression")
rlang::abort("For event time predictions, the object should be a censored regression.")

switch(
type,
"numeric" = if (object$spec$mode != "regression") {
rlang::abort("For numeric predictions, the object should be a regression model.")
},
"class" = if (object$spec$mode != "classification") {
rlang::abort("For class predictions, the object should be a classification model.")
},
"prob" = if (object$spec$mode != "classification") {
rlang::abort("For probability predictions, the object should be a classification model.")
},
"time" = if (object$spec$mode != "censored regression") {
rlang::abort("For event time predictions, the object should be a censored regression.")
},
"survival" = if (object$spec$mode != "censored regression") {
rlang::abort("For survival probability predictions, the object should be a censored regression.")
},
"hazard" = if (object$spec$mode != "censored regression") {
rlang::abort("For hazard predictions, the object should be a censored regression.")
},
"linear_pred" = if (object$spec$mode != "censored regression") {
rlang::abort("For the linear predictor, the object should be a censored regression.")
}
)

# TODO check for ... options when not the correct type
type
Expand Down