Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is another way to pre-treat aspectra set with the SNV math-treatment
(Standard Normal Variate). You can see the other one in the post :

In this post, I use the R function “sweep“.

library(ChemometricsWithR)

#in a first step I calculate the average value
#of all the data points for every spectrum and
#subtract it to every data point of the
#spectrum using the function “colMeans”
#from the package “ChemometricsWhithR”
#the mean value for every spectrum is now cero.
NIR.1<-sweep(gasoline\$NIR,MARGIN=1,
+colMeans(t(gasoline\$NIR)),FUN=”-“)

#sd function calculates the SD for all the data
#points of every #spectrum.
#We divide now the value of every data point
#by the SD of all the values of that spectrum.
NIR.2<-sweep(NIR.1,MARGIN=1,
+sd(t(gasoline\$NIR)),FUN=”/”)

#Now the spectrum has a mean of cero and a SD of 1.
#Use matplot to plot the spectra.
matplot(wavelengths,t(NIR.2),type=”l”,lty=1,
+xlab=”nm”,ylab=”log 1/R”,
+main=”SNV Gasoline Spectra”,col=”blue”)

We have to take consider that in the Gasoline matrix, the rows are the
samples and the columns the wavelengths, so we have to transpose the matrix
for some calculations.

Gasoline is a data set included in the “pls” package. Is not a set to see the benefits
of the SNV math treatment (not enough scatter), but you can try
with other data sets as “yarn”.