Skip to content
RDocumentation: mcrals
  • AI Chat
  • Code
  • Report
  • Spinner

    Note that this notebook was automatically generated from an RDocumentation page. It depends on the package and the example code whether this code will run without errors. You may need to edit the code to make things work.

    if(!require('mdatools')) {
        install.packages('mdatools')
        library('mdatools')
    }
    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)
       )
    }
    
    # See bookdown tutorial for more details.