Skip to content

Commit ba0c38e

Browse files
committed
touch-ups and snaps
1 parent d6b5dfa commit ba0c38e

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# error traps
2+
3+
Code
4+
linear_reg(penalty = 0.01) %>% set_engine("glmnet") %>% fit(mpg ~ ., data = mtcars[
5+
-(1:4), ]) %>% predict(mtcars[-(1:4), ], penalty = 0:1)
6+
Error <rlang_error>
7+
`penalty` should be a single numeric value. `multi_predict()` can be used to get multiple predictions per row of data.
8+
9+
---
10+
11+
Code
12+
linear_reg() %>% set_engine("glmnet") %>% fit(mpg ~ ., data = mtcars[-(1:4), ])
13+
Error <rlang_error>
14+
For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
15+
* There are 0 values for `penalty`.
16+
* To try multiple values for total regularization, use the tune package.
17+
* To predict multiple penalties, use `multi_predict()`
18+

tests/testthat/test-glmnet-linear.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test_that('glmnet execution error', {
1010
data("hpc_data", package = "modeldata", envir = rlang::current_env())
1111
hpc <- hpc_data[1:150, c(2:5, 8)]
1212

13-
hpc_basic <- linear_reg(penalty = .1, mixture = .3) %>%
13+
hpc_basic <- linear_reg(penalty = 0.1, mixture = 0.3) %>%
1414
set_engine("glmnet", nlambda = 15)
1515

1616
# this error/test is not glmnet-specific,
@@ -26,14 +26,16 @@ test_that('glmnet execution error', {
2626
})
2727

2828
test_that("glmnet model object", {
29+
skip_if_not_installed("glmnet")
30+
2931
hpc <- hpc_data[1:150, c(2:5, 8)]
3032
hpc_x <- model.matrix(~ log(compounds) + class, data = hpc)[, -1]
3133
hpc_y <- hpc$input_fields
3234

3335
exp_fit <- glmnet::glmnet(x = hpc_x, y = hpc_y, family = "gaussian",
3436
alpha = 0.3, nlambda = 15)
3537

36-
lm_spec <- linear_reg(penalty = 0.123, mixture = 0.3) %>%
38+
lm_spec <- linear_reg(penalty = 0.1, mixture = 0.3) %>%
3739
set_engine("glmnet", nlambda = 15)
3840
expect_no_error(
3941
f_fit <- fit(lm_spec, input_fields ~ log(compounds) + class, data = hpc)
@@ -81,7 +83,6 @@ test_that("glmnet prediction: type numeric", {
8183
})
8284

8385
test_that('glmnet prediction: column order of `new_data` irrelevant', {
84-
8586
skip_if_not_installed("glmnet")
8687

8788
data("hpc_data", package = "modeldata", envir = rlang::current_env())
@@ -133,7 +134,6 @@ test_that("formula interface can deal with missing values", {
133134
hpc <- hpc_data[1:150, c(2:5, 8)]
134135

135136
hpc$compounds[1] <- NA
136-
hpc_x[1,1] <- NA
137137

138138
lm_spec <- linear_reg(penalty = 0.123) %>% set_engine("glmnet")
139139
f_fit <- fit(lm_spec, input_fields ~ log(compounds) + class, data = hpc)

0 commit comments

Comments
 (0)