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 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: 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
1 change: 1 addition & 0 deletions R/linear_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ 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)
check_glmnet_penalty(x)
}

x
Expand Down
1 change: 1 addition & 0 deletions R/logistic_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ 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)
check_glmnet_penalty(x)
}

if (engine == "LiblineaR") {
Expand Down
11 changes: 10 additions & 1 deletion R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,13 @@ stan_conf_int <- function(object, newdata) {
rlang::eval_tidy(fn)
}


check_glmnet_penalty <- function(x) {
if (length(x$args$penalty) != 1) {
rlang::abort(c(
"For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).",
glue::glue("There are {length(x$args$penalty)} values for `penalty`."),
"To try multiple values for total regularization, use the tune package.",
"To predict multiple penalties, use `multi_predict()`"
))
}
}
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
10 changes: 6 additions & 4 deletions man/contr_one_hot.Rd

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

10 changes: 4 additions & 6 deletions man/linear_reg.Rd

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

11 changes: 4 additions & 7 deletions man/logistic_reg.Rd

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

9 changes: 4 additions & 5 deletions man/multinom_reg.Rd

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

7 changes: 1 addition & 6 deletions man/rmd/linear-reg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ Engines may have pre-set default arguments when executing the model fit call. Fo
```{r lm-reg}
linear_reg() %>%
set_engine("lm") %>%
set_mode("regression") %>%
translate()
```

## glmnet

```{r glmnet-csl}
linear_reg() %>%
linear_reg(penalty = 0.1) %>%
set_engine("glmnet") %>%
set_mode("regression") %>%
translate()
```

Expand All @@ -37,7 +35,6 @@ penalty results.
```{r stan-reg}
linear_reg() %>%
set_engine("stan") %>%
set_mode("regression") %>%
translate()
```

Expand All @@ -55,7 +52,6 @@ returned.
```{r spark-reg}
linear_reg() %>%
set_engine("spark") %>%
set_mode("regression") %>%
translate()
```

Expand All @@ -64,7 +60,6 @@ linear_reg() %>%
```{r keras-reg}
linear_reg() %>%
set_engine("keras") %>%
set_mode("regression") %>%
translate()
```

Expand Down
8 changes: 1 addition & 7 deletions man/rmd/logistic-reg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ For this type of model, the template of the fit calls are below.
```{r glm-reg}
logistic_reg() %>%
set_engine("glm") %>%
set_mode("classification") %>%
translate()
```

## glmnet

```{r glmnet-csl}
logistic_reg() %>%
logistic_reg(penalty = 0.1) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
translate()
```

Expand All @@ -38,7 +36,6 @@ penalty results.
```{r liblinear-reg}
logistic_reg() %>%
set_engine("LiblineaR") %>%
set_mode("classification") %>%
translate()
```

Expand All @@ -54,7 +51,6 @@ regularized regression models do not, which will result in different parameter e
```{r stan-reg}
logistic_reg() %>%
set_engine("stan") %>%
set_mode("classification") %>%
translate()
```

Expand All @@ -72,7 +68,6 @@ returned.
```{r spark-reg}
logistic_reg() %>%
set_engine("spark") %>%
set_mode("classification") %>%
translate()
```

Expand All @@ -81,7 +76,6 @@ logistic_reg() %>%
```{r keras-reg}
logistic_reg() %>%
set_engine("keras") %>%
set_mode("classification") %>%
translate()
```

Expand Down
6 changes: 1 addition & 5 deletions man/rmd/multinom-reg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ 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 All @@ -29,7 +28,6 @@ penalty results.
```{r nnet-cls}
multinom_reg() %>%
set_engine("nnet") %>%
set_mode("classification") %>%
translate()
```

Expand All @@ -38,7 +36,6 @@ multinom_reg() %>%
```{r spark-cls}
multinom_reg() %>%
set_engine("spark") %>%
set_mode("classification") %>%
translate()
```

Expand All @@ -47,7 +44,6 @@ multinom_reg() %>%
```{r keras-cls}
multinom_reg() %>%
set_engine("keras") %>%
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.

Loading