predict.specproc_filter: Apply an Orthogonalization Filter to New Spectra

predict.specproc_filterR Documentation

Apply an Orthogonalization Filter to New Spectra

Description

Corrects new spectra with a filter estimated by epo(), osc(), direct_orthogonal(), direct_osc(), projected_osc() or o2pls(). The new spectra are preprocessed with the centers and scales of the calibration data, and the components estimated on the calibration data are removed. Estimating the filter on calibration data and applying it to validation data with predict() avoids the optimistic bias that arises when a supervised filter is fitted to all the data before validation.

Usage

## S3 method for class 'specproc_epo'
predict(object, newdata, ...)

## S3 method for class 'specproc_osc'
predict(object, newdata, ...)

## S3 method for class 'specproc_direct_orthogonal'
predict(object, newdata, ...)

## S3 method for class 'specproc_direct_osc'
predict(object, newdata, ...)

## S3 method for class 'specproc_projected_osc'
predict(object, newdata, ...)

## S3 method for class 'o2pls'
predict(object, newdata, ...)

Arguments

object

A filter returned by epo(), osc(), direct_orthogonal(), direct_osc(), projected_osc() or o2pls().

newdata

A numeric matrix or data frame of new spectra, with the same variables as the calibration data. If both have column names, the columns of newdata are matched by name.

...

Not used.

Details

For epo() the spectra are not centered, and the correction is \textbf{X}_{new} - \textbf{X}_{new}\textbf{VV}^T. For the other methods, the corrected spectra are centered (and scaled, if scale = TRUE was used), like the correction component of the fitted object:

  • osc() with method = "wold" or "sjoblom", and o2pls(), remove the orthogonal components one at a time, because their weights refer to the deflated matrix.

  • osc() with method = "fearn", direct_orthogonal(), direct_osc() and projected_osc() remove all components at once: \textbf{X}_{new} - \textbf{X}_{new}\textbf{WP}^T.

Applied to the calibration spectra, predict() returns the correction component of the fitted object.

Value

A tibble of corrected spectra, with the same columns as newdata.

Author(s)

Christian L. Goueguel

See Also

The recipe steps step_osc() and related functions, which apply these filters within a tidymodels workflow.

Examples

data(forageLIBS)
spectra <- forageLIBS[-(1:14)]
wl <- as.numeric(names(spectra))
x <- spectra[wl > 760 & wl < 780]  # the K I resonance lines
cal <- 1:300
fit <- osc(x[cal, ], forageLIBS$K[cal], method = "fearn", ncomp = 2)
corrected <- predict(fit, x[-cal, ])
dim(corrected)

# applied to the calibration spectra, predict() gives the correction
all.equal(predict(fit, x[cal, ]), fit$correction)

specProc documentation built on Nov. 20, 2026, 5:11 p.m.