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

making orbital work with classification #62

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -2,6 +2,7 @@

S3method(augment,orbital_class)
S3method(orbital,default)
S3method(orbital,glm)
S3method(orbital,last_fit)
S3method(orbital,model_fit)
S3method(orbital,model_spec)
Expand Down
17 changes: 17 additions & 0 deletions R/model-glm.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#' @export
orbital.glm <- function(x, ..., mode = c("classification", "regression")) {
mode <- rlang::arg_match(mode)

if (mode == "classification") {
levels <- levels(x$model$Species)
levels <- glue::double_quote(levels)
res <- tidypredict::tidypredict_fit(x)
res <- deparse1(res)
res <- glue::glue("dplyr::case_when({res} < 0.5 ~ {levels[1]}, .default = {levels[2]})")
}

if (mode == "regression") {
res <- tidypredict::tidypredict_fit(x)
}
res
}
60 changes: 44 additions & 16 deletions R/parsnip.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
#' @export
orbital.model_fit <- function(x, ..., prefix = ".pred") {
res <- tryCatch(
tidypredict::tidypredict_fit(x),
error = function(cnd) {
if (grepl("no applicable method for", cnd$message)) {
cls <- class(x)
cls <- setdiff(cls, "model_fit")
cls <- gsub("^_", "", cls)

cli::cli_abort(
"A model of class {.cls {cls}} is not supported.",
call = rlang::call2("orbital")
)
mode <- x$spec$mode

check_mode(mode)

res <- try(orbital(x$fit, mode = mode), silent = TRUE)

if (inherits(res, "try-error")) {
res <- tryCatch(
tidypredict::tidypredict_fit(x),
error = function(cnd) {
if (grepl("no applicable method for", cnd$message)) {
cls <- class(x)
cls <- setdiff(cls, "model_fit")
cls <- gsub("^_", "", cls)

cli::cli_abort(
"A model of class {.cls {cls}} is not supported.",
call = rlang::call2("orbital")
)
}
stop(cnd)
}
stop(cnd)
}
)
)
}

if (mode == "classification") {
prefix <- paste0(prefix, "_class")
}

res <- stats::setNames(deparse1(res), prefix)
if (is.language(res)) {
res <- deparse1(res)
}

res <- stats::setNames(res, prefix)

new_orbital_class(res)
}
Expand All @@ -26,3 +42,15 @@ orbital.model_fit <- function(x, ..., prefix = ".pred") {
orbital.model_spec <- function(x, ...) {
cli::cli_abort("{.arg x} must be fitted model.")
}

check_mode <- function(mode, call = rlang::caller_env()) {
supported_modes <- c("regression", "classification")

if (!(mode %in% supported_modes)) {
cli::cli_abort(
"Only models with modes {.val {supported_modes}} are supported.
Not {.val {mode}}.",
call = call
)
}
}
Loading