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

handle attributes on outcome columns #1062

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# parsnip (development version)

* Tightened logic for outcome checking. This resolves issues—some errors and some silent failures—when atomic outcome variables have an attribute (#1060, #1061).

* `rpart_train()` has been deprecated in favor of using `decision_tree()` with the `"rpart"` engine or `rpart::rpart()` directly (#1044).

* Fixed bug in fitting some model types with the `"spark"` engine (#1045).
Expand Down
4 changes: 2 additions & 2 deletions R/convert_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
if (is.matrix(y)) {
y <- as.data.frame(y)
} else {
if (is.vector(y) | is.factor(y)) {
if (is.atomic(y) | is.factor(y)) {
y <- data.frame(y)
names(y) <- y_name
}
Expand Down Expand Up @@ -328,7 +328,7 @@ make_formula <- function(x, y, short = TRUE) {


will_make_matrix <- function(y) {
if (is.matrix(y) | is.vector(y) | is.factor(y))
if (is.matrix(y) | is.atomic(y) | is.factor(y))
return(FALSE)
cls <- unique(unlist(lapply(y, class)))
if (length(cls) > 1)
Expand Down
4 changes: 2 additions & 2 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fit_xy.model_spec <-
}
y_var <- colnames(y)

if (object$engine != "spark" & NCOL(y) == 1 & !(is.vector(y) | is.factor(y))) {
if (object$engine != "spark" & NCOL(y) == 1 & !(is.atomic(y) | is.factor(y))) {
if (is.matrix(y)) {
y <- y[, 1]
} else {
Expand Down Expand Up @@ -413,7 +413,7 @@ check_xy_interface <- function(x, y, cl, model) {

# `y` can be a vector (which is not a class), or a factor or
# Surv object (which are not vectors)
if (!is.null(y) && !is.vector(y))
if (!is.null(y) && !is.atomic(y))
inher(y, c("data.frame", "matrix", "factor", "Surv"), cl)

# rule out spark data sets that don't use the formula interface
Expand Down
4 changes: 2 additions & 2 deletions R/fit_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ xy_xy <- function(object, env, control, target = "none", ...) {
elapsed <- list(elapsed = NA_real_)
}

if (is.vector(env$y)) {
if (is.atomic(env$y)) {
y_name <- character(0)
} else {
y_name <- colnames(env$y)
Expand Down Expand Up @@ -199,7 +199,7 @@ xy_form <- function(object, env, control, ...) {
if (!is.null(env$y_var)) {
data_obj$y_var <- env$y_var
} else {
if (is.vector(env$y)) {
if (is.atomic(env$y)) {
data_obj$y_var <- character(0)
}

Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test_fit_interfaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,36 @@ test_that('No loaded engines', {
expect_snapshot_error({poisson_reg() %>% fit(mpg ~., data = mtcars)})
expect_snapshot_error({cubist_rules(engine = "Cubist") %>% fit(mpg ~., data = mtcars)})
})

test_that("fit_xy() can handle attributes on a data.frame outcome (#1060)", {
lr <- linear_reg()
x <- data.frame(x = 1:5)
y <- c(2:5, 5)

expect_silent(res <-
fit_xy(lr, x = x, y = data.frame(y = structure(y, label = "hi")))
)
expect_equal(res[["fit"]], fit_xy(lr, x, y)[["fit"]], ignore_attr = "label")
})

test_that("fit_xy() can handle attributes on an atomic outcome (#1061)", {
lr <- linear_reg()
x <- data.frame(x = 1:5)
y <- c(2:5, 5)

expect_silent(res <- fit_xy(lr, x = x, y = structure(y, label = "hi")))
expect_equal(res[["fit"]], fit_xy(lr, x, y)[["fit"]], ignore_attr = "label")
})

test_that("fit() can handle attributes on a vector outcome", {
lr <- linear_reg()
dat <- data.frame(x = 1:5, y = c(2:5, 5))
dat_attr <- data.frame(x = 1:5, y = structure(c(2:5, 5), label = "hi"))

expect_silent(res <- fit(lr, y ~ x, dat_attr))
expect_equal(
res[["fit"]],
fit(lr, y ~ x, dat)[["fit"]],
ignore_attr = TRUE
)
})
Loading