| step_osc | R Documentation |
step_osc() creates a specification of a recipe step that removes the
variation of the selected predictors that is orthogonal to the outcome,
with osc(). The filter is estimated on the training data when the recipe
is prepped and applied to new data when it is baked.
step_osc(
recipe,
...,
role = NA,
trained = FALSE,
outcome = NULL,
method = "sjoblom",
num_comp = 2,
options = list(),
res = NULL,
columns = NULL,
skip = FALSE,
id = recipes::rand_id("osc")
)
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose the predictors (for
example, the spectral channels). See |
role |
Not used by this step, since no new variables are created. |
trained |
A logical indicating whether the step has been trained. |
outcome |
The outcome variable, as a bare name or a selector. If
|
method |
The OSC algorithm: |
num_comp |
The number of orthogonal components to remove. |
options |
A list of further arguments passed to the underlying
function, such as |
res |
The fitted filter, stored once the step has been trained. |
columns |
The names of the selected predictors, stored once the step has been trained. |
skip |
A logical. Should the step be skipped when the recipe is baked
by |
id |
A character string that is unique to this step. |
The selected columns are replaced by the corrected values, which are
centered (and scaled, if options = list(scale = TRUE)). Because the
filter uses the outcome, it must be estimated on training data only.
Within a workflows::workflow() and tune::tune_grid(), this happens
automatically for every resample. The outcome is not needed when new data
are baked.
The related steps step_direct_orthogonal(), step_direct_osc(),
step_projected_osc(), step_opls() and step_o2pls() (which also
handles several outcomes) remove response-orthogonal variation with other
algorithms. step_epo() and step_glsw() remove variation described by
external clutter, and step_y_gradient_glsw() down-weights variation
between samples with similar outcomes.
An updated version of recipe with the new step added to the
sequence of existing steps.
num_comp can be tuned with tune::tune(); its default range is
dials::num_comp() with values 1 to 4. When the model also has a
num_comp argument (for example, parsnip::pls()), give the step's
parameter its own id, such as num_comp = tune("filter_comp"), so that
the two can be tuned together:
rec <- recipe(K ~ ., data = spectra) |>
step_osc(all_predictors(), method = "fearn", num_comp = tune("filter_comp"))
model <- parsnip::pls(num_comp = tune()) |>
set_mode("regression") |>
set_engine("mixOmics", scale = FALSE) # center only, like pls::plsr()
wf <- workflow(rec, model)
tune_grid(wf, resamples = group_vfold_cv(spectra, group = Sample), grid = 9)
tidy() returns a tibble with columns terms (the
selected predictors), num_comp and id.
Christian L. Goueguel
osc(), predict.specproc_filter()
library(recipes)
data(forageLIBS)
# potassium and the K I resonance lines
wl <- suppressWarnings(as.numeric(names(forageLIBS)))
dat <- forageLIBS[c(which(names(forageLIBS) == "K"), which(wl > 760 & wl < 780))]
rec <- recipe(K ~ ., data = dat[1:300, ]) |>
step_osc(all_predictors(), method = "fearn", num_comp = 2)
prepped <- prep(rec)
tidy(prepped, number = 1)
# new spectra are corrected with the filter estimated on the training data
bake(prepped, new_data = dat[301:368, -1])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.