This is a simple exercise. I start wit some expectra in the NIR zone, and firs I wan to run the PCA with the NIPALS algorithm from the package "Chemometrics".
The name of the spectra I´m going to use to develop the Principal Components Analysis is: sflw.msc3.tra$NIRmsc
Now with the NIPALS function:
The result fom names are two matrices: T (scores matrix) and P (loading matrix).I´m going to give them another name:
Which are the dimensions of these matrices?
 2800 10
 75 10
2800 is the number of wavelenths of the spectra
10 is the number of PCs
75 is the number of samples
We can call Xorig to the original matrix (75 rows, 2800 columns)
from our "sflw.msc3.tra$NIRmsc".
As we know: Xorig = Xrecon + E
beeing Xrecon = T * t(P)
E is the Residual Matrix.
In R we have to proceed like this:
Xorig is the original spectra centered.
So, the Error matrix is:
Now we can check that:
gives a TRUE response for all the values