9 feb. 2014

Using R: NIPALS Spectra Reconstruction

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".
library(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:
sflw.msc3.tra_nipals<-nipals(sflw.msc3.tra$NIRmsc,a=10,it=160)
names(sflw.msc3.tra_nipals)


The result fom names are two matrices: T (scores matrix) and P (loading matrix).I´m going to give them another name:
T_nipals<-sflw.msc3.tra_nipals$T
P_nipals<-sflw.msc3.tra_nipals$P

Which are the dimensions of these matrices?
> dim(P_nipals)
[1] 2800   10

> dim(T_nipals)
[1] 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:
Xrecon<-T_nipals%*%(t(P_nipals))
Xorig<-scale(sflw.msc3.tra$NIRmsc,center=TRUE,scale=FALSE)
Xorig is the original spectra centered.

So, the Error matrix is:
E<-Xorig-Xrecon

Now we can check that:
Xorig==Xrecon+E
gives a TRUE response for all the values

2 comentarios:

  1. Congrats Jose Ramón!!! Two very interesting posts about NIPALS function with R software. I worked with the NIPALS function in Pirouette software and it is a very useful tool to extract internal information of the data set.

    ResponderEliminar
  2. Thanks Antonio.
    I really apreciate your comments.

    ResponderEliminar