You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
measure extends tidymodels with
preprocessing steps for analytical measurement data such as
spectroscopy, chromatography, and other instrument-generated signals. It
provides a recipes-style interface
for common spectral preprocessing techniques.
measure helps you:
Convert measurement data from wide or long formats into an
internal representation
Preprocess spectra using techniques like smoothing, derivatives,
and normalization
Transform data back to wide or long format for modeling or
visualization
Handle multi-dimensional data like LC-DAD, EEM fluorescence, and
2D NMR with native nD support
Decompose complex signals using PARAFAC, Tucker, and MCR-ALS
methods
Installation
You can install the development version of measure from
GitHub:
rec<- recipe(water+fat+protein~., data=meats_long) |># Assign sample ID role (not used as predictor)
update_role(id, new_role="id") |># Convert long-format measurements to internal representation
step_measure_input_long(transmittance, location= vars(channel)) |># Apply Savitzky-Golay smoothing with first derivative
step_measure_savitzky_golay(window_side=5, differentiation_order=1) |># Standard Normal Variate normalization
step_measure_snv() |># Convert back to wide format for modeling
step_measure_output_wide(prefix="nir_")
Preparing and applying the recipe
# Prep learns any parameters from training dataprepped<- prep(rec)
#> Dropping 1 list column for wide output: channel# Bake applies the transformationsprocessed<- bake(prepped, new_data=NULL)
# Result is ready for modelingprocessed[1:5, 1:8]
#> # A tibble: 5 × 8#> id water fat protein nir_01 nir_02 nir_03 nir_04#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 1 60.5 22.5 16.7 -0.126 -0.110 -0.0928 -0.0745#> 2 2 46 40.1 13.5 0.0184 0.0381 0.0601 0.0841#> 3 3 71 8.4 20.5 0.105 0.114 0.125 0.136 #> 4 4 72.8 5.9 20.7 0.0716 0.0786 0.0871 0.0974#> 5 5 58.3 25.5 15.5 -0.132 -0.118 -0.101 -0.0817
Visualizing the preprocessing
# Get data at intermediate step (before output conversion)rec_for_viz<- recipe(water+fat+protein~., data=meats_long) |>
update_role(id, new_role="id") |>
step_measure_input_long(transmittance, location= vars(channel)) |>
step_measure_savitzky_golay(window_side=5, differentiation_order=1) |>
step_measure_snv()
processed_long<- bake(prep(rec_for_viz), new_data=NULL)
# Extract and plot a few spectra
library(tidyr)
library(dplyr)
plot_data<-processed_long|>
slice(1:10) |>
mutate(sample_id= row_number()) |>
unnest(.measures)
ggplot(plot_data, aes(x=location, y=value, group=sample_id, color=factor(sample_id))) +
geom_line(alpha=0.7) +
labs(
x="Channel",
y="Preprocessed Signal",
title="NIR Spectra After Preprocessing",
subtitle="Savitzky-Golay first derivative + SNV normalization",
color="Sample"
) +
theme_minimal() +
theme(legend.position="none")
Available Steps
Input/Output Steps
Step
Description
step_measure_input_wide()
Convert wide format (measurements in columns) to internal format
step_measure_input_long()
Convert long format (measurements in rows) to internal format
Detect peaks using prominence or derivative methods
step_measure_peaks_integrate()
Calculate peak areas
step_measure_peaks_filter()
Filter peaks by height, area, or count
step_measure_peaks_deconvolve()
Deconvolve overlapping peaks
step_measure_peaks_to_table()
Convert peaks to wide format for modeling
SEC/GPC Analysis
Step
Description
step_measure_mw_averages()
Calculate Mn, Mw, Mz, Mp, and dispersity
step_measure_mw_distribution()
Generate molecular weight distribution curve
step_measure_mw_fractions()
Calculate molecular weight fractions
Feature Engineering
Step
Description
step_measure_integrals()
Calculate integrated areas for specified regions
step_measure_ratios()
Calculate ratios between integrated regions
step_measure_moments()
Calculate statistical moments from spectra
step_measure_bin()
Reduce spectrum to fewer points via binning
Data Augmentation
Step
Description
step_measure_augment_noise()
Add random noise for training augmentation
step_measure_augment_shift()
Random x-axis shifts for shift invariance
step_measure_augment_scale()
Random intensity scaling
Drift & Batch Correction
Step/Function
Description
step_measure_drift_qc_loess()
QC-RLSC drift correction using LOESS
step_measure_drift_linear()
Linear drift correction
step_measure_drift_spline()
Spline-based drift correction
step_measure_qc_bracket()
QC bracketing interpolation
step_measure_batch_reference()
Reference-based batch correction
measure_detect_drift()
Detect significant drift in QC samples
Analytical Validation Functions
measure provides a comprehensive suite of functions for analytical
method validation, designed for compatibility with ICH Q2(R2), ISO
17025, and similar regulatory frameworks.
Calibration & Quantitation
Function
Description
measure_calibration_fit()
Fit weighted calibration curves (linear/quadratic)