# \donttest{
library(mdatools)
# resolve mixture of carbonhydrates Raman spectra
data(carbs)
# define constraints for contributions
cc <- list(
constraint("nonneg")
)
# define constraints for spectra
cs <- list(
constraint("nonneg"),
constraint("norm", params = list(type = "area"))
)
# because by default initial approximation is made by using random numbers
# we need to seed the generator in order to get reproducable results
set.seed(6)
# run ALS
m <- mcrals(carbs$D, ncomp = 3, cont.constraints = cc, spec.constraints = cs)
summary(m)
# plot cumulative and individual explained variance
par(mfrow = c(1, 2))
plotVariance(m)
plotCumVariance(m)
# plot resolved spectra (all of them or individually)
par(mfrow = c(2, 1))
plotSpectra(m)
plotSpectra(m, comp = 2:3)
# plot resolved contributions (all of them or individually)
par(mfrow = c(2, 1))
plotContributions(m)
plotContributions(m, comp = 2:3)
# of course you can do this manually as well, e.g. show original
# and resolved spectra
par(mfrow = c(1, 1))
mdaplotg(
list(
"original" = prep.norm(carbs$D, "area"),
"resolved" = prep.norm(mda.subset(mda.t(m$resspec), 1), "area")
), col = c("gray", "red"), type = "l"
)
# in case if you have reference spectra of components you can compare them with
# the resolved ones:
par(mfrow = c(3, 1))
for (i in 1:3) {
mdaplotg(
list(
"pure" = prep.norm(mda.subset(mda.t(carbs$S), 1), "area"),
"resolved" = prep.norm(mda.subset(mda.t(m$resspec), 1), "area")
), col = c("gray", "red"), type = "l", lwd = c(3, 1)
)
}
# This example shows how to force some of the contribution values
# First of all we combine the matrix with mixtures and the pure spectra, so the pure
# spectra are on top of the combined matrix
Dplus <- mda.rbind(mda.t(carbs$S), carbs$D)
# since we know that concentration of C2 and C3 is zero in the first row (it is a pure
# spectrum of first component), we can force them to be zero in the optimization procedure.
# Similarly we can do this for second and third rows.
cont.forced <- matrix(NA, nrow(Dplus), 3)
cont.forced[1, ] <- c(NA, 0, 0)
cont.forced[2, ] <- c(0, NA, 0)
cont.forced[3, ] <- c(0, 0, NA)
m <- mcrals(Dplus, 3, cont.forced = cont.forced, cont.constraints = cc, spec.constraints = cs)
plot(m)
# See bookdown tutorial for more details.
# }
Run the code above in your browser using DataLab