Skip to contents

step_measure_derivative() creates a specification of a recipe step that computes derivatives using simple finite differences.

Usage

step_measure_derivative(
  recipe,
  order = 1L,
  measures = NULL,
  role = NA,
  trained = FALSE,
  skip = FALSE,
  id = recipes::rand_id("measure_derivative")
)

Arguments

recipe

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

order

The order of the derivative (1 or 2). Default is 1 (first derivative).

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

This step computes derivatives using forward finite differences:

$$\frac{dy}{dx} \approx \frac{y_{i+1} - y_i}{x_{i+1} - x_i}$$

For each derivative order, the spectrum length is reduced by 1.

  • First derivative: n-1 points

  • Second derivative: n-2 points

The location values are updated to the left point of each difference.

Note: For smoothed derivatives, consider using step_measure_savitzky_golay() with differentiation_order > 0 instead.

Examples

library(recipes)

# First derivative
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(order = 1) |>
  prep()

# Second derivative
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(order = 2) |>
  prep()