Skip to content
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
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New Features

* A "null model" is now available that fits a predictor-free model (using the mean of the outcome for regression or the mode for classification).
* `fit_xy()` can take a single column data frame or matrix for `y` without error

## Other Changes

Expand Down Expand Up @@ -38,7 +39,7 @@ First CRAN release

# parsnip 0.0.0.9005

* The engine, and any associated arguments, are now specified using `set_engine`. There is no `engine` argument
* The engine, and any associated arguments, are now specified using `set_engine()`. There is no `engine` argument


# parsnip 0.0.0.9004
Expand All @@ -64,7 +65,7 @@ First CRAN release

# parsnip 0.0.0.9000

* The `fit` interface was previously used to cover both the x/y interface as well as the formula interface. Now, `fit` is the formula interface and [`fit_xy` is for the x/y interface](https://github.com/topepo/parsnip/issues/33).
* The `fit` interface was previously used to cover both the x/y interface as well as the formula interface. Now, `fit()` is the formula interface and [`fit_xy()` is for the x/y interface](https://github.com/topepo/parsnip/issues/33).
* Added a `NEWS.md` file to track changes to the package.
* `predict` methods were [overhauled](https://github.com/topepo/parsnip/issues/34) to be [consistent](https://github.com/topepo/parsnip/issues/41).
* MARS was added.
8 changes: 8 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ fit_xy.model_spec <-
if (any(names(dots) == "engine"))
stop("Use `set_engine()` to supply the engine.", call. = FALSE)

if (object$engine != "spark" & NCOL(y) == 1 & !(is.vector(y) | is.factor(y))) {
if (is.matrix(y)) {
y <- y[, 1]
} else {
y <- y[[1]]
}
}

cl <- match.call(expand.dots = TRUE)
eval_env <- rlang::env()
eval_env$x <- x
Expand Down
25 changes: 24 additions & 1 deletion tests/testthat/test_fit_interfaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that('good args', {
expect_equal( tester(NULL, formula = f, data = iris, model = rmod), "formula")
expect_equal(tester_xy(NULL, x = iris, y = iris, model = rmod), "data.frame")
expect_equal( tester(NULL, f, data = iris, model = rmod), "formula")
expect_equal( tester(NULL, f, data = sprk, model = rmod), "formula")
expect_equal( tester(NULL, f, data = sprk, model = rmod), "formula")
})

#test_that('unnamed args', {
Expand All @@ -37,3 +37,26 @@ test_that('wrong args', {
expect_error(tester(NULL, f, data = as.matrix(iris[, 1:4])))
})

test_that('single column df for issue #129', {

expect_error(
lm1 <-
linear_reg() %>%
set_engine("lm") %>%
fit_xy(x = mtcars[, 2:4], y = mtcars[,1, drop = FALSE]),
regexp = NA
)
expect_error(
lm2 <-
linear_reg() %>%
set_engine("lm") %>%
fit_xy(x = mtcars[, 2:4], y = as.matrix(mtcars)[,1, drop = FALSE]),
regexp = NA
)
lm3 <-
linear_reg() %>%
set_engine("lm") %>%
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg)
expect_equal(coef(lm1), coef(lm3))
expect_equal(coef(lm2), coef(lm3))
})