-
Notifications
You must be signed in to change notification settings - Fork 93
improve docs and errors re: model formulas #1015
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f622eb
improve docs and errors re: model formulas
simonpcouch 18e57b4
add `model_formula` pkgdown entry
simonpcouch 250adc8
apply suggestions from code review
simonpcouch 8d502ee
Merge branch 'main' into model-formula
simonpcouch 19b1645
bump dev version
simonpcouch 9fa3100
"model terms" -> "input variables"
simonpcouch a7c4ac4
Merge branch 'main' into model-formula
simonpcouch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#' Formulas with special terms in tidymodels | ||
simonpcouch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
#' @description | ||
#' | ||
#' In R, formulas provide a compact, symbolic notation to specify model terms. | ||
#' Many modeling functions in R make use of ["specials"][stats::terms.formula], | ||
#' or nonstandard notations used in formulas. Specials are defined and handled as | ||
#' a special case by a given modeling package. For example, the mgcv package, | ||
#' which provides support for | ||
#' [generalized additive models][parsnip::gen_additive_mod] in R, defines a | ||
#' function `s()` to be in-lined into formulas. It can be used like so: | ||
#' | ||
#' ``` r | ||
#' mgcv::gam(mpg ~ wt + s(disp, k = 5), data = mtcars) | ||
#' ``` | ||
#' | ||
#' In this example, the `s()` special defines a smoothing term that the mgcv | ||
#' package knows to look for when preprocessing model input. | ||
#' | ||
#' The parsnip package can handle most specials without issue. The analogous | ||
#' code for specifying this generalized additive model | ||
#' [with the parsnip "mgcv" engine][parsnip::details_gen_additive_mod_mgcv] | ||
#' looks like: | ||
#' | ||
#' ``` r | ||
#' gen_additive_mod() %>% | ||
#' set_mode("regression") %>% | ||
#' set_engine("mgcv") %>% | ||
#' fit(mpg ~ wt + s(disp, k = 5), data = mtcars) | ||
#' ``` | ||
#' | ||
#' However, parsnip is often used in conjunction with the greater tidymodels | ||
#' package ecosystem, which defines its own pre-processing infrastructure and | ||
#' functionality via packages like hardhat and recipes. The specials defined | ||
#' in many modeling packages introduce conflicts with that infrastructure. | ||
#' | ||
#' To support specials while also maintaining consistent syntax elsewhere in | ||
#' the ecosystem, **tidymodels delineates between two types of formulas: | ||
#' preprocessing formulas and model formulas**. Preprocessing formulas determine | ||
#' the model terms, while model formulas determine the model structure. | ||
simonpcouch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
#' @section Example: | ||
#' | ||
#' To create the preprocessing formula from the model formula, just remove | ||
#' the specials, retaining references to model terms themselves. For example: | ||
simonpcouch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
#' ``` | ||
#' model_formula <- mpg ~ wt + s(disp, k = 5) | ||
#' preproc_formula <- mpg ~ wt + disp | ||
#' ``` | ||
#' | ||
#' \itemize{ | ||
#' \item **With parsnip,** use the model formula: | ||
#' | ||
#' ``` r | ||
#' model_spec <- | ||
#' gen_additive_mod() %>% | ||
#' set_mode("regression") %>% | ||
#' set_engine("mgcv") | ||
#' | ||
#' model_spec %>% | ||
#' fit(model_formula, data = mtcars) | ||
#' ``` | ||
#' | ||
#' \item **With recipes**, use the preprocessing formula only: | ||
#' | ||
#' ``` r | ||
#' library(recipes) | ||
#' | ||
#' recipe(preproc_formula, mtcars) | ||
#' ``` | ||
#' | ||
#' The recipes package supplies a large variety of preprocessing techniques | ||
#' that may replace the need for specials altogether, in some cases. | ||
#' | ||
#' \item **With workflows,** use the preprocessing formula everywhere, but | ||
#' pass the model formula to the `formula` argument in `add_model()`: | ||
#' | ||
#' ``` r | ||
#' library(workflows) | ||
#' | ||
#' wflow <- | ||
#' workflow() %>% | ||
#' add_formula(preproc_formula) %>% | ||
#' add_model(model_spec, formula = model_formula) | ||
#' | ||
#' fit(wflow, data = mtcars) | ||
#' ``` | ||
#' | ||
#' The workflow will then pass the model formula to parsnip, using the | ||
#' preprocessor formula elsewhere. We would still use the preprocessing | ||
#' formula if we had added a recipe preprocessor using `add_recipe()` | ||
#' instead a formula via `add_formula()`. | ||
#' | ||
#' } | ||
#' | ||
#' @name model_formula | ||
NULL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.