Skip to content

augment() for models without class probabilities #487

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
8 changes: 6 additions & 2 deletions R/aaa_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,13 @@ check_pred_info <- function(pred_obj, type) {
invisible(NULL)
}

check_spec_pred_type <- function(object, type) {
spec_has_pred_type <- function(object, type) {
possible_preds <- names(object$spec$method$pred)
if (!any(possible_preds == type)) {
any(possible_preds == type)
}
check_spec_pred_type <- function(object, type) {
if (!spec_has_pred_type(object, type)) {
possible_preds <- names(object$spec$method$pred)
rlang::abort(c(
glue::glue("No {type} prediction method available for this model."),
glue::glue("Value for `type` should be one of: ",
Expand Down
20 changes: 14 additions & 6 deletions R/augment.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#' [fit()] and `new_data` contains the outcome column, a `.resid` column is
#' also added.
#'
#' For classification models, the results include a column called `.pred_class`
#' as well as class probability columns named `.pred_{level}`.
#' For classification models, the results can include a column called
#' `.pred_class` as well as class probability columns named `.pred_{level}`.
#' This depends on what type of prediction types are available for the model.
#' @param x A `model_fit` object produced by [fit()] or [fit_xy()].
#' @param new_data A data frame or matrix.
#' @param ... Not currently used.
Expand Down Expand Up @@ -56,6 +57,7 @@
#'
augment.model_fit <- function(x, new_data, ...) {
if (x$spec$mode == "regression") {
check_spec_pred_type(x, "numeric")
new_data <-
new_data %>%
dplyr::bind_cols(
Expand All @@ -68,12 +70,18 @@ augment.model_fit <- function(x, new_data, ...) {
}
}
} else if (x$spec$mode == "classification") {
new_data <-
new_data %>%
dplyr::bind_cols(
predict(x, new_data = new_data, type = "class"),
if (spec_has_pred_type(x, "class")) {
new_data <- dplyr::bind_cols(
new_data,
predict(x, new_data = new_data, type = "class")
)
}
if (spec_has_pred_type(x, "prob")) {
new_data <- dplyr::bind_cols(
new_data,
predict(x, new_data = new_data, type = "prob")
)
}
} else {
rlang::abort(paste("Unknown mode:", x$spec$mode))
}
Expand Down
5 changes: 3 additions & 2 deletions man/augment.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-augment.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ test_that('classification models', {

})


test_that('augment for model without class probabilities', {
skip_if_not_installed("LiblineaR")

data(two_class_dat, package = "modeldata")
x <- svm_linear(mode = "classification") %>% set_engine("LiblineaR")
cls_form <- x %>% fit(Class ~ ., data = two_class_dat)

expect_equal(
colnames(augment(cls_form, head(two_class_dat))),
c("A", "B", "Class", ".pred_class")
)
expect_equal(nrow(augment(cls_form, head(two_class_dat))), 6)

})