View source: R/boundary_compute.R
| boundary_compute | R Documentation |
Generates a 2D grid of predicted class labels (and optionally class probabilities)
for a fitted classbound model. The resulting boundary data is stored in the returned
object and consumed directly by plot_boundary().
boundary_compute(
model,
feature_range = NULL,
resolution = 100,
predfun = NULL,
projection = NULL,
reference = NULL,
...
)
model |
A |
feature_range |
A named list of length 2 specifying the axis ranges, e.g.,
|
resolution |
An integer >= 2 specifying the number of grid points per axis. Higher values produce smoother boundaries. Default is 100; use 50 for faster interactive exploration. |
predfun |
An optional custom prediction function for non-standard classifiers.
Must accept |
projection |
An optional named list defining a high-dimensional projection.
Must contain |
reference |
An optional named list of fixed reference values for features not
specified in |
... |
Additional arguments passed to |
When projection is NULL, boundary_compute() generates a regular grid over the
two features named in feature_range. If the model was trained on more than two
features, all remaining numeric features are fixed at their training-set median and
categorical features at their training-set mode, unless you supply explicit values
via reference.
This is a 2D slice of the full multivariate decision boundary. Two observations that appear in the same region may still be separated in a dimension that is held fixed. Use this mode when you want to examine how two specific features interact, or when your model was trained on exactly two features.
When projection is provided, the 2D grid is generated in the projected space and
then inverse-projected back to the original feature space before prediction.
This means the model uses all of its training features; only the visualization is
collapsed to two dimensions.
The projection $basis must be a numeric matrix with:
row count equal to the number of training features, row names matching feature names
exactly two columns (the projected axes)
orthonormal columns (crossprod(basis) must equal diag(2))
Suitable bases can be obtained from prcomp() or the tourr package.
Pass a named list of classbound objects (all trained on the same features and class
levels) to compute boundaries for multiple models at once. The result contains a
model column, suitable for use with plot_boundary(facet_col = "model").
A modified classbound object with the additional class "classbound_boundary".
The boundary grid is stored in $boundary_data (columns: x, y, prediction,
and per-class probability columns when available). For multi-model input, also
contains a model column.
fit_model(), plot_boundary(), boundary_workflow_set()
library(palmerpenguins)
data(penguins)
peng_data <- na.omit(penguins[, c("species", "bill_length_mm", "bill_depth_mm")])
# Fit and compute 2D boundary with auto-detected ranges
m <- fit_model(peng_data, species ~ ., rpart::rpart)
m <- boundary_compute(m, resolution = 50)
head(m$boundary_data)
# Explicit axis ranges
m <- boundary_compute(m,
feature_range = list(bill_length_mm = c(30, 60), bill_depth_mm = c(10, 25)),
resolution = 100
)
# 3-feature model visualized as a 2D slice
peng3d <- na.omit(penguins[, c(
"species", "bill_length_mm",
"bill_depth_mm", "flipper_length_mm"
)])
m3d <- fit_model(peng3d, species ~ ., rpart::rpart)
m3d_slice <- boundary_compute(m3d,
feature_range = list(bill_length_mm = c(30, 60), bill_depth_mm = c(10, 25)),
resolution = 50
)
# 3-feature model visualized via PCA projection
feat_cols <- c("bill_length_mm", "bill_depth_mm", "flipper_length_mm")
pca <- prcomp(peng3d[, feat_cols], scale. = TRUE)
basis <- pca$rotation[, 1:2]
m3d_proj <- boundary_compute(m3d,
feature_range = list(PC1 = c(-4, 4), PC2 = c(-3, 3)),
resolution = 50,
projection = list(basis = basis, center = pca$center, scale = pca$scale)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.