Skip to contents

step_measure_subtract_reference() creates a specification of a recipe step that subtracts or divides each spectrum by an external reference. This is a simpler version of step_measure_subtract_blank() that always uses an externally provided reference.

Usage

step_measure_subtract_reference(
  recipe,
  reference,
  method = "subtract",
  measures = NULL,
  role = NA,
  trained = FALSE,
  learned_ref = NULL,
  skip = FALSE,
  id = recipes::rand_id("measure_subtract_reference")
)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

reference

A required external reference spectrum. Can be:

  • A measure_tbl object with location and value columns

  • A numeric vector (must match the number of locations in data)

  • A data.frame with location and value columns (will be interpolated)

method

The correction method to apply:

  • "subtract" (default): Subtract the blank from each spectrum

  • "divide": Divide each spectrum by the blank

measures

An optional character vector of measure column names to process. If NULL (the default), all measure columns (columns with class measure_list) will be processed.

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.

learned_ref

A named list containing the validated reference values for each measure column. This is NULL until the step is trained.

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.

Details

This step applies a simple reference correction to each spectrum:

  • method = "subtract": result = sample - reference

  • method = "divide": result = sample / reference

Unlike step_measure_subtract_blank(), this step always requires an externally provided reference and does not support learning from training data.

Examples

library(recipes)

# Create a reference spectrum
ref_spectrum <- rep(1.0, 100)

rec <- recipe(water + fat + protein ~ ., data = meats_long) |>
  update_role(id, new_role = "id") |>
  step_measure_input_long(transmittance, location = vars(channel)) |>
  step_measure_subtract_reference(reference = ref_spectrum, method = "divide")