step_measure_qc_bracket() creates a specification of a recipe step
that corrects for drift using linear interpolation between bracketing
QC or reference samples. This is a simple, intuitive method where each
sample is corrected based on the two nearest QC samples.
Arguments
- recipe
A recipe object.
- ...
One or more selector functions to choose feature columns. For feature-level data, select the numeric response columns. For curve-level data with
.measures, leave empty to apply to all locations.- run_order_col
Name of the column containing run order (injection sequence). Must be numeric/integer.
- sample_type_col
Name of the column containing sample type.
- qc_type
Value(s) in
sample_type_colthat identify QC samples to use for drift modeling. Default is"qc".- apply_to
Which samples to apply correction to:
"all"(default): Correct all samples"unknown": Only correct unknown samples
- extrapolate
Logical. Should correction be extrapolated for samples before the first or after the last QC? Default is TRUE. If FALSE, those samples use the nearest QC's correction factor.
- min_qc
Minimum number of QC samples required. Default is 5.
- role
Not used by this step.
- trained
Logical indicating if the step has been trained.
- skip
Logical. Should the step be skipped when baking?
- id
Unique step identifier.
Details
How It Works
For each sample at run order t:
Find the nearest QC samples before (
t1) and after (t2)Calculate correction factors at
t1andt2(target / observed)Linearly interpolate the correction factor for
tApply the interpolated correction
This method is commonly used in clinical and bioanalytical laboratories where QC samples are injected at regular intervals throughout the run.
See also
Other drift-correction:
step_measure_drift_linear(),
step_measure_drift_qc_loess(),
step_measure_drift_spline()
Examples
library(recipes)
# Data with QC samples at regular intervals
data <- data.frame(
sample_id = paste0("S", 1:15),
sample_type = c("qc", rep("unknown", 4), "qc", rep("unknown", 4), "qc",
rep("unknown", 3), "qc"),
run_order = 1:15,
feature1 = c(100, 101, 103, 105, 107, 105, 107, 109, 111, 113,
110, 112, 114, 116, 115) # Drift pattern
)
rec <- recipe(~ ., data = data) |>
update_role(sample_id, new_role = "id") |>
step_measure_qc_bracket(feature1) |>
prep()
corrected <- bake(rec, new_data = NULL)