View source: R/recipe_steps_preprocessing.R
| step_baseline | R Documentation |
step_baseline() creates a specification of a recipe step that
subtracts a fitted baseline from each spectrum, with baseline_arpls(),
baseline_als() or baseline_lsp().
step_baseline(
recipe,
...,
role = NA,
trained = FALSE,
method = "arpls",
lambda = 1000,
degree = 4,
options = list(),
columns = NULL,
skip = FALSE,
id = recipes::rand_id("baseline")
)
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. |
method |
The baseline algorithm: |
lambda |
The smoothing parameter of the |
degree |
The polynomial degree of the |
options |
A list of further arguments passed to the baseline
function: |
columns |
The names of the selected columns, 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, in the order in which they appear in the data, form one spectrum per row, so select all the channels of the spectrum and only them. The baseline is fitted to each spectrum separately, so nothing is estimated from the training data and the step gives the same result whether it is applied before or after the data are split. The selected columns are replaced by the corrected values.
An updated version of recipe with the new step added to the
sequence of existing steps.
lambda (methods "arpls" and "als") can be tuned with tune::tune(),
using baseline_lambda(), and degree (method "lsp") using
dials::degree_int() with values 1 to 8.
tidy() returns a tibble with columns terms,
method and id.
Christian L. Goueguel
baseline_arpls(), baseline_als(), baseline_lsp()
library(recipes)
data(forageLIBS)
spectra <- forageLIBS[c(2, 15:406)] # sample id and spectral channels
rec <- recipe(~ ., data = spectra[1:16, ]) |>
update_role(Sample, new_role = "id") |>
step_baseline(all_predictors(), method = "arpls", lambda = 1e5) |>
step_snv(all_predictors())
prepped <- prep(rec)
bake(prepped, new_data = spectra[17:24, ])[, 1:6]
# five spectra between 240 and 300 nm, before and after the baseline correction
wl <- suppressWarnings(as.numeric(names(forageLIBS)))
region <- forageLIBS[1:5, c(2, which(wl > 240 & wl < 300))]
corrected <- recipe(~ ., data = region) |>
update_role(Sample, new_role = "id") |>
step_baseline(all_predictors(), method = "arpls", lambda = 1e5) |>
prep() |>
bake(new_data = NULL)
plot_spectra(region, id = Sample) +
ggplot2::coord_cartesian(ylim = c(500, 3000))
plot_spectra(corrected, id = Sample) +
ggplot2::coord_cartesian(ylim = c(-200, 2500))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.