Skip to content

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

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 8 commits into from
Jan 29, 2025
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
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).

* The deprecated function `rpart_train()` was removed after its deprecation period (#1044).

## Bug Fixes
Expand Down
22 changes: 22 additions & 0 deletions R/linear_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ translate.linear_reg <- function(x, engine = x$engine, ...) {
# evaluated value for the parameter.
x$args$penalty <- rlang::eval_tidy(x$args$penalty)
}

x
}

Expand Down Expand Up @@ -113,5 +114,26 @@ check_args.linear_reg <- function(object, call = rlang::caller_env()) {
check_number_decimal(args$mixture, min = 0, max = 1, allow_null = TRUE, call = call, arg = "mixture")
check_number_decimal(args$penalty, min = 0, allow_null = TRUE, call = call, arg = "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(object$eng_args) == "family"
if (any(is_fam)) {
eng_args <- rlang::eval_tidy(object$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"))
}
}

invisible(object)
}
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) %>% fit(mpg ~
., data = mtcars)
Condition
Error in `fit()`:
! Please install the glmnet package to use this engine.

---

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = stats::poisson) %>%
fit(mpg ~ ., data = mtcars)
Condition
Error in `fit()`:
! Please install the glmnet package to use this engine.

---

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = stats::poisson()) %>%
fit(mpg ~ ., data = mtcars)
Condition
Error in `fit()`:
! Please install the glmnet package to use this engine.

---

Code
linear_reg(penalty = 1) %>% set_engine("glmnet", family = "poisson") %>% fit(
mpg ~ ., data = mtcars)
Condition
Error in `fit()`:
! Please install the glmnet package to use this engine.

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


test_that("prevent using a Poisson family", {
skip_if(rlang::is_installed("glmnet"))
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = poisson) %>%
fit(mpg ~ ., data = mtcars),
error = TRUE
)
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = stats::poisson) %>%
fit(mpg ~ ., data = mtcars),
error = TRUE
)
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = stats::poisson()) %>%
fit(mpg ~ ., data = mtcars),
error = TRUE
)
expect_snapshot(
linear_reg(penalty = 1) %>%
set_engine("glmnet", family = "poisson") %>%
fit(mpg ~ ., data = mtcars),
error = TRUE
)
})
Loading