step_direct_osc() creates a specification of a recipe step that
removes response-orthogonal variation from the selected predictors with
direct_osc().
Arguments
- 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
recipes::selections().- 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
NULL(default), the single outcome of the recipe is used.- num_comp
The number of orthogonal components to remove.
- options
A list of further arguments passed to
direct_osc(), such asscaleortol.- 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
recipes::bake()? Keep the defaultFALSE, so that new data are corrected with the same filter as the training data.- id
A character string that is unique to this step.
Details
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.
Examples
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 <- recipes::recipe(K ~ ., data = dat[1:300, ]) |>
step_direct_osc(recipes::all_predictors(), num_comp = 2)
prepped <- recipes::prep(rec)
recipes::bake(prepped, new_data = dat[301:368, ])
#> # A tibble: 68 × 246
#> `760.0161416` `760.1001689` `760.1841961` `760.2682233` `760.3522506`
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1.77 -24.5 -45.6 -45.5 -50.3
#> 2 -25.9 -110. -76.7 -71.0 -79.9
#> 3 11.5 46.3 71.5 19.3 6.38
#> 4 -26.4 -17.1 17.7 -27.9 -35.6
#> 5 -69.1 -51.7 -18.3 20.5 15.6
#> 6 64.9 48.7 54.5 37.5 2.97
#> 7 -2.95 22.3 14.8 -18.1 -40.8
#> 8 14.0 -51.6 -52.8 -37.3 -66.7
#> 9 -20.9 -32.1 -25.2 2.06 -20.8
#> 10 -35.2 -21.5 -48.9 -15.9 -4.34
#> # ℹ 58 more rows
#> # ℹ 241 more variables: `760.4362778` <dbl>, `760.520305` <dbl>,
#> # `760.6043323` <dbl>, `760.6883595` <dbl>, `760.7723867` <dbl>,
#> # `760.856414` <dbl>, `760.9404412` <dbl>, `761.0244684` <dbl>,
#> # `761.1084957` <dbl>, `761.1925229` <dbl>, `761.2765501` <dbl>,
#> # `761.3605774` <dbl>, `761.4446046` <dbl>, `761.5286319` <dbl>,
#> # `761.6126591` <dbl>, `761.6966863` <dbl>, `761.7807136` <dbl>, …
