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.
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.
See also
step_measure_derivative_gap() for gap derivatives,
step_measure_savitzky_golay() for smoothed derivatives
Other measure-preprocessing:
step_measure_absorbance(),
step_measure_calibrate_x(),
step_measure_calibrate_y(),
step_measure_derivative_gap(),
step_measure_emsc(),
step_measure_kubelka_munk(),
step_measure_log(),
step_measure_map(),
step_measure_msc(),
step_measure_normalize_istd(),
step_measure_osc(),
step_measure_ratio_reference(),
step_measure_snv(),
step_measure_subtract_blank(),
step_measure_subtract_reference(),
step_measure_transmittance()
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()