21 jun 2015

Scores and reconstruction

While looking to the first lesson of the algebra course of MIT, I capture this screen shot because it explain, in an easy way, the reconstruction of a spectrum.
The x and y would be the scores of the spectrum (0,3) , and we have to find them, knowing that the 
first loading is (2,-1), and the second (-1,2), so  x and y will be the solutions of the equation:
2x - y = 0
-x + 2y = 3
In this case there is a solution,  and the residual is cero, but in the case of real spectra there are more variables than two and we try to fit as best as possible the solution to the unknown with the linear combinations of the loadings multiplied by the scores, and the residual is the residual vector e.

The loading matrix “P” : a good example of orthogonal matrix

We know that for an orthogonal matrix A:

At.A=A.At=I

When we calculate the loading matrix during the PCA process, each loading is orthogonal (perpendicular) to all others. So we can check for fun in R, Excel,…., this condition with the loading matrix.

P is a very large matrix, so we will check it with just a few columns (6 loadings or terms) and the same number of files (6 wavelengths):
 
> round(gas.loadings[1:6,1:6],digits=4)
          PC1   PC2   PC3    PC4   PC5    PC6
900 nm -0.011 0.022 0.034 -0.039 0.042 -0.020
902 nm -0.010 0.022 0.031 -0.041 0.039 -0.022
904 nm -0.011 0.022 0.030 -0.042 0.036 -0.021
906 nm -0.012 0.024 0.027 -0.045 0.031 -0.012
908 nm -0.013 0.021 0.025 -0.045 0.035 -0.013
910 nm -0.014 0.023 0.023 -0.046 0.036 -0.018
 
Pt is the transpose, so the columns are the wavelengths and the files the loadings:
 
> round(t(gas.loadings[1:6,1:6]),digits=4)
    900 nm 902 nm 904 nm 906 nm 908 nm 910 nm
PC1 -0.011 -0.010 -0.011 -0.012 -0.013 -0.014
PC2  0.022  0.022  0.022  0.024  0.021  0.023
PC3  0.034  0.031  0.030  0.027  0.025  0.023
PC4 -0.039 -0.041 -0.042 -0.045 -0.045 -0.046
PC5  0.042  0.039  0.036  0.031  0.035  0.036
PC6 -0.020 -0.022 -0.021 -0.012 -0.013 -0.018

Now we multiply the two matrix:

> round((gas.loadings[1:6,1:6])%*% solve((gas.loadings[1:6,1:6])),digits=4)
       900 nm 902 nm 904 nm 906 nm 908 nm 910 nm
900 nm      1      0      0      0      0      0
902 nm      0      1      0      0      0      0
904 nm      0      0      1      0      0      0
906 nm      0      0      0      1      0      0
908 nm      0      0      0      0      1      0
910 nm      0      0      0      0      0      1
 

> round(((solve(gas.loadings[1:6,1:6]))%*%(gas.loadings[1:6,1:6])),digits=4)
    PC1 PC2 PC3 PC4 PC5 PC6
PC1   1   0   0   0   0   0
PC2   0   1   0   0   0   0
PC3   0   0   1   0   0   0
PC4   0   0   0   1   0   0
PC5   0   0   0   0   1   0
PC6   0   0   0   0   0   1

14 jun 2015

Studing structure in LOCAL for validation

One way to understand the structure of the spectra population is to order the database of spectra by the constituent of interest and select different groups (in this case eight). One group is keeping it for validation and the others for calibration, so in the case of the figure I use “group_6” for validation and all the rest for calibration.

I continue with all possible combinations.

 It is the same that “cross validation”, but in this case I use the LOCAL algorithm. The resulting statistics (RSQ , SEP, ….), help me to understand if the calibration will perform as it should in routine and to fine outliers.

It was very useful to improve the performance of a calibration for Process Analysis, where GLOBAL, did not perform well, and LOCAL seems to be better.

In Process we can´t expect very nice statistics, but not so high RSQ can help you to see tendencies, and take decisions immediately.

27 may 2015

NIR used to study Mixtures (Part 1)

When developing a mixture of ingredients we use a to live some time to be sure that the mixture is fine and if we divide the mixed batch into several subsamples, the spectra of all the subsamples are similar each other in a certain level. If we see differences, we consider that the mixture is not completed and we can continue mixing. There is a point at a certain time where the mixture cannot be improve and after that point it can became worse to certain levels. We must be sure that we finish the mixture as closer to that point as possible.
If we get a library of spectra with those characteristics we can keep it and compare new batches in order to check if the spectrum at that time match with the spectra from the library.
In a mixture is also important that the quantities of every ingredient is correct, maybe we miss to add an ingredient, we add it in the wrong proportions,  or by mistake an ingredient which does not form part of the formula. Why not to detect even a cross contaminant, or other issues.
If we develop a model based on good spectra, and prepare a realistic cutoff we can check all the batches and detect if the PASS or FAIL the Identification or Qualification Test.
There can be cases where we can know what the problem was looking to the spectra. One simple example is to add to a mixture an ingredient which should not be there... let´s suppose "Dextrose", and I acquire the spectrum of this mixture, after I subtract this spectrum from the average spectrum of all the good spectra in the library (let´s say the ideal spectrum)…..you will see how the difference spectrum is the Dextrose itself, or very similar with a high correlation to the samples in the Dextrose product.
You can see also this with more complex techniques as Principal Component Analysisn (PCAs), where we have a reconstructed spectrum and a residual spectrum as well. Can we see any features in the residual spectrum which tell us what is the ingredient or ingredients not explained by the model? Maybe is the spectra of an adulterant, wrong ingredient added by mistake,...

20 may 2015

MD Looking to the subsamples statistics



Normally when scanning ungrounded samples like wheat, barley, corn in a NIR instrument, we use a large cup which moves across the sample presentation plate, in some cases continuously and in others stopping and scanning several times. Finally we get the average results, but there are some software’s which give you also the subsample results, giving information of the standard deviation between subsamples which is a good indicator for homogeneity or other issues. It can give you also the Mahalanobis distance for each subsample.

Suppose the case of a large cup where we acquire 32 scans. The cup stop in 8 different positions to scan the sample, so we have 8 subsamples with 4 scans each. Lower numbers of scans mean a noisier spectrum, so the Mahalanobis distance will be normally higher in all the subsamples (independently) than the Mahalanobis distance of the 32 scans average spectrum.

We have build our score matrix with 32 subscans average spectra, so if we compre spectra of 4 subscans average spectra it is normal we get out of range MD values.