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

Divert linear regressions with poisson family to poisson_reg() #1219

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

* Aligned `null_model()` with other model types; the model type now has an engine argument that defaults to `"parsnip"` and is checked with the same machinery that checks other model types in the package (#1083).

* If linear regression is requested with a Poisson family, an error will occur and refer the user to `poisson_reg()` (#1219).

## Bug Fixes

* Make sure that parsnip does not convert ordered factor predictions to be unordered.
Expand Down
21 changes: 21 additions & 0 deletions R/linear_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ translate.linear_reg <- function(x, engine = x$engine, ...) {
# evaluated value for the parameter.
x$args$penalty <- rlang::eval_tidy(x$args$penalty)
}

# ------------------------------------------------------------------------------
# We want to avoid folks passing in a poisson family instead of using
# poisson_reg(). It's hard to detect this.

is_fam <- names(x$eng_args) == "family"
if (any(is_fam)) {
eng_args <- rlang::eval_tidy(x$eng_args[[which(is_fam)]])
if (is.function(eng_args)) {
eng_args <- try(eng_args(), silent = TRUE)
}
if (inherits(eng_args, "family")) {
eng_args <- eng_args$family
}
if (eng_args == "poisson") {
cli::cli_abort(
"A Poisson family was requested for {.fn linear_reg}. Please use
{.fn poisson_reg} and the engines in the {.pkg poissonreg} package.",
call = rlang::call2("linear_reg"))
}
}
x
}

Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/_snaps/linear_reg.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,39 @@
Error in `fit()`:
! `penalty` must be a number larger than or equal to 0 or `NULL`, not the number -1.

# prevent using a Poisson family

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = poisson) %>%
translate()
Condition
Error in `linear_reg()`:
! A Poisson family was requested for `linear_reg()`. Please use `poisson_reg()` and the engines in the poissonreg package.

---

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = stats::poisson) %>%
translate()
Condition
Error in `linear_reg()`:
! A Poisson family was requested for `linear_reg()`. Please use `poisson_reg()` and the engines in the poissonreg package.

---

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = stats::poisson()) %>%
translate()
Condition
Error in `linear_reg()`:
! A Poisson family was requested for `linear_reg()`. Please use `poisson_reg()` and the engines in the poissonreg package.

---

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = "poisson") %>%
translate()
Condition
Error in `linear_reg()`:
! A Poisson family was requested for `linear_reg()`. Please use `poisson_reg()` and the engines in the poissonreg package.

28 changes: 28 additions & 0 deletions tests/testthat/test-linear_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,31 @@ test_that("check_args() works", {
}
)
})


test_that("prevent using a Poisson family", {
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = poisson) %>%
translate(),
error = TRUE
)
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = stats::poisson) %>%
translate(),
error = TRUE
)
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = stats::poisson()) %>%
translate(),
error = TRUE
)
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = "poisson") %>%
translate(),
error = TRUE
)
Comment on lines +364 to +387
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the clarity of these tests

})
Loading