sortLoadings.RdSort rows of hyperSpoec object by possition of maximum
value in rows (in other words, sort component spectra, a.k.a. loadings, by
possition of top peak) and do additional tasks:
If sp is provided, convert resulting matrix to corresponding
hyperSpec object by using function
decomposition.
If PCA.flip = TRUE and sp is provided, flip the
loadings of a component in respect with x axis if the mean of the
component's scores is negative:
(sign(mean(Scores_of_component_i)) < 0)
loadings and sp are used to calculate the scores.
sortLoadings(
loadings,
sp = NULL,
PCA.flip = FALSE,
sort = TRUE,
label.spc = "Comp. spectra",
wl.units = "nm"
)Components' spectra (loadings) as either a matrix or a
hyperSpec object.
hyperSpec object.
Logical. If TRUE, some components are flipped.
Set to TRUE if PCA loadings are used. Default PCA.flip = FALSE.
The flipping follows the rule:
$$loading_i * (-score_i) = (-loading_i) * score_i$$
where \(-loading_i\) represents the i-th flipped loading.
Logical. Indicates if returned componenst must be sorted.
If FALSE, only additional tasks are performed.
Default is TRUE.
The new label for the matrix of loadings spectra. Default is
"Comp. spectra".
The units of wavelength axis. Will be used as suffix to
values in columns cNames and PeakAt, if applicable.
Either matrix (if sp is not provided) or
hyperSpec object with prepreocessed (sorted, flipped, named, etc.)
loadings.
In case of hyperSpec object, 3 columns
(cNames and PeakAt, order.of.rows) are added/overwritten.
spectra (object of class hyperSpec)
which will be used to convert sorted loadings into
hyperSpec object.
Other component analysis / factorisation related functions in spHelper:
getScores(),
infoDim(),
plot_spDiff(),
qplot_infoDim(),
qplot_kAmp(),
qplot_kSp(),
qplot_spc(),
reconstructSp(),
unipeak(),
whichOutlier()
# Load data
library(spHelper)
data(Loadings2,package = "spHelper")
# ======================================================================
unsorted_loadings <- Loadings2[c(2,3,5,1,4),,]
unsorted_loadings # print unsorted
#> hyperSpec object
#> 5 spectra
#> 4 data columns
#> 501 data points / spectrum
sortLoadings(unsorted_loadings) # print sorted
#> hyperSpec object
#> 5 spectra
#> 4 data columns
#> 501 data points / spectrum
# ======================================================================
# Extract loadings as a matrix:
unsorted_loadings_matrix <- Loadings2[c(2,3,5,1,4),,][[]]
# returns a matrix -----
L1 <- sortLoadings(unsorted_loadings_matrix)
class(L1)
#> [1] "matrix" "array"
## [1] "matrix"
# returns a hyperSpec object -----
L2 <- sortLoadings(unsorted_loadings_matrix, Spectra2)
class(L2)
#> [1] "hyperSpec"
#> attr(,"package")
#> [1] "hyperSpec"
## [1] "hyperSpec"
# ======================================================================