27 sept 2022

GLOSOLAN Soil Spectroscopy Webinar #11 | Soil sample preparation for MIR...

New Quarto blog for NIR and Chemometrics

 

I have created a new blog with Quarto, to work in R and R-Studio with spectroscopy data available for Near Infrared . By the moment, a serial of posts are coming about Near Infrared Transmitance with Meat data. It is a good ocasion to start learning from the beginning to work with chemometrics in R with this type of spectra


https://jrcuesta.quarto.pub/nir-chemometrics/

20 sept 2022

NIT Spectroscopic Tutorial with Caret (part 5)

Working these days with the tecator data set traying to find the better statistics as possible for the cross validation, and checking after with the test set. I use algorithms to remove the scatter but at the moment not derivatives to enhance the resolution. I show the statistics results for the CV and the validation results for the test set.

Multiple Scatter Correction has at the moment the better statistics for protein, but still some combinations of Math treatments could be tryed.


This is the XY plot for the test set with the pls model using the train set math treated with MSC.



16 sept 2022

NIT Spectroscopic Tutorial with Caret (part 4)

Looking to the spectra of the previous post was clear that we must apply a pre-treatment to remove the baselines shifts. One way to do it is to treat every spectrum individually, calculating a linear model (absorbance values vs. wavelengths) and getting the values of the slope at every wavelength, after these values are subtracted from the absorption values of the spectrum getting the baseline corrected spectrum. We repeat the process for every spectrum.

This function is available in the package pracma, and its name is detrend. In case we use it for the spectra pre-process in Caret with “center” and “scale”, we change the view from this:

 


To this:

 


This way we can see more clearly the variation in the spectra that we can try to interpret some way. Also, with this new pre-treatment we can check if the PCA change some way, and better to find outliers.

One of the options for interpretation are the correlation spectrum (see the correlation of every wavelength with the parameter of interest). In this case we can see how fat is inverse correlated with protein and moisture, and there are some positive correlations between protein and moisture.


We can see this if we run the correlation plot for the parameters;
correlation_par <- cor(endpoints)
library(corrplot)
corrplot(correlation_par, method = "ellipse")




See the code in basic R for the correlation spectra:

cor_moi <- cor(endpoints_train[ , 1], 
               train_scaled_fit)
cor_fat <- cor(endpoints_train[ , 2], 
               train_scaled_fit)
cor_prot <- cor(endpoints_train[ , 3], 
                train_scaled_fit)

matplot(seq(850, 1048, by = 2), t(cor_moi),
        xlab = "Wavelengths", ylab = "Absorbance", 
        main = "Correlation Spectra", type = "l", 
        ylim = c(-1, 1))

par(new = TRUE)

matplot(seq(850, 1048, by = 2), t(cor_fat),
        xlab = " ", ylab = " ", 
        main = " ", type = "l", col = "red", 
        ylim = c(-1, 1))

par(new = TRUE)

matplot(seq(850, 1048, by = 2), t(cor_prot),
        xlab = " ", ylab = " ", 
        main = " ", type = "l", col = "blue", 
        ylim = c(-1, 1))


legend("topright",                    
       legend = c("Moisture", "Fat", "Protein"),
       col = c("black", "red", "blue"),
       lty = 1)