
Apply an Orthogonalization Filter to New Spectra
Source:R/filter_predict.R
predict.specproc_filter.RdCorrects 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()oro2pls().- 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
newdataare 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()withmethod = "wold"or"sjoblom", ando2pls(), remove the orthogonal components one at a time, because their weights refer to the deflated matrix.osc()withmethod = "fearn",direct_orthogonal(),direct_osc()andprojected_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.
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)
#> [1] 68 245
# applied to the calibration spectra, predict() gives the correction
all.equal(predict(fit, x[cal, ]), fit$correction)
#> [1] TRUE