Skip to content

Commit 3e2447c

Browse files
juliasilgetopepo
andauthored
Better set_engine() docs (#667)
* Replace list of engines (no longer needed) with details on engines + engine arguments * Update spec details to link to `set_engine()` (mention eng args) * Redocument * added a note about naming conventions * cleaned up show_engines docs a bit * Clarify naming of main vs. engine args * Fix quotes again Co-authored-by: Max Kuhn <[email protected]>
1 parent 8f0678e commit 3e2447c

34 files changed

+175
-86
lines changed

R/engine_docs.R

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,33 +285,6 @@ find_details_topics <- function(mod, pkg = "parsnip") {
285285
unique(res)
286286
}
287287

288-
# For use in `set_engine()` docs
289-
generate_set_engine_bullets <- function() {
290-
env <- get_model_env()
291-
models <- env$models
292-
info <- rlang::env_get_list(env, models)
293-
294-
model_engines <- purrr::map(info, get_sorted_unique_engines)
295-
296-
model_prefixes <- glue::glue(
297-
"\\code{\\link[=.{models}.]{.{models}.()}}:",
298-
.open = ".{",
299-
.close = "}."
300-
)
301-
302-
bullets <- purrr::map2(
303-
.x = model_prefixes,
304-
.y = model_engines,
305-
.f = combine_prefix_with_engines
306-
)
307-
308-
bullets <- glue::glue("\\item {bullets}")
309-
bullets <- glue::glue_collapse(bullets, sep = "\n")
310-
bullets <- paste("\\itemize{", bullets, "}", sep = "\n")
311-
312-
bullets
313-
}
314-
315288
sort_c <- function(x) {
316289
withr::with_collate("C", sort(x))
317290
}

R/engines.R

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,54 @@ load_libs <- function(x, quiet, attach = FALSE) {
5454
#' `set_engine()` is used to specify which package or system will be used
5555
#' to fit the model, along with any arguments specific to that software.
5656
#'
57-
#' @section Engines:
58-
#' Based on the currently loaded packages, the following lists the set of
59-
#' engines available to each model specification.
57+
#' @details
58+
#' In parsnip,
59+
#'
60+
#' - the model **type** differentiates basic modeling approaches, such as random
61+
#' forests, logistic regression, linear support vector machines, etc.,
62+
#' - the **mode** denotes in what kind of modeling context it will be used
63+
#' (most commonly, classification or regression), and
64+
#' - the computational **engine** indicates how the model is fit, such as with
65+
#' a specific R package implementation or even methods outside of R like Keras
66+
#' or Stan.
67+
#'
68+
#' Use [show_engines()] to get a list of possible engines for the model of
69+
#' interest.
70+
#'
71+
#' Modeling functions in parsnip separate model arguments into two categories:
72+
#'
73+
#' - _Main arguments_ are more commonly used and tend to be available across
74+
#' engines. These names are standardized to work with different engines in a
75+
#' consistent way, so you can use the \pkg{parsnip} main argument `trees`,
76+
#' instead of the heterogeneous arguments for this parameter from \pkg{ranger}
77+
#' and \pkg{randomForest} packages (`num.trees` and `ntree`, respectively). Set
78+
#' these in your model type function, like `rand_forest(trees = 2000)`.
79+
#' - _Engine arguments_ are either specific to a particular engine or used
80+
#' more rarely; there is no change for these argument names from the underlying
81+
#' engine. Set these in `set_engine()`, like
82+
#' `set_engine("ranger", importance = "permutation")`.
6083
#'
61-
#' \Sexpr[stage=render,results=rd]{parsnip:::generate_set_engine_bullets()}
6284
#'
6385
#' @param object A model specification.
6486
#' @param engine A character string for the software that should
6587
#' be used to fit the model. This is highly dependent on the type
6688
#' of model (e.g. linear regression, random forest, etc.).
6789
#' @param ... Any optional arguments associated with the chosen computational
68-
#' engine. These are captured as quosures and can be `tune()`.
90+
#' engine. These are captured as quosures and can be tuned with `tune()`.
6991
#' @return An updated model specification.
7092
#' @examples
71-
#' # First, set general arguments using the standardized names
72-
#' mod <-
73-
#' logistic_reg(penalty = 0.01, mixture = 1/3) %>%
74-
#' # now say how you want to fit the model and another other options
75-
#' set_engine("glmnet", nlambda = 10)
76-
#' translate(mod, engine = "glmnet")
93+
#' # First, set main arguments using the standardized names
94+
#' logistic_reg(penalty = 0.01, mixture = 1/3) %>%
95+
#' # Now specify how you want to fit the model with another argument
96+
#' set_engine("glmnet", nlambda = 10) %>%
97+
#' translate()
98+
#'
99+
#' # Many models have possible engine-specific arguments
100+
#' decision_tree(tree_depth = 5) %>%
101+
#' set_engine("rpart", parms = list(prior = c(.65,.35))) %>%
102+
#' set_mode("classification") %>%
103+
#' translate()
104+
#'
77105
#' @export
78106
set_engine <- function(object, engine, ...) {
79107
mod_type <- class(object)[1]
@@ -107,11 +135,12 @@ set_engine <- function(object, engine, ...) {
107135
#' Display currently available engines for a model
108136
#'
109137
#' The possible engines for a model can depend on what packages are loaded.
110-
#' Some `parsnip`-adjacent packages add engines to existing models. For example,
111-
#' the `multilevelmod` package adds additional engines for the [linear_reg()]
112-
#' model and these are not available unless `multilevelmod` is loaded.
138+
#' Some \pkg{parsnip} extension add engines to existing models. For example,
139+
#' the \pkg{poissonreg} package adds additional engines for the [poisson_reg()]
140+
#' model and these are not available unless \pkg{poissonreg} is loaded.
113141
#' @param x The name of a `parsnip` model (e.g., "linear_reg", "mars", etc.)
114142
#' @return A tibble.
143+
#'
115144
#' @examples
116145
#' show_engines("linear_reg")
117146
#' @export

man-roxygen/spec-details.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#' @details
22
#' This function only defines what _type_ of model is being fit. Once an engine
3-
#' is specified, the _method_ to fit the model is also defined.
3+
#' is specified, the _method_ to fit the model is also defined. See
4+
#' [set_engine()] for more on setting the engine, including how to set engine
5+
#' arguments.
46
#'
57
#' The model is not trained or fit until the [`fit()`][fit.model_spec()] function is used
68
#' with the data.

man/C5_rules.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bag_mars.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bag_tree.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bart.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/boost_tree.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cubist_rules.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/decision_tree.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)