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)

No hay comentarios:

Publicar un comentario