Skip to content

Add error for glmnet models if penalty is not exactly 1 #485

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 7 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 1 deletion R/engines.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ load_libs <- function(x, quiet, attach = FALSE) {
#' @examples
#' # First, set general arguments using the standardized names
#' mod <-
#' logistic_reg(mixture = 1/3) %>%
#' logistic_reg(penalty = 0.01, mixture = 1/3) %>%
#' # now say how you want to fit the model and another other options
#' set_engine("glmnet", nlambda = 10)
#' translate(mod, engine = "glmnet")
Expand Down
7 changes: 7 additions & 0 deletions R/linear_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ translate.linear_reg <- function(x, engine = x$engine, ...) {
# Since the `fit` information is gone for the penalty, we need to have an
# evaluated value for the parameter.
x$args$penalty <- rlang::eval_tidy(x$args$penalty)
if (length(x$args$penalty) != 1) {
rlang::abort(c(
"For the glmnet engine, `penalty` must be a single number.",
glue::glue("There are {length(x$args$penalty)} values for `penalty`."),
"To try multiple values for total regularization, use the tune package."
))
}
}

x
Expand Down
7 changes: 7 additions & 0 deletions R/logistic_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ translate.logistic_reg <- function(x, engine = x$engine, ...) {
# Since the `fit` information is gone for the penalty, we need to have an
# evaluated value for the parameter.
x$args$penalty <- rlang::eval_tidy(x$args$penalty)
if (length(x$args$penalty) != 1) {
rlang::abort(c(
"For the glmnet engine, `penalty` must be a single number.",
glue::glue("There are {length(x$args$penalty)} values for `penalty`."),
"To try multiple values for total regularization, use the tune package."
))
}
}

if (engine == "LiblineaR") {
Expand Down
2 changes: 1 addition & 1 deletion R/translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' translate(lm_spec, engine = "spark")
#'
#' # with a placeholder for an unknown argument value:
#' translate(linear_reg(mixture = varying()), engine = "glmnet")
#' translate(linear_reg(penalty = varying(), mixture = varying()), engine = "glmnet")
#'
#' @export

Expand Down
5 changes: 4 additions & 1 deletion man/linear_reg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/logistic_reg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/multinom_reg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/rmd/linear-reg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ linear_reg() %>%
## glmnet

```{r glmnet-csl}
linear_reg() %>%
linear_reg(penalty = 0.1) %>%
set_engine("glmnet") %>%
set_mode("regression") %>%
translate()
Expand Down
2 changes: 1 addition & 1 deletion man/rmd/logistic-reg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ logistic_reg() %>%
## glmnet

```{r glmnet-csl}
logistic_reg() %>%
logistic_reg(penalty = 0.1) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
translate()
Expand Down
2 changes: 1 addition & 1 deletion man/rmd/multinom-reg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ For this type of model, the template of the fit calls are below.
## glmnet

```{r glmnet-cls}
multinom_reg() %>%
multinom_reg(penalty = 0.1) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
translate()
Expand Down
2 changes: 1 addition & 1 deletion man/set_engine.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/translate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 13 additions & 30 deletions tests/testthat/test_linear_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ hpc <- hpc_data[1:150, c(2:5, 8)]
test_that('primary arguments', {
basic <- linear_reg()
basic_lm <- translate(basic %>% set_engine("lm"))
basic_glmnet <- translate(basic %>% set_engine("glmnet"))
expect_error(
basic_glmnet <- translate(basic %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)
basic_stan <- translate(basic %>% set_engine("stan"))
basic_spark <- translate(basic %>% set_engine("spark"))
expect_equal(basic_lm$method$fit$args,
Expand All @@ -25,14 +28,6 @@ test_that('primary arguments', {
weights = expr(missing_arg())
)
)
expect_equal(basic_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
family = "gaussian"
)
)
expect_equal(basic_stan$method$fit$args,
list(
formula = expr(missing_arg()),
Expand All @@ -51,17 +46,11 @@ test_that('primary arguments', {
)

mixture <- linear_reg(mixture = 0.128)
mixture_glmnet <- translate(mixture %>% set_engine("glmnet"))
mixture_spark <- translate(mixture %>% set_engine("spark"))
expect_equal(mixture_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
alpha = new_empty_quosure(0.128),
family = "gaussian"
)
expect_error(
mixture_glmnet <- translate(mixture %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)
mixture_spark <- translate(mixture %>% set_engine("spark"))
expect_equal(mixture_spark$method$fit$args,
list(
x = expr(missing_arg()),
Expand Down Expand Up @@ -92,17 +81,11 @@ test_that('primary arguments', {
)

mixture_v <- linear_reg(mixture = varying())
mixture_v_glmnet <- translate(mixture_v %>% set_engine("glmnet"))
mixture_v_spark <- translate(mixture_v %>% set_engine("spark"))
expect_equal(mixture_v_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
alpha = new_empty_quosure(varying()),
family = "gaussian"
)
expect_error(
mixture_v_glmnet <- translate(mixture_v %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)
mixture_v_spark <- translate(mixture_v %>% set_engine("spark"))
expect_equal(mixture_v_spark$method$fit$args,
list(
x = expr(missing_arg()),
Expand All @@ -125,7 +108,7 @@ test_that('engine arguments', {
)
)

glmnet_nlam <- linear_reg() %>% set_engine("glmnet", nlambda = 10)
glmnet_nlam <- linear_reg(penalty = 0.1) %>% set_engine("glmnet", nlambda = 10)
expect_equal(translate(glmnet_nlam)$method$fit$args,
list(
x = expr(missing_arg()),
Expand Down
43 changes: 13 additions & 30 deletions tests/testthat/test_logistic_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ hpc <- hpc_data[1:150, c(2:5, 8)]
test_that('primary arguments', {
basic <- logistic_reg()
basic_glm <- translate(basic %>% set_engine("glm"))
basic_glmnet <- translate(basic %>% set_engine("glmnet"))
expect_error(
basic_glmnet <- translate(basic %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)
basic_liblinear <- translate(basic %>% set_engine("LiblineaR"))
basic_stan <- translate(basic %>% set_engine("stan"))
basic_spark <- translate(basic %>% set_engine("spark"))
Expand All @@ -28,14 +31,6 @@ test_that('primary arguments', {
family = expr(stats::binomial)
)
)
expect_equal(basic_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
family = "binomial"
)
)
expect_equal(basic_liblinear$method$fit$args,
list(
x = expr(missing_arg()),
Expand Down Expand Up @@ -63,17 +58,11 @@ test_that('primary arguments', {
)

mixture <- logistic_reg(mixture = 0.128)
mixture_glmnet <- translate(mixture %>% set_engine("glmnet"))
mixture_spark <- translate(mixture %>% set_engine("spark"))
expect_equal(mixture_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
alpha = new_empty_quosure(0.128),
family = "binomial"
)
expect_error(
mixture_glmnet <- translate(mixture %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)
mixture_spark <- translate(mixture %>% set_engine("spark"))
expect_equal(mixture_spark$method$fit$args,
list(
x = expr(missing_arg()),
Expand Down Expand Up @@ -116,18 +105,12 @@ test_that('primary arguments', {
)

mixture_v <- logistic_reg(mixture = varying())
mixture_v_glmnet <- translate(mixture_v %>% set_engine("glmnet"))
expect_error(
mixture_v_glmnet <- translate(mixture_v %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)
mixture_v_liblinear <- translate(mixture_v %>% set_engine("LiblineaR"))
mixture_v_spark <- translate(mixture_v %>% set_engine("spark"))
expect_equal(mixture_v_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
alpha = new_empty_quosure(varying()),
family = "binomial"
)
)
expect_equal(mixture_v_liblinear$method$fit$args,
list(
x = expr(missing_arg()),
Expand Down Expand Up @@ -194,7 +177,7 @@ test_that('engine arguments', {
)
)

glmnet_nlam <- logistic_reg()
glmnet_nlam <- logistic_reg(penalty = 0.1)
expect_equal(
translate(glmnet_nlam %>% set_engine("glmnet", nlambda = 10))$method$fit$args,
list(
Expand Down
20 changes: 7 additions & 13 deletions tests/testthat/test_multinom_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ hpc <- hpc_data[1:150, c(2:5, 8)]

test_that('primary arguments', {
basic <- multinom_reg()
basic_glmnet <- translate(basic %>% set_engine("glmnet"))
expect_equal(basic_glmnet$method$fit$args,
list(
x = expr(missing_arg()),
y = expr(missing_arg()),
weights = expr(missing_arg()),
family = "multinomial"
)
expect_error(
basic_glmnet <- translate(basic %>% set_engine("glmnet")),
"For the glmnet engine, `penalty` must be a single"
)

mixture <- multinom_reg(mixture = 0.128)
mixture <- multinom_reg(penalty = 0.1, mixture = 0.128)
mixture_glmnet <- translate(mixture %>% set_engine("glmnet"))
expect_equal(mixture_glmnet$method$fit$args,
list(
Expand All @@ -46,7 +40,7 @@ test_that('primary arguments', {
)
)

mixture_v <- multinom_reg(mixture = varying())
mixture_v <- multinom_reg(penalty = 0.01, mixture = varying())
mixture_v_glmnet <- translate(mixture_v %>% set_engine("glmnet"))
expect_equal(mixture_v_glmnet$method$fit$args,
list(
Expand All @@ -61,7 +55,7 @@ test_that('primary arguments', {
})

test_that('engine arguments', {
glmnet_nlam <- multinom_reg()
glmnet_nlam <- multinom_reg(penalty = 0.01)
expect_equal(
translate(glmnet_nlam %>% set_engine("glmnet", nlambda = 10))$method$fit$args,
list(
Expand Down Expand Up @@ -117,5 +111,5 @@ test_that('bad input', {
expect_error(multinom_reg(mode = "regression"))
expect_error(translate(multinom_reg() %>% set_engine("wat?")))
expect_error(translate(multinom_reg() %>% set_engine()))
expect_warning(translate(multinom_reg() %>% set_engine("glmnet", x = hpc[,1:3], y = hpc$class)))
expect_warning(translate(multinom_reg(penalty = 0.01) %>% set_engine("glmnet", x = hpc[,1:3], y = hpc$class)))
})