Custom Baseline Correction with User-Provided Function
Source:R/baseline-custom.R
step_measure_baseline_custom.Rdstep_measure_baseline_custom() creates a specification of a recipe step
that applies a user-provided function for baseline correction. This allows
for flexible, custom baseline estimation algorithms.
Usage
step_measure_baseline_custom(
recipe,
.fn,
...,
subtract = TRUE,
measures = NULL,
tunable = NULL,
role = NA,
trained = FALSE,
skip = FALSE,
id = recipes::rand_id("measure_baseline_custom")
)Arguments
- recipe
A recipe object. The step will be added to the sequence of operations for this recipe.
- .fn
A function or formula for baseline estimation. The function should accept a
measure_tbl(tibble withlocationandvaluecolumns) and return a numeric vector of baseline values with the same length as the input. Formulas are converted to functions viarlang::as_function(), where.xrepresents themeasure_tbl.- ...
Additional arguments passed to
.fn. These are captured as quosures and evaluated at bake time. For formulas, named arguments are available as names in the formula body. For functions, they are passed as ordinary function arguments.- subtract
If
TRUE(default), the baseline is subtracted from the signal. IfFALSE, the baseline values replace the original values (useful for extracting baselines).- measures
An optional character vector of measure column names to process. If
NULL(the default), all measure columns (columns with classmeasure_list) will be processed.- tunable
An optional named list specifying which arguments in
...are tunable. Each element should be a list withpkg,fun, and optionallyrange. Each name must match exactly one named argument in..., andpkgandfunmust be non-empty character strings. Fixed unnamed arguments are allowed but cannot be declared tunable.- role
Not used by this step since no new variables are created.
- trained
A logical to indicate if the quantities for preprocessing have been estimated.
- skip
A logical. Should the step be skipped when the recipe is baked?
- id
A character string that is unique to this step to identify it.
Value
An updated version of recipe with the new step added to the
sequence of any existing operations.
Details
This step allows you to use any baseline estimation algorithm by providing
a custom function. The function receives a measure_tbl object (a tibble
with location and value columns) and should return a numeric vector
of the estimated baseline values.
Function Contract
Your function should:
Accept a
measure_tblas its first argumentReturn a numeric vector of the same length as
nrow(measure_tbl)Handle NA values appropriately
Formula Interface
You can use a formula instead of a function. The formula is converted to a
function where .x represents the measure_tbl:
# These are equivalent:
step_measure_baseline_custom(.fn = function(x) mean(x$value))
step_measure_baseline_custom(.fn = ~ mean(.x$value))Tunability
To make parameters tunable with dials, provide a tunable argument:
step_measure_baseline_custom(
.fn = ~ {
fit <- stats::lm(.x$value ~ poly(.x$location, degree))
stats::predict(fit)
},
degree = tune::tune(),
tunable = list(
degree = list(
pkg = "dials",
fun = "degree_int",
range = c(1L, 5L)
)
)
)In a recipe, tune::extract_parameter_set_dials() discovers this argument.
Finalize it with tune::finalize_recipe() before calling recipes::prep().
No selectors should be supplied to this step function. The data should be
in the internal format produced by step_measure_input_wide() or
step_measure_input_long().
Tidying
When you tidy() this step, a tibble with columns
terms, subtract, and id is returned.
See also
step_measure_baseline_als(), step_measure_baseline_poly() for
built-in baseline correction methods.
Other measure-baseline:
step_measure_baseline_airpls(),
step_measure_baseline_als(),
step_measure_baseline_arpls(),
step_measure_baseline_aspls(),
step_measure_baseline_auto(),
step_measure_baseline_fastchrom(),
step_measure_baseline_gpc(),
step_measure_baseline_iarpls(),
step_measure_baseline_minima(),
step_measure_baseline_morph(),
step_measure_baseline_morphological(),
step_measure_baseline_poly(),
step_measure_baseline_py(),
step_measure_baseline_rf(),
step_measure_baseline_rolling(),
step_measure_baseline_snip(),
step_measure_baseline_tophat(),
step_measure_detrend()
Examples
library(recipes)
# Simple polynomial baseline using a function
poly_baseline <- function(x) {
fit <- lm(x$value ~ poly(x$location, 2))
predict(fit)
}
rec <- recipe(water + fat + protein ~ ., data = meats_long) |>
update_role(id, new_role = "id") |>
step_measure_input_long(transmittance, location = vars(channel)) |>
step_measure_baseline_custom(.fn = poly_baseline) |>
prep()
bake(rec, new_data = NULL)
#> # A tibble: 215 × 6
#> id water fat protein .measures channel
#> <int> <dbl> <dbl> <dbl> <meas> <list>
#> 1 1 60.5 22.5 16.7 [100 × 2] <int [100]>
#> 2 2 46 40.1 13.5 [100 × 2] <int [100]>
#> 3 3 71 8.4 20.5 [100 × 2] <int [100]>
#> 4 4 72.8 5.9 20.7 [100 × 2] <int [100]>
#> 5 5 58.3 25.5 15.5 [100 × 2] <int [100]>
#> 6 6 44 42.7 13.7 [100 × 2] <int [100]>
#> 7 7 44 42.7 13.7 [100 × 2] <int [100]>
#> 8 8 69.3 10.6 19.3 [100 × 2] <int [100]>
#> 9 9 61.4 19.9 17.7 [100 × 2] <int [100]>
#> 10 10 61.4 19.9 17.7 [100 × 2] <int [100]>
#> # ℹ 205 more rows
# Using formula interface with additional parameters
rec2 <- recipe(water + fat + protein ~ ., data = meats_long) |>
update_role(id, new_role = "id") |>
step_measure_input_long(transmittance, location = vars(channel)) |>
step_measure_baseline_custom(
.fn = ~ stats::loess(.x$value ~ .x$location, span = span)$fitted,
span = 0.5
) |>
prep()