step_measure_log() creates a specification of a recipe step that applies
a logarithmic transformation to measurement values.
Arguments
- recipe
A recipe object. The step will be added to the sequence of operations for this recipe.
- base
The base of the logarithm. Default is
exp(1)(natural log). Use10for log10 transformation.- offset
A numeric offset added to values before taking the log. Default is
0. Use a small positive value (e.g.,1forlog1p) to handle zero or near-zero values.- 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 applies the transformation:
$$y' = \log_b(y + \text{offset})$$
where \(b\) is the base.
Log transformation is commonly used for:
Variance stabilization
Normalizing skewed distributions
Converting multiplicative relationships to additive
Warning: Non-positive values (after offset) will produce -Inf or NaN.
The measurement locations are preserved unchanged.
See also
Other measure-preprocessing:
step_measure_absorbance(),
step_measure_calibrate_x(),
step_measure_calibrate_y(),
step_measure_derivative(),
step_measure_derivative_gap(),
step_measure_emsc(),
step_measure_kubelka_munk(),
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)
# Natural log transformation
rec <- recipe(water + fat + protein ~ ., data = meats_long) |>
update_role(id, new_role = "id") |>
step_measure_input_long(transmittance, location = vars(channel)) |>
step_measure_log(offset = 1) |>
prep()
# Log10 transformation
rec2 <- recipe(water + fat + protein ~ ., data = meats_long) |>
update_role(id, new_role = "id") |>
step_measure_input_long(transmittance, location = vars(channel)) |>
step_measure_log(base = 10) |>
prep()