diff --git a/R/aaa_multi_predict.R b/R/aaa_multi_predict.R index f79e8e9e7..e4a7a0cc7 100644 --- a/R/aaa_multi_predict.R +++ b/R/aaa_multi_predict.R @@ -3,7 +3,7 @@ #' Model predictions across many sub-models #' #' For some models, predictions can be made on sub-models in the model object. -#' @param object A `model_fit` object. +#' @param object A [model fit][model_fit]. #' @param new_data A rectangular data object, such as a data frame. #' @param type A single character value or `NULL`. Possible values are #' `"numeric"`, `"class"`, `"prob"`, `"conf_int"`, `"pred_int"`, `"quantile"`, @@ -38,7 +38,7 @@ multi_predict.default <- function(object, ...) { #' @export predict.model_spec <- function(object, ...) { cli::cli_abort( - "You must {.fun fit} your model specification + "You must {.fun fit} your {.help [model specification](parsnip::model_spec)} before you can use {.fun predict}." ) } diff --git a/R/arguments.R b/R/arguments.R index 0e62a0938..f3d3198e4 100644 --- a/R/arguments.R +++ b/R/arguments.R @@ -35,7 +35,7 @@ check_eng_args <- function(args, obj, core_args) { #' `set_args()` can be used to modify the arguments of a model specification while #' `set_mode()` is used to change the model's mode. #' -#' @param object A model specification. +#' @param object A [model specification][model_spec]. #' @param ... One or more named model arguments. #' @param mode A character string for the model type (e.g. "classification" or #' "regression") @@ -134,7 +134,7 @@ maybe_eval <- function(x) { #' Evaluate parsnip model arguments #' @export #' @keywords internal -#' @param spec A model specification +#' @param spec A [model specification][model_spec]. #' @param ... Not used. eval_args <- function(spec, ...) { spec$args <- purrr::map(spec$args, maybe_eval) diff --git a/R/augment.R b/R/augment.R index 17af1dd78..a825d3730 100644 --- a/R/augment.R +++ b/R/augment.R @@ -2,7 +2,7 @@ #' #' `augment()` will add column(s) for predictions to the given data. #' -#' @param x A `model_fit` object produced by [fit.model_spec()] or +#' @param x A [model fit][model_fit] produced by [fit.model_spec()] or #' [fit_xy.model_spec()]. #' @param eval_time For censored regression models, a vector of time points at #' which the survival probability is estimated. diff --git a/R/case_weights.R b/R/case_weights.R index 0c9d45e64..3708f5020 100644 --- a/R/case_weights.R +++ b/R/case_weights.R @@ -72,7 +72,7 @@ patch_formula_environment_with_case_weights <- function(formula, #' Not all modeling engines can incorporate case weights into their #' calculations. This function can determine whether they can be used. #' -#' @param spec A parsnip model specification. +#' @param spec A parsnip [model specification][model_spec]. #' @return A single logical. #' @examples #' case_weights_allowed(linear_reg()) diff --git a/R/convert_data.R b/R/convert_data.R index 48a751a31..e5881fd14 100644 --- a/R/convert_data.R +++ b/R/convert_data.R @@ -160,7 +160,7 @@ } -#' @param object An object of class `model_fit`. +#' @param object A [model fit][model_fit]. #' @inheritParams predict.model_fit #' @rdname convert_helpers #' @keywords internal diff --git a/R/discrim_flexible.R b/R/discrim_flexible.R index 8b2826b1a..a0bbf5abd 100644 --- a/R/discrim_flexible.R +++ b/R/discrim_flexible.R @@ -51,7 +51,7 @@ discrim_flexible <- # ------------------------------------------------------------------------------ #' Update a model specification -#' @param object A model specification. +#' @param object A [model specification][model_spec]. #' @param ... Not used for `update()`. #' @param fresh A logical for whether the arguments should be #' modified in-place of or replaced wholesale. @@ -92,7 +92,7 @@ check_args.discrim_flexible <- function(object, call = rlang::caller_env()) { check_number_whole(args$prod_degree, min = 1, allow_null = TRUE, call = call, arg = "prod_degree") check_number_whole(args$num_terms, min = 1, allow_null = TRUE, call = call, arg = "num_terms") check_string(args$prune_method, allow_empty = FALSE, allow_null = TRUE, call = call, arg = "prune_method") - + invisible(object) } diff --git a/R/engines.R b/R/engines.R index f90c77ba3..5545e3cf6 100644 --- a/R/engines.R +++ b/R/engines.R @@ -82,7 +82,7 @@ load_libs <- function(x, quiet, attach = FALSE) { #' `set_engine("ranger", importance = "permutation")`. #' #' -#' @param object A model specification. +#' @param object A [model specification][model_spec]. #' @param engine A character string for the software that should #' be used to fit the model. This is highly dependent on the type #' of model (e.g. linear regression, random forest, etc.). diff --git a/R/extract.R b/R/extract.R index 040682b6c..70e14d75b 100644 --- a/R/extract.R +++ b/R/extract.R @@ -4,7 +4,7 @@ #' These functions extract various elements from a parsnip object. If they do #' not exist yet, an error is thrown. #' -#' - `extract_spec_parsnip()` returns the parsnip model specification. +#' - `extract_spec_parsnip()` returns the parsnip [model specification][model_spec]. #' #' - `extract_fit_engine()` returns the engine specific fit embedded within #' a parsnip model fit. For example, when using [parsnip::linear_reg()] diff --git a/R/fit.R b/R/fit.R index 3f9c7a8ee..6f67f2cae 100644 --- a/R/fit.R +++ b/R/fit.R @@ -116,7 +116,9 @@ fit.model_spec <- ... ) { if (object$mode == "unknown") { - cli::cli_abort("Please set the mode in the model specification.") + cli::cli_abort( + "Please set the mode in the {.help [model specification](parsnip::model_spec)}." + ) } control <- condense_control(control, control_parsnip()) check_case_weights(case_weights, object) @@ -249,7 +251,10 @@ fit_xy.model_spec <- ... ) { if (object$mode == "unknown") { - cli::cli_abort("Please set the mode in the model specification.") + cli::cli_abort( + "Please set the mode in the + {.help [model specification](parsnip::model_spec)}." + ) } if (inherits(object, "surv_reg")) { diff --git a/R/model_object_docs.R b/R/model_object_docs.R index c4175329d..e3d973d78 100644 --- a/R/model_object_docs.R +++ b/R/model_object_docs.R @@ -1,7 +1,32 @@ -#' Model Specification Information +#' Model Specifications #' +#' @description #' -#' An object with class "model_spec" is a container for +#' The parsnip package splits the process of fitting models into two steps: +#' +#' 1) Specify how a model will be fit using a _model specification_ +#' 2) Fit a model using the model specification +#' +#' This is a different approach to many other model interfaces in R, like `lm()`, +#' where both the specification of the model and the fitting happens in one +#' function call. Splitting the process into two steps allows users to +#' iteratively define model specifications throughout the model development +#' process. +#' +#' This intermediate object that defines how the model will be fit is called +#' a _model specification_ and has class `model_spec`. Model type functions, +#' like [linear_reg()] or [boost_tree()], return `model_spec` objects. +#' +#' Fitted model objects, resulting from passing a `model_spec` to +#' [fit()][fit.model_spec()] or [fit_xy][fit_xy.model_spec()], have +#' class `model_fit`, and contain the original `model_spec` objects inside +#' them. See [?model_fit][model_fit] for more on that object type, and +#' [?extract_spec_parsnip][extract_spec_parsnip.model_fit] to +#' extract `model_spec`s from `model_fit`s. +#' +#' @details +#' +#' An object with class `"model_spec"` is a container for #' information about a model that will be fit. #' #' The main elements of the object are: @@ -140,10 +165,18 @@ #' @name model_spec NULL -#' Model Fit Object Information +#' Model Fit Objects +#' +#' @description +#' +#' Model fits are trained [model specifications][model_spec] that are +#' ready to [predict][predict.model_fit] on new data. Model fits have class +#' `model_fit` and, usually, a subclass referring to the engine +#' used to fit the model. #' +#' @details #' -#' An object with class "model_fit" is a container for +#' An object with class `"model_fit"` is a container for #' information about a model that has been fit to the data. #' #' The main elements of the object are: diff --git a/R/predict.R b/R/predict.R index c8e6b5788..4bc22bd42 100644 --- a/R/predict.R +++ b/R/predict.R @@ -4,7 +4,7 @@ #' `predict()` can be used for all types of models and uses the #' "type" argument for more specificity. #' -#' @param object An object of class `model_fit`. +#' @param object A [model fit][model_fit]. #' @param new_data A rectangular data object, such as a data frame. #' @param type A single character value or `NULL`. Possible values #' are `"numeric"`, `"class"`, `"prob"`, `"conf_int"`, `"pred_int"`, diff --git a/R/req_pkgs.R b/R/req_pkgs.R index 1fbf2c67a..5324b4778 100644 --- a/R/req_pkgs.R +++ b/R/req_pkgs.R @@ -3,7 +3,7 @@ #' @description #' `r lifecycle::badge("deprecated")` #' -#' @param x A model specification or fit. +#' @param x A [model specification][model_spec] or [fit][model_fit]. #' @param ... Not used. #' @return A character string of package names (if any). #' @details diff --git a/R/required_pkgs.R b/R/required_pkgs.R index 56b709284..5fb4eb4d1 100644 --- a/R/required_pkgs.R +++ b/R/required_pkgs.R @@ -1,6 +1,6 @@ #' Determine required packages for a model #' -#' @param x A model specification or fit. +#' @param x A [model specification][model_spec] or [fit][model_fit]. #' @param infra Should parsnip itself be included in the result? #' @param ... Not used. #' @return A character vector diff --git a/R/translate.R b/R/translate.R index 7a43a3827..eb2ef7158 100644 --- a/R/translate.R +++ b/R/translate.R @@ -1,10 +1,10 @@ #' Resolve a Model Specification for a Computational Engine #' -#' `translate()` will translate a model specification into a code +#' `translate()` will translate a [model specification][model_spec] into a code #' object that is specific to a particular engine (e.g. R package). #' It translates generic parameters to their counterparts. #' -#' @param x A model specification. +#' @param x A [model specification][model_spec]. #' @param engine The computational engine for the model (see `?set_engine`). #' @param ... Not currently used. #' @details diff --git a/R/update.R b/R/update.R index 4658a82e3..3dd890fb4 100644 --- a/R/update.R +++ b/R/update.R @@ -18,7 +18,7 @@ #' @inheritParams svm_linear #' @inheritParams svm_poly #' @inheritParams svm_rbf -#' @param object A model specification. +#' @param object A [model specification][model_spec]. #' @param parameters A 1-row tibble or named list with _main_ #' parameters to update. Use **either** `parameters` **or** the main arguments #' directly when updating. If the main arguments are used, diff --git a/man/augment.Rd b/man/augment.Rd index 100645abd..ce482cf91 100644 --- a/man/augment.Rd +++ b/man/augment.Rd @@ -7,7 +7,7 @@ \method{augment}{model_fit}(x, new_data, eval_time = NULL, ...) } \arguments{ -\item{x}{A \code{model_fit} object produced by \code{\link[=fit.model_spec]{fit.model_spec()}} or +\item{x}{A \link[=model_fit]{model fit} produced by \code{\link[=fit.model_spec]{fit.model_spec()}} or \code{\link[=fit_xy.model_spec]{fit_xy.model_spec()}}.} \item{new_data}{A data frame or matrix.} diff --git a/man/case_weights_allowed.Rd b/man/case_weights_allowed.Rd index 1c070b6b5..1a34a79ac 100644 --- a/man/case_weights_allowed.Rd +++ b/man/case_weights_allowed.Rd @@ -7,7 +7,7 @@ case_weights_allowed(spec) } \arguments{ -\item{spec}{A parsnip model specification.} +\item{spec}{A parsnip \link[=model_spec]{model specification}.} } \value{ A single logical. diff --git a/man/convert_helpers.Rd b/man/convert_helpers.Rd index 5b29bacb1..d25f830bd 100644 --- a/man/convert_helpers.Rd +++ b/man/convert_helpers.Rd @@ -57,7 +57,7 @@ should be returned as a \code{"matrix"} or a \code{"data.frame"}.} \item{remove_intercept}{A logical indicating whether to remove the intercept column after \code{model.matrix()} is finished.} -\item{object}{An object of class \code{model_fit}.} +\item{object}{A \link[=model_fit]{model fit}.} \item{new_data}{A rectangular data object, such as a data frame.} diff --git a/man/eval_args.Rd b/man/eval_args.Rd index b51234e95..59938117f 100644 --- a/man/eval_args.Rd +++ b/man/eval_args.Rd @@ -7,7 +7,7 @@ eval_args(spec, ...) } \arguments{ -\item{spec}{A model specification} +\item{spec}{A \link[=model_spec]{model specification}.} \item{...}{Not used.} } diff --git a/man/extract-parsnip.Rd b/man/extract-parsnip.Rd index 8d893114a..718942842 100644 --- a/man/extract-parsnip.Rd +++ b/man/extract-parsnip.Rd @@ -38,7 +38,7 @@ section. These functions extract various elements from a parsnip object. If they do not exist yet, an error is thrown. \itemize{ -\item \code{extract_spec_parsnip()} returns the parsnip model specification. +\item \code{extract_spec_parsnip()} returns the parsnip \link[=model_spec]{model specification}. \item \code{extract_fit_engine()} returns the engine specific fit embedded within a parsnip model fit. For example, when using \code{\link[=linear_reg]{linear_reg()}} with the \code{"lm"} engine, this returns the underlying \code{lm} object. diff --git a/man/model_fit.Rd b/man/model_fit.Rd index 46c89fe44..ffea26452 100644 --- a/man/model_fit.Rd +++ b/man/model_fit.Rd @@ -2,12 +2,17 @@ % Please edit documentation in R/model_object_docs.R \name{model_fit} \alias{model_fit} -\title{Model Fit Object Information} +\title{Model Fit Objects} \description{ -An object with class "model_fit" is a container for -information about a model that has been fit to the data. +Model fits are trained \link[=model_spec]{model specifications} that are +ready to \link[=predict.model_fit]{predict} on new data. Model fits have class +\code{model_fit} and, usually, a subclass referring to the engine +used to fit the model. } \details{ +An object with class \code{"model_fit"} is a container for +information about a model that has been fit to the data. + The main elements of the object are: \itemize{ \item \code{lvl}: A vector of factor levels when the outcome is diff --git a/man/model_spec.Rd b/man/model_spec.Rd index 3345eb89e..6d5faeeed 100644 --- a/man/model_spec.Rd +++ b/man/model_spec.Rd @@ -2,12 +2,35 @@ % Please edit documentation in R/model_object_docs.R \name{model_spec} \alias{model_spec} -\title{Model Specification Information} +\title{Model Specifications} \description{ -An object with class "model_spec" is a container for -information about a model that will be fit. +The parsnip package splits the process of fitting models into two steps: +\enumerate{ +\item Specify how a model will be fit using a \emph{model specification} +\item Fit a model using the model specification +} + +This is a different approach to many other model interfaces in R, like \code{lm()}, +where both the specification of the model and the fitting happens in one +function call. Splitting the process into two steps allows users to +iteratively define model specifications throughout the model development +process. + +This intermediate object that defines how the model will be fit is called +a \emph{model specification} and has class \code{model_spec}. Model type functions, +like \code{\link[=linear_reg]{linear_reg()}} or \code{\link[=boost_tree]{boost_tree()}}, return \code{model_spec} objects. + +Fitted model objects, resulting from passing a \code{model_spec} to +\link[=fit.model_spec]{fit()} or \link[=fit_xy.model_spec]{fit_xy}, have +class \code{model_fit}, and contain the original \code{model_spec} objects inside +them. See \link[=model_fit]{?model_fit} for more on that object type, and +\link[=extract_spec_parsnip.model_fit]{?extract_spec_parsnip} to +extract \code{model_spec}s from \code{model_fit}s. } \details{ +An object with class \code{"model_spec"} is a container for +information about a model that will be fit. + The main elements of the object are: \itemize{ \item \code{args}: A vector of the main arguments for the model. The diff --git a/man/multi_predict.Rd b/man/multi_predict.Rd index ccdaad916..34f3856f8 100644 --- a/man/multi_predict.Rd +++ b/man/multi_predict.Rd @@ -38,7 +38,7 @@ multi_predict(object, ...) \method{multi_predict}{`_train.kknn`}(object, new_data, type = NULL, neighbors = NULL, ...) } \arguments{ -\item{object}{A \code{model_fit} object.} +\item{object}{A \link[=model_fit]{model fit}.} \item{...}{Optional arguments to pass to \code{predict.model_fit(type = "raw")} such as \code{type}.} diff --git a/man/other_predict.Rd b/man/other_predict.Rd index 6c997e28d..efe0fbdb0 100644 --- a/man/other_predict.Rd +++ b/man/other_predict.Rd @@ -72,7 +72,7 @@ predict_survival(object, ...) predict_time(object, ...) } \arguments{ -\item{object}{An object of class \code{model_fit}.} +\item{object}{A \link[=model_fit]{model fit}.} \item{new_data}{A rectangular data object, such as a data frame.} diff --git a/man/parsnip_update.Rd b/man/parsnip_update.Rd index 51961a67f..922e717c2 100644 --- a/man/parsnip_update.Rd +++ b/man/parsnip_update.Rd @@ -312,7 +312,7 @@ ) } \arguments{ -\item{object}{A model specification.} +\item{object}{A \link[=model_spec]{model specification}.} \item{parameters}{A 1-row tibble or named list with \emph{main} parameters to update. Use \strong{either} \code{parameters} \strong{or} the main arguments diff --git a/man/predict.model_fit.Rd b/man/predict.model_fit.Rd index eaf3364ef..1da83bd09 100644 --- a/man/predict.model_fit.Rd +++ b/man/predict.model_fit.Rd @@ -13,7 +13,7 @@ predict_raw(object, ...) } \arguments{ -\item{object}{An object of class \code{model_fit}.} +\item{object}{A \link[=model_fit]{model fit}.} \item{new_data}{A rectangular data object, such as a data frame.} diff --git a/man/req_pkgs.Rd b/man/req_pkgs.Rd index 9f7f439fa..79c16a8ea 100644 --- a/man/req_pkgs.Rd +++ b/man/req_pkgs.Rd @@ -7,7 +7,7 @@ req_pkgs(x, ...) } \arguments{ -\item{x}{A model specification or fit.} +\item{x}{A \link[=model_spec]{model specification} or \link[=model_fit]{fit}.} \item{...}{Not used.} } diff --git a/man/required_pkgs.model_spec.Rd b/man/required_pkgs.model_spec.Rd index 5e490b87e..2ef558b8c 100644 --- a/man/required_pkgs.model_spec.Rd +++ b/man/required_pkgs.model_spec.Rd @@ -10,7 +10,7 @@ \method{required_pkgs}{model_fit}(x, infra = TRUE, ...) } \arguments{ -\item{x}{A model specification or fit.} +\item{x}{A \link[=model_spec]{model specification} or \link[=model_fit]{fit}.} \item{infra}{Should parsnip itself be included in the result?} diff --git a/man/set_args.Rd b/man/set_args.Rd index 7e6c678d6..b8e4620c5 100644 --- a/man/set_args.Rd +++ b/man/set_args.Rd @@ -10,7 +10,7 @@ set_args(object, ...) set_mode(object, mode) } \arguments{ -\item{object}{A model specification.} +\item{object}{A \link[=model_spec]{model specification}.} \item{...}{One or more named model arguments.} diff --git a/man/set_engine.Rd b/man/set_engine.Rd index a032316db..07573b12f 100644 --- a/man/set_engine.Rd +++ b/man/set_engine.Rd @@ -7,7 +7,7 @@ set_engine(object, engine, ...) } \arguments{ -\item{object}{A model specification.} +\item{object}{A \link[=model_spec]{model specification}.} \item{engine}{A character string for the software that should be used to fit the model. This is highly dependent on the type diff --git a/man/translate.Rd b/man/translate.Rd index 7a8c9c61d..b181fe49e 100644 --- a/man/translate.Rd +++ b/man/translate.Rd @@ -10,14 +10,14 @@ translate(x, ...) \method{translate}{default}(x, engine = x$engine, ...) } \arguments{ -\item{x}{A model specification.} +\item{x}{A \link[=model_spec]{model specification}.} \item{...}{Not currently used.} \item{engine}{The computational engine for the model (see \code{?set_engine}).} } \description{ -\code{translate()} will translate a model specification into a code +\code{translate()} will translate a \link[=model_spec]{model specification} into a code object that is specific to a particular engine (e.g. R package). It translates generic parameters to their counterparts. } diff --git a/tests/testthat/_snaps/decision_tree.md b/tests/testthat/_snaps/decision_tree.md index 41bc8ae1a..539dbcbb3 100644 --- a/tests/testthat/_snaps/decision_tree.md +++ b/tests/testthat/_snaps/decision_tree.md @@ -21,11 +21,11 @@ --- - Please set the mode in the model specification. + Please set the mode in the model specification (`?parsnip::model_spec()`). --- - Please set the mode in the model specification. + Please set the mode in the model specification (`?parsnip::model_spec()`). --- diff --git a/tests/testthat/_snaps/fit_interfaces.md b/tests/testthat/_snaps/fit_interfaces.md index 41fc0d75f..15f131073 100644 --- a/tests/testthat/_snaps/fit_interfaces.md +++ b/tests/testthat/_snaps/fit_interfaces.md @@ -20,7 +20,7 @@ fit(mars_spec, am ~ ., data = mtcars) Condition Error in `fit()`: - ! Please set the mode in the model specification. + ! Please set the mode in the model specification (`?parsnip::model_spec()`). --- @@ -28,7 +28,7 @@ fit_xy(mars_spec, x = mtcars[, -1], y = mtcars[, 1]) Condition Error in `fit_xy()`: - ! Please set the mode in the model specification. + ! Please set the mode in the model specification (`?parsnip::model_spec()`). --- @@ -36,7 +36,7 @@ fit_xy(mars_spec, x = lending_club[, 1:2], y = lending_club$Class) Condition Error in `fit_xy()`: - ! Please set the mode in the model specification. + ! Please set the mode in the model specification (`?parsnip::model_spec()`). # misspecified formula argument