Skip to contents

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().

Usage

step_baseline(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  method = "arpls",
  lambda = 1000,
  degree = 4,
  options = list(),
  columns = NULL,
  skip = FALSE,
  id = recipes::rand_id("baseline")
)

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.

method

The baseline algorithm: "arpls" (default), "als" or "lsp".

lambda

The smoothing parameter of the "arpls" and "als" methods. Default is 1000.

degree

The polynomial degree of the "lsp" method. Default is 4.

options

A list of further arguments passed to the baseline function: ratio and max.iter for baseline_arpls(), p and max.iter for baseline_als(), tol and max.iter for baseline_lsp().

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 recipes::bake()? Keep the default FALSE, so that new data are corrected with the same filter as the training data.

id

A character string that is unique to this step.

Value

An updated version of recipe with the new step added to the sequence of existing steps.

Details

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.

Tuning

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.

Tidying

tidy() returns a tibble with columns terms, method and id.

Author

Christian L. Goueguel

Examples

library(recipes)
#> Loading required package: dplyr
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
#> 
#> Attaching package: ‘recipes’
#> The following object is masked from ‘package:stats’:
#> 
#>     step
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]
#> # A tibble: 8 × 6
#>   Sample   `199.3771616` `199.4644141` `199.5516666` `199.6389192` `199.7261717`
#>   <chr>            <dbl>         <dbl>         <dbl>         <dbl>         <dbl>
#> 1 FEN6585…        -0.363        -0.370        0.213       -0.327          -0.322
#> 2 FEN6585…        -0.329        -0.454        0.198       -0.164          -0.355
#> 3 FEN6585…        -0.396        -0.269        0.320        0.00691        -0.143
#> 4 FEN6585…        -0.296        -0.273        0.0430      -0.258          -0.277
#> 5 FEN6585…        -0.270        -0.374        0.189       -0.316          -0.351
#> 6 FEN6585…        -0.517        -0.544        0.499       -0.361          -0.184
#> 7 FEN6585…        -0.512        -0.211        0.422       -0.275          -0.220
#> 8 FEN6585…        -0.611        -0.528        0.620       -0.183          -0.552

# 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))