loading_peaks: Peaks of the Loadings of a PCA

View source: R/plot_loadings.R

loading_peaksR Documentation

Peaks of the Loadings of a PCA

Description

Finds the wavelengths that contribute most to the components of a PCA: the largest positive and negative local extrema of the loadings, or the largest maxima of the variance explained by wavelength, optionally matched to emission lines. plot_loadings() labels the same peaks.

Usage

loading_peaks(
  model,
  components = NULL,
  type = c("loadings", "contribution"),
  top = 10,
  lines = NULL,
  tol = 0.1,
  span = 5
)

Arguments

model

A stats::prcomp() fit or an object returned by robpca(), rospca() or macropca(). The variable names must be the wavelengths; otherwise, the variables are numbered.

components

The components to show (numbers). Default is the first three (or all of them, if fewer).

type

"loadings" (default) for one panel of loadings per component, or "contribution" for the variance of each wavelength explained by components.

top

The number of peaks labeled per component (per panel). Default is 10; use 0 for no labels.

lines

Optional line list returned by libs_lines(), to label the peaks with the emission lines they match.

tol

The largest distance, in nm, between a peak and a line it matches. Default is 0.1.

span

The half-width, in channels, of the window in which a peak is a local extremum. Default is 5.

Details

See plot_loadings() for the definitions of the peaks, of the explained variance, of the orientation of the components and of the line matching. A peak is flagged as part of a derivative shape (derivative = TRUE) when a local extremum of the opposite sign, at least half as large, lies within 2 * span channels on the same detector segment: this usually means a shift or a broadening of the line between spectra.

Value

A tibble with one row per peak, sorted by component and by decreasing absolute value, and columns component, variable (the column name), wavelength, value (the loading, or the explained variance), sign ("positive" or "negative") and, for loadings, derivative. With lines, it also has the columns species and line_wavelength of the matched line (NA when none is within tol) and candidates, all the lines within tol, from the nearest.

Author(s)

Christian L. Goueguel

See Also

plot_loadings(), libs_lines()

Examples

spectra_id <- forageLIBS |> dplyr::select(1:2) |> names()
minerals <- forageLIBS |> dplyr::select(3:14) |> names()
spectra <- forageLIBS |> dplyr::select(-dplyr::all_of(c(spectra_id, minerals)))
set.seed(1)
fit <- robpca(center(spectra))
loading_peaks(fit, top = 5)


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