Skip to content

Harmonize error for wrong prediction type #489

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 5 commits into from
May 13, 2021
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
17 changes: 16 additions & 1 deletion R/aaa_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,27 @@ check_pred_info <- function(pred_obj, type) {
invisible(NULL)
}

check_spec_pred_type <- function(object, type) {
possible_preds <- names(object$spec$method$pred)
if (!any(possible_preds == type)) {
rlang::abort(c(
glue::glue("No {type} prediction method available for this model."),
glue::glue("Value for `type` should be one of: ",
glue::glue_collapse(glue::glue("'{possible_preds}'"), sep = ", "))
))
}
invisible(NULL)
}


check_pkg_val <- function(pkg) {
if (rlang::is_missing(pkg) || length(pkg) != 1 || !is.character(pkg))
if (rlang::is_missing(pkg) || length(pkg) != 1 || !is.character(pkg)) {
rlang::abort("Please supply a single character value for the package name.")
}
invisible(NULL)
}


check_interface_val <- function(x) {
exp_interf <- c("data.frame", "formula", "matrix")
if (length(x) != 1 || !(x %in% exp_interf)) {
Expand Down
3 changes: 1 addition & 2 deletions R/predict_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ predict_class.model_fit <- function(object, new_data, ...) {
if (object$spec$mode != "classification")
rlang::abort("`predict.model_fit()` is for predicting factor outcomes.")

if (!any(names(object$spec$method$pred) == "class"))
rlang::abort("No class prediction module defined for this model.")
check_spec_pred_type(object, "class")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
4 changes: 2 additions & 2 deletions R/predict_classprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ predict_classprob.model_fit <- function(object, new_data, ...) {
if (object$spec$mode != "classification")
rlang::abort("`predict.model_fit()` is for predicting factor outcomes.")

if (!any(names(object$spec$method$pred) == "prob"))
rlang::abort("No class probability module defined for this model.")
check_spec_pred_type(object, "prob")


if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_hazard.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
predict_hazard.model_fit <-
function(object, new_data, .time, ...) {

if (is.null(object$spec$method$pred$hazard))
rlang::abort("No hazard prediction method defined for this engine.")
check_spec_pred_type(object, "hazard")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
6 changes: 2 additions & 4 deletions R/predict_interval.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#' @export
predict_confint.model_fit <- function(object, new_data, level = 0.95, std_error = FALSE, ...) {

if (is.null(object$spec$method$pred$conf_int))
rlang::abort("No confidence interval method defined for this engine.")
check_spec_pred_type(object, "conf_int")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down Expand Up @@ -58,8 +57,7 @@ predict_confint <- function(object, ...)
# @export
predict_predint.model_fit <- function(object, new_data, level = 0.95, std_error = FALSE, ...) {

if (is.null(object$spec$method$pred$pred_int))
rlang::abort("No prediction interval method defined for this engine.")
check_spec_pred_type(object, "pred_int")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_linear_pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#' @export
predict_linear_pred.model_fit <- function(object, new_data, ...) {

if (!any(names(object$spec$method$pred) == "linear_pred"))
rlang::abort("No prediction module defined for this model.")
check_spec_pred_type(object, "linear_pred")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ predict_numeric.model_fit <- function(object, new_data, ...) {
"Use `predict_class()` or `predict_classprob()` for ",
"classification models."))

if (!any(names(object$spec$method$pred) == "numeric"))
rlang::abort("No prediction module defined for this model.")
check_spec_pred_type(object, "numeric")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_quantile.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
predict_quantile.model_fit <-
function(object, new_data, quantile = (1:9)/10, ...) {

if (is.null(object$spec$method$pred$quantile))
rlang::abort("No quantile prediction method defined for this engine.")
check_spec_pred_type(object, "quantile")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ predict_raw.model_fit <- function(object, new_data, opts = list(), ...) {
c(object$spec$method$pred$raw$args, opts)
}

if (!any(names(object$spec$method$pred) == "raw"))
rlang::abort("No raw prediction module defined for this model.")
check_spec_pred_type(object, "raw")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_survival.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
predict_survival.model_fit <-
function(object, new_data, .time, ...) {

if (is.null(object$spec$method$pred$survival))
rlang::abort("No survival prediction method defined for this engine.")
check_spec_pred_type(object, "survival")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
3 changes: 1 addition & 2 deletions R/predict_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ predict_time.model_fit <- function(object, new_data, ...) {
"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.")
check_spec_pred_type(object, "time")

if (inherits(object$fit, "try-error")) {
rlang::warn("Model fit failed; cannot make predictions.")
Expand Down
21 changes: 0 additions & 21 deletions R/svm_linear_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,6 @@ set_pred(
)
)
)
set_pred(
model = "svm_linear",
eng = "LiblineaR",
mode = "classification",
type = "prob",
value = list(
pre = function(x, object) {
rlang::abort(
paste0("The LiblineaR engine does not support class probabilities ",
"for any `svm` models.")
)
},
post = NULL,
func = c(fun = "predict"),
args =
list(
object = quote(object$fit),
newx = expr(as.matrix(new_data))
)
)
)
set_pred(
model = "svm_linear",
eng = "LiblineaR",
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_svm_linear.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ test_that('linear svm classification prediction: LiblineaR', {

expect_error(
predict(cls_form, hpc_no_m[ind, -5], type = "prob"),
"The LiblineaR engine does not support class probabilities"
"No prob prediction method available for this model"
)

expect_error(
predict(cls_xy_form, hpc_no_m[ind, -5], type = "prob"),
"The LiblineaR engine does not support class probabilities"
"No prob prediction method available for this model"
)

})
Expand Down