18 oct 2021
17 oct 2021
Modelling complex spectral data (soil) with the resemble package (V)
Continuing with the vignette Modelling complex spectral data with the resemble package (Leonardo Ramirez-Lopez and Alexandre M.J.-C. Wadoux)
Continuing from last post: Once we have calculated the PC terms or components (11
in the case of the last PCA analysis using the method OPC), we define planes
defined by the combinations of two of those terms (for example: PC1-PC2,
PC2-PC3, PC1-PC3,…), and the training spectra is projected on the plane to get
the scores of every spectrum vs. each PC component. All those scores are kept
in a score matrix “T”. All the projections form a cloud that in the case of
just two terms would be a 2D cloud, making easy the interpretations of the
distances between every sample and the mean or their neighbors. But in the case
of more dimensions it is a multivariate cloud, making the visual inspection
more difficult, so we have to check the projections individually in 2D planes
or 3D planes.
Algorithms like the Mahalanobis distance to the mean or to the neighbors will help us to check if the sample can be an outlier, it has very close neighbors (so it is represented by samples in theory similar), or if the sample has not closer neighbors and is a good sample to improve the structure of the database and make it more robust.
Let´s see in the case of the previous code one of those score planes, the one formed by the PC1 and PC2 terms:
plot(pca_tr_opc$scores[,1],pca_tr_opc$scores[,2],ylim = c(min(pca_tr_opc$scores[,2]),
We can project the testing data on the same plane, getting the scores of
the samples:
pca_projected <- predict(pca_tr_opc, newdata = testing$spc_p)
par(new=TRUE)
plot(pca_projected[,1],pca_projected[,2], col = "red",ylim = c(min(pca_tr_opc$scores[,2]),
xlab=" ", ylab=" ")
plot_ly(T_training, x=~T_training[,1], y=~T_training[,2],
z=~T_training[,3], alpha = 0.7)
14 oct 2021
Modelling complex spectral data (soil) with the resemble package (IV)
Continuing with the vignette Modelling complex spectral data with the resemble package (Leonardo Ramirez-Lopez and Alexandre M.J.-C. Wadoux)
Now we will use the PCA with the method “opc” in order to find the optimal number of components, bases on the its rationale behind that if two spectra are close in the X space (near neighbors), their constituents values will be closer as well on its value, so the optimal number of components will be the one that makes minimum the RMSD (root mean square difference) between them.
For more details you can find more info from the developers of this algorithm : L. Ramirez-Lopez, Behrens, Schmidt, Stevens, et al. (2013)pca_tr_opc <- ortho_projection
Yr = training$Ciso,
method = "pca",
pc_selection = optimal_sel)
pca_tr_opc # to obtain details of the PCA calculations.
We specify a maximum
value of 40, and the “opc” method estimate that 11 is the best option. If we
plot it, we can see graphically the reason:
The vignette shows
an interesting code, that if you run it will get the XY plot of the reference Ciso
value (for every spectrum) and the reference Ciso value for its closer neighbor,
and we can se a high correlation what is really the idea behind the “opc”
method.
13 oct 2021
Modelling complex spectral data (soil) with the resemble package (III)
Continuing with the vignette Modelling complex spectral data with the resemble package (Leonardo Ramirez-Lopez and Alexandre M.J.-C. Wadoux)
(from previous post) - These 825 samples are divided in two sets, one for training (value equal 1 in the variable train) and another for testing (value = 0).count(train)
train n
0 207
1 618
Let´s create these two dataframes:
testing<- NIRsoil[NIRsoil$train == 0, ]
11 oct 2021
Modelling complex spectral data (soil) with the resemble package (II)
Continuing with the vignette Modelling complex spectral data with the resemble package (Leonardo Ramirez-Lopez and Alexandre M.J.-C. Wadoux), now it is time to see the predictor variables which are reflectance values of the soil samples acquired in a NIR (Near Infrared Reflectance) instrument in the range from 1100 to 2498 nm in two nm steps, so we have 700 data points. We prepare a vector with the wavelengths and we call it "wav" (same as the vignette).
wavs<-NIRsoil$spc %>% colnames() %>% as.numeric()
Now we can se to the raw spectra (spectra without any treatment):
matplot(x = wavs, y = t(NIRsoil$spc),ylab = "Absorbance", type = "l",
Let´s create a new vector considering the wavelength reduction:
new_wavs <- as.matrix(as.numeric(colnames(NIRsoil$spc_p)))
and plot the spectra to see their appearance:
matplot(x = new_wavs, y = t(NIRsoil$spc_p),xlab = "Wavelengths, nm",
ylab = "1st derivative",
type = "l", lty = 1, col = "#5177A133")
Now in the data frame "NIRsoil" we have two spectra matrices, the raw spectra (spc) and the spectra reduced and math treated with the SG first derivative (spc_p).
We can check the dimensions of these matrices:
names(NIRsoil)"Nt" "Ciso" "CEC" "train" "spc" "spc_p"
dim(NIRsoil$spc)
825 700
dim(NIRsoil$spc_p)
825 276
In the next post we will continue the preprocessing process and preparation of the data as the vignette suggest, trying to understand the different procedures to model, as better as possible, the soil spectral data.






