| fit_model | R Documentation |
Fits a classification model and wraps it in a classbound object, which carries
the feature metadata needed by boundary_compute() and plot_boundary().
fit_model(data, formula, classifier, ...)
## Default S3 method:
fit_model(
data,
formula,
classifier,
interface = c("formula", "matrix", "custom"),
fit_args = list(),
...
)
## S3 method for class ''function''
fit_model(
data,
formula,
classifier,
interface = c("formula", "matrix", "custom"),
fit_args = list(),
...
)
## S3 method for class 'character'
fit_model(
data,
formula,
classifier,
interface = c("formula", "matrix", "custom"),
fit_args = list(),
...
)
data |
A data frame containing the training features and response variable.
All columns referenced in |
formula |
A formula specifying the response and predictors, e.g.,
|
classifier |
The classification function or model specification to use.
Pass a function (e.g., |
... |
Additional arguments passed to methods. |
interface |
A string specifying how to invoke the classifier: |
fit_args |
A named list of additional arguments forwarded to the classifier
during fitting (e.g., |
fit_model() supports three calling conventions via the interface argument:
"formula" (default): passes formula and data directly to the classifier.
Works for the vast majority of R classifiers (e.g., rpart::rpart, e1071::svm,
stats::lda).
"matrix": constructs a predictor matrix x and response vector y from
the formula, then calls classifier(x, y, ...). Required for classifiers whose
primary interface is matrix-based (e.g., randomForest::randomForest).
"custom": passes only fit_args to the classifier, giving you full control
over the call. Use this for non-standard APIs.
If classifier is a parsnip model specification (model_spec), a fitted
model_fit, or a tidymodels workflow, fit_model() dispatches to the
appropriate method automatically. The interface argument is not needed for these
objects.
fit_model() calls preprocess_data() internally to coerce response labels to
a factor, handle missing values, and extract feature metadata. Do not call
preprocess_data() manually before calling fit_model(); this will corrupt the
stored metadata.
A classbound object (a list of class "classbound") containing:
$fit: the raw fitted model object returned by the classifier
$metadata: a list with $features (names, types, ranges, imputation values)
and $class_levels (sorted character vector of class labels)
$boundary_data: NULL until boundary_compute() is called
boundary_compute(), predict_model(), classbound()
library(palmerpenguins)
data(penguins)
peng_data <- na.omit(penguins[, c("species", "bill_length_mm", "bill_depth_mm")])
# Formula interface (most classifiers)
m_rpart <- fit_model(peng_data, species ~ ., rpart::rpart)
# Matrix interface (randomForest)
m_rf <- fit_model(peng_data, species ~ ., randomForest::randomForest,
interface = "matrix"
)
# Additional fitting arguments via fit_args
m_rpart_cp <- fit_model(peng_data, species ~ ., rpart::rpart,
fit_args = list(control = rpart::rpart.control(cp = 0.001))
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.