Skip to content

Commit 9ddb7ac

Browse files
committed
further changes for #431
1 parent 20d5ba4 commit 9ddb7ac

File tree

13 files changed

+217
-60
lines changed

13 files changed

+217
-60
lines changed

NEWS.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
# parsnip (development version)
22

3-
* `generics::required_pkgs()` was extended for `parsnip` objects.
4-
5-
* The `liquidSVM` engine for `svm_rbf()` was deprecated due to that package's removal from CRAN. (#425)
3+
## Model Specification Changes
64

75
* A new linear SVM model `svm_linear()` is now available with the `LiblineaR` engine (#424) and the `kernlab` engine (#438), and the `LiblineaR` engine is available for `logistic_reg()` as well (#429). These models can use sparse matrices via `fit_xy()` (#447) and have a `tidy` method (#474).
86

7+
* For models with `glmnet` engines:
8+
9+
- A single value is required for `penalty` (either a single numeric value or a value of `tune()`) (#481)
10+
- A special argument called `path_values` can be used to set the `lambda` path is a specific set of numbers (independent of the value of `penalty`). A pure ridge regression models (i.e., `mixture = 1`) will generate incorrect values if the path does not include zero. See issue #431.
11+
12+
* The `liquidSVM` engine for `svm_rbf()` was deprecated due to that package's removal from CRAN. (#425)
13+
914
* New model specification `survival_reg()` for the new mode `"censored regression"` (#444). `surv_reg()` is now soft-deprecated (#448).
1015

1116
* New model specification `proportional_hazards()` for the `"censored regression"` mode (#451).
1217

18+
## Other Changes
19+
1320
* Re-licensed package from GPL-2 to MIT. See [consent from copyright holders here](https://github.com/tidymodels/parsnip/issues/462).
1421

1522
* `set_mode()` now checks if `mode` is compatible with the model class, similar to `new_model_spec()` (@jtlandis, #467).
1623

1724
* Re-organized model documentation for `update` methods (#479).
1825

26+
27+
28+
* `generics::required_pkgs()` was extended for `parsnip` objects.
29+
30+
1931

2032
# parsnip 0.1.5
2133

R/logistic_reg.R

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,19 @@ translate.logistic_reg <- function(x, engine = x$engine, ...) {
108108
arg_vals <- x$method$fit$args
109109
arg_names <- names(arg_vals)
110110

111-
112111
if (engine == "glmnet") {
113-
# See discussion in https://github.com/tidymodels/parsnip/issues/195
114-
arg_vals$lambda <- NULL
112+
if (any(names(x$eng_args) == "path_values")) {
113+
# Since we decouple the parsnip `penalty` argument from being the same
114+
# as the glmnet `lambda` value, this allows users to set the path
115+
# differently from the default that glmnet uses. See
116+
# https://github.com/tidymodels/parsnip/issues/431
117+
x$method$fit$args$lambda <- x$eng_args$path_values
118+
x$eng_args$path_values <- NULL
119+
x$method$fit$args$path_values <- NULL
120+
} else {
121+
# See discussion in https://github.com/tidymodels/parsnip/issues/195
122+
x$method$fit$args$lambda <- NULL
123+
}
115124
# Since the `fit` information is gone for the penalty, we need to have an
116125
# evaluated value for the parameter.
117126
x$args$penalty <- rlang::eval_tidy(x$args$penalty)
@@ -133,11 +142,8 @@ translate.logistic_reg <- function(x, engine = x$engine, ...) {
133142
rlang::abort("For the LiblineaR engine, mixture must be 0 or 1.")
134143
}
135144
}
136-
145+
x$method$fit$args <- arg_vals
137146
}
138-
139-
x$method$fit$args <- arg_vals
140-
141147
x
142148
}
143149

R/multinom_reg.R

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,28 @@ print.multinom_reg <- function(x, ...) {
9696
}
9797

9898
#' @export
99-
translate.multinom_reg <- translate.linear_reg
99+
translate.multinom_reg <- function(x, engine = x$engine, ...) {
100+
x <- translate.default(x, engine, ...)
101+
102+
if (engine == "glmnet") {
103+
if (any(names(x$eng_args) == "path_values")) {
104+
# Since we decouple the parsnip `penalty` argument from being the same
105+
# as the glmnet `lambda` value, this allows users to set the path
106+
# differently from the default that glmnet uses. See
107+
# https://github.com/tidymodels/parsnip/issues/431
108+
x$method$fit$args$lambda <- x$eng_args$path_values
109+
x$eng_args$path_values <- NULL
110+
x$method$fit$args$path_values <- NULL
111+
} else {
112+
# See discussion in https://github.com/tidymodels/parsnip/issues/195
113+
x$method$fit$args$lambda <- NULL
114+
}
115+
# Since the `fit` information is gone for the penalty, we need to have an
116+
# evaluated value for the parameter.
117+
x$args$penalty <- rlang::eval_tidy(x$args$penalty)
118+
}
119+
x
120+
}
100121

101122
# ------------------------------------------------------------------------------
102123

man/contr_one_hot.Rd

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/linear_reg.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/logistic_reg.Rd

Lines changed: 32 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/multinom_reg.Rd

Lines changed: 32 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rmd/linear-reg.Rmd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ the value given in `linear_reg(penalty)`. For example:
3333
```{r glmnet-path}
3434
linear_reg(penalty = .1) %>%
3535
set_engine("glmnet", path_values = c(0, 10^seq(-10, 1, length.out = 20))) %>%
36-
set_mode("regression") %>%
3736
translate()
3837
```
3938

39+
When fitting a pure ridge regression model (i.e., `penalty = 0`), we _strongly
40+
suggest_ that you pass in a vector for `path_values` that includes zero. See
41+
[issue #431](https://github.com/tidymodels/parsnip/issues/431) for a discussion.
42+
4043
When using `predict()`, the single penalty value used for prediction is the one
4144
given to `linear_reg()`.
4245

@@ -49,7 +52,6 @@ with all of the penalty results.
4952
```{r stan-reg}
5053
linear_reg() %>%
5154
set_engine("stan") %>%
52-
set_mode("regression") %>%
5355
translate()
5456
```
5557

man/rmd/logistic-reg.Rmd

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,32 @@ logistic_reg() %>%
2020
```{r glmnet-csl}
2121
logistic_reg() %>%
2222
set_engine("glmnet") %>%
23-
set_mode("classification") %>%
2423
translate()
2524
```
2625

27-
For `glmnet` models, the full regularization path is always fit regardless of the
28-
value given to `penalty`. Also, there is the option to pass multiple values (or
29-
no values) to the `penalty` argument. When using the `predict()` method in these
30-
cases, the return value depends on the value of `penalty`. When using
31-
`predict()`, only a single value of the penalty can be used. When predicting on
32-
multiple penalties, the `multi_predict()` function can be used. It returns a
33-
tibble with a list column called `.pred` that contains a tibble with all of the
34-
penalty results.
26+
`logistic_reg()` requires a single value for the `penalty` argument (a number
27+
or `tune()`). Despite this, the full regularization path is always fit
28+
regardless of the value given to `penalty`. To pass in a custom sequence of
29+
values for `lambda`, use the argument `path_values` in `set_engine()`. This
30+
will assign the value of the glmnet `lambda` parameter without disturbing
31+
the value given in `logistic_reg(penalty)`. For example:
32+
33+
```{r glmnet-path}
34+
logistic_reg(penalty = .1) %>%
35+
set_engine("glmnet", path_values = c(0, 10^seq(-10, 1, length.out = 20))) %>%
36+
translate()
37+
```
38+
39+
When fitting a pure ridge regression model (i.e., `penalty = 0`), we _strongly
40+
suggest_ that you pass in a vector for `path_values` that includes zero. See
41+
[issue #431](https://github.com/tidymodels/parsnip/issues/431) for a discussion.
42+
43+
When using `predict()`, the single penalty value used for prediction is the one
44+
given to `logistic_reg()`.
45+
46+
To predict on multiple penalties, the `multi_predict()` function can be used.
47+
It returns a tibble with a list column called `.pred` that contains a tibble
48+
with all of the penalty results.
3549

3650
## LiblineaR
3751

man/rmd/multinom-reg.Rmd

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,32 @@ For this type of model, the template of the fit calls are below.
1111
```{r glmnet-cls}
1212
multinom_reg() %>%
1313
set_engine("glmnet") %>%
14-
set_mode("classification") %>%
1514
translate()
1615
```
1716

18-
For `glmnet` models, the full regularization path is always fit regardless of the
19-
value given to `penalty`. Also, there is the option to pass multiple values (or
20-
no values) to the `penalty` argument. When using the `predict()` method in these
21-
cases, the return value depends on the value of `penalty`. When using
22-
`predict()`, only a single value of the penalty can be used. When predicting on
23-
multiple penalties, the `multi_predict()` function can be used. It returns a
24-
tibble with a list column called `.pred` that contains a tibble with all of the
25-
penalty results.
17+
`multinom_reg()` requires a single value for the `penalty` argument (a number
18+
or `tune()`). Despite this, the full regularization path is always fit
19+
regardless of the value given to `penalty`. To pass in a custom sequence of
20+
values for `lambda`, use the argument `path_values` in `set_engine()`. This
21+
will assign the value of the glmnet `lambda` parameter without disturbing
22+
the value given in `multinom_reg(penalty)`. For example:
23+
24+
```{r glmnet-path}
25+
multinom_reg(penalty = .1) %>%
26+
set_engine("glmnet", path_values = c(0, 10^seq(-10, 1, length.out = 20))) %>%
27+
translate()
28+
```
29+
30+
When fitting a pure ridge regression model (i.e., `penalty = 0`), we _strongly
31+
suggest_ that you pass in a vector for `path_values` that includes zero. See
32+
[issue #431](https://github.com/tidymodels/parsnip/issues/431) for a discussion.
33+
34+
When using `predict()`, the single penalty value used for prediction is the one
35+
given to `multinom_reg()`.
36+
37+
To predict on multiple penalties, the `multi_predict()` function can be used.
38+
It returns a tibble with a list column called `.pred` that contains a tibble
39+
with all of the penalty results.
2640

2741
## nnet
2842

0 commit comments

Comments
 (0)