Skip to contents

step_measure_derivative_gap() creates a specification of a recipe step that computes gap derivatives using the Norris-Williams method.

Usage

step_measure_derivative_gap(
  recipe,
  gap = 2L,
  segment = 1L,
  measures = NULL,
  role = NA,
  trained = FALSE,
  skip = FALSE,
  id = recipes::rand_id("measure_derivative_gap")
)

Arguments

recipe

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

gap

The gap size (number of points to skip on each side). Default is 2. The derivative at point i is computed from points i-gap and i+gap.

segment

The segment size for averaging. Default is 1 (no averaging). When greater than 1, multiple points are averaged on each side before computing the difference.

measures

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

role

Not used by this step since no new variables are created.

trained

A logical to indicate if the step has been trained.

skip

A logical. Should the step be skipped when baking?

id

A character string that is unique to this step.

Value

An updated version of recipe with the new step added.

Details

Gap derivatives compute the difference between points separated by a gap:

$$\frac{dy}{dx} \approx \frac{y_{i+g} - y_{i-g}}{x_{i+g} - x_{i-g}}$$

where \(g\) is the gap size.

When segment > 1, the Norris-Williams method is used, which averages segment points on each side before computing the difference.

The spectrum length is reduced by 2 * gap points.

Gap derivatives are often used in NIR chemometrics as an alternative to Savitzky-Golay derivatives when less smoothing is desired.

Examples

library(recipes)

# Gap derivative with gap=2
rec <- recipe(water + fat + protein ~ ., data = meats_long) |>
  update_role(id, new_role = "id") |>
  step_measure_input_long(transmittance, location = vars(channel)) |>
  step_measure_derivative_gap(gap = 2) |>
  prep()

# Norris-Williams with gap=3, segment=2
rec2 <- recipe(water + fat + protein ~ ., data = meats_long) |>
  update_role(id, new_role = "id") |>
  step_measure_input_long(transmittance, location = vars(channel)) |>
  step_measure_derivative_gap(gap = 3, segment = 2) |>
  prep()