step_bin_spectra() creates a specification of a recipe step that
replaces each group of width adjacent channels of the spectra by their
mean, with bin_spectra().
Usage
step_bin_spectra(
recipe,
...,
width = 3,
segments = TRUE,
role = "predictor",
trained = FALSE,
columns = NULL,
bins = NULL,
skip = FALSE,
id = recipes::rand_id("bin_spectra")
)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().- width
The number of adjacent channels averaged, a positive integer. Default is 3;
width = 1returns the spectra unchanged.- segments
A logical: bin the segments between gaps of the wavelength axis separately (
TRUE, default). Needs wavelengths as names.- role
The role of the binned columns. Default is
"predictor".- trained
A logical indicating whether the step has been trained.
- columns
The names of the selected columns, stored once the step has been trained.
- bins
The groups of channels and the names of the binned columns, set when the recipe is prepped. Not to be set by the user.
- 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 form one spectrum per row, and are replaced by the
fewer binned columns, named by their mean wavelength (see
bin_spectra()). The groups of channels are set from the column names
when the recipe is prepped; nothing is estimated from the training data.
The width can be tuned with tune::tune() (dials parameter
window_size()). tidy() returns the terms,
width and id.
Examples
if (rlang::is_installed("recipes")) {
data(forageLIBS)
# binning, normalization to the total area and gap-segment derivative
rec <- recipes::recipe(K ~ ., data = forageLIBS[-c(1:10, 12:14)]) |>
step_bin_spectra(recipes::all_predictors(), width = 3) |>
step_spectral_norm(recipes::all_predictors(), method = "area") |>
step_gap_derivative(recipes::all_predictors(), gap = 3, segment = 1) |>
recipes::prep()
dim(recipes::bake(rec, new_data = NULL))
}
#> [1] 368 2385
