Skip to content

Improve errors for set_mode() and set_engine() #488

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 5 commits into from
May 13, 2021
Merged

Conversation

juliasilge
Copy link
Member

Closes #426

This PR makes our handling of a missing argument and a bad argument a bit more consistent for set_mode() and set_engine(). There was some handling coded up in both cases but we weren't really getting those benefits for missing (or even NULL) inputs before. This rearranging should make help things out.

The already existing warning/replacement for engines now works:

library(parsnip)
linear_reg() %>% set_engine()
#> Warning: `engine` was NULL and updated to be `lm`
#> Linear Regression Model Specification (regression)
#> 
#> Computational engine: lm

## same as before
linear_reg() %>% set_engine("ranger")
#> Error: Engine 'ranger' is not available. Please use one of: 'lm', 'glmnet', 'stan', 'spark', 'keras'

Created on 2021-05-11 by the reprex package (v2.0.0)

For mode, I just set this up to tell the user their options:

library(parsnip)
rand_forest() %>% set_mode()
#> Error: `mode` should be one of 'classification', 'regression', 'censored regression'
rand_forest() %>% set_mode(NULL)
#> Error: `mode` should be one of 'classification', 'regression', 'censored regression'
rand_forest() %>% set_mode("ranger")
#> Error: `mode` should be one of 'classification', 'regression', 'censored regression'

Created on 2021-05-11 by the reprex package (v2.0.0)

I did need to adjust some tests, switching between errors and warnings, but I don't think any of that is substantive.

@juliasilge
Copy link
Member Author

For both set_mode() and set_engine(), we could set a default value of NULL for the mode/engine and remove those lines like:

if (rlang::is_missing(engine)) engine <- NULL

Do we want to have a default value show up in the docs? It seems a little weird when a value of NULL just triggers these warnings / errors.

@topepo
Copy link
Member

topepo commented May 12, 2021

There might be a number of places in the code and docs to remove traces of "unknown" mode. TBH I'm fine with leaving that possible mode but not referencing it in warnings or errors that happen during checks about model fitting.

@juliasilge
Copy link
Member Author

I looked at this again today and changed my mind about the set_mode() approach. Now it all goes through check_spec_mode_val() and there is no more returning the generic parsnip:::all_modes in some situations:

library(parsnip)
logistic_reg() %>% set_mode()
#> Error: `mode` should be one of: 'unknown', 'classification'
logistic_reg() %>% set_mode("regression")
#> Error: `mode` should be one of: 'unknown', 'classification'

rand_forest() %>% set_mode()
#> Error: `mode` should be one of: 'unknown', 'classification', 'regression'
rand_forest() %>% set_mode(NULL)
#> Error: `mode` should be one of: 'unknown', 'classification', 'regression'
rand_forest() %>% set_mode("ranger")
#> Error: `mode` should be one of: 'unknown', 'classification', 'regression'

Created on 2021-05-12 by the reprex package (v2.0.0)

I tend to think it is OK to leave "unknown" for now and we can make a call about that in the future.

@juliasilge juliasilge requested a review from topepo May 12, 2021 22:37
@topepo topepo merged commit a546656 into master May 13, 2021
@github-actions
Copy link

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2021
@juliasilge juliasilge deleted the missing-engine-mode branch June 27, 2021 16:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More informative error when engine argument is missing in set_engine()
2 participants