step_lincomp {MachineShop} | R Documentation |
Creates a specification of a recipe step that will compute one or more linear combinations of a set of numeric variables according to a user-specified transformation matrix.
step_lincomp( recipe, ..., transform, num_comp = 5, options = list(), center = TRUE, scale = TRUE, replace = TRUE, prefix = "LinComp", role = "predictor", skip = FALSE, id = recipes::rand_id("lincomp") ) ## S3 method for class 'step_lincomp' tidy(x, ...) tunable.step_lincomp(x, ...)
recipe |
recipe object to which the step will be added. |
... |
one or more selector functions to choose which variables will be
used to compute the components. See |
transform |
function whose first argument |
num_comp |
number of components to derive. The value of |
options |
list of elements to be added to the step object for use in the
|
center, scale |
logicals indicating whether to mean center and standard deviation scale the original variables prior to deriving components, or functions or names of functions for the centering and scaling. |
replace |
logical indicating whether to replace the original variables. |
prefix |
character string prefix added to a sequence of zero-padded integers to generate names for the resulting new variables. |
role |
analysis role that added step variables should be assigned. By default, they are designated as model predictors. |
skip |
logical indicating whether to skip the step when the recipe is
baked. While all operations are baked when |
id |
unique character string to identify the step. |
x |
|
An updated version of recipe
with the new step added to the
sequence of existing steps (if any). For the tidy
method, a tibble
with columns terms
(selectors or variables selected), weight
of each variable in the linear transformations, and name
of the new
variable names.
library(recipes) pca_mat <- function(x, step) { prcomp(x)$rotation[, 1:step$num_comp, drop = FALSE] } rec <- recipe(rating ~ ., data = attitude) lincomp_rec <- rec %>% step_lincomp(all_numeric(), -all_outcomes(), transform = pca_mat, num_comp = 3, prefix = "PCA") lincomp_prep <- prep(lincomp_rec, training = attitude) lincomp_data <- bake(lincomp_prep, attitude) pairs(lincomp_data, lower.panel = NULL) tidy(lincomp_rec, number = 1) tidy(lincomp_prep, number = 1)