Skip to contents

step_measure_peaks_detect() creates a specification of a recipe step that detects peaks in measurement data and stores them in a new .peaks column.

Usage

step_measure_peaks_detect(
  recipe,
  method = c("prominence", "derivative"),
  min_height = 0,
  min_distance = 0,
  min_prominence = 0,
  snr_threshold = FALSE,
  measures = NULL,
  role = NA,
  trained = FALSE,
  skip = FALSE,
  id = recipes::rand_id("measure_peaks_detect")
)

Arguments

recipe

A recipe object.

method

Peak detection method. One of "prominence" (default) or "derivative".

min_height

Minimum peak height. If snr_threshold = TRUE, this is interpreted as a signal-to-noise ratio threshold.

min_distance

Minimum distance between peaks in x-axis units.

min_prominence

Minimum peak prominence (only for method = "prominence").

snr_threshold

Logical. If TRUE, min_height is interpreted as a signal-to-noise ratio. Noise is estimated as the MAD of the signal.

measures

Optional character vector of measure column names.

role

Not used.

trained

Logical indicating if the step has been trained.

skip

Logical. Should the step be skipped when baking?

id

Unique step identifier.

Value

An updated recipe with the new step added.

Details

This step detects peaks in measurement data and creates a new .peaks column containing the detected peaks for each sample. The original .measures column is preserved.

Detection methods:

  • "prominence": Finds local maxima and calculates their prominence (how much a peak stands out from surrounding signal). More robust to noise.

  • "derivative": Finds peaks by detecting zero-crossings in the first derivative. Faster but more sensitive to noise.

Peak properties stored:

  • peak_id: Integer identifier

  • location: X-axis position of peak apex

  • height: Y-value at peak apex

  • left_base, right_base: X-axis positions of peak boundaries

  • area: Initially NA; use step_measure_peaks_integrate() to calculate

Examples

library(recipes)

rec <- recipe(water + fat + protein ~ ., data = meats_long) |>
  update_role(id, new_role = "id") |>
  step_measure_input_long(transmittance, location = vars(channel)) |>
  step_measure_peaks_detect(min_height = 0.5, min_distance = 5) |>
  prep()

result <- bake(rec, new_data = NULL)
# Result now has .peaks column alongside .measures