Site icon R-bloggers

Standard Normal Variate (SNV: Other way)

[This article was first published on NIR-Quimiometria, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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 : < o:p>
“Standard Normal Variate (SNV)”.< o:p>

In this post, I use the R function “sweep“.< o:p>

library(ChemometricsWithR)< o:p>

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

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

#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,< o:p>
+xlab=”nm”,ylab=”log 1/R”,< o:p>
+main=”SNV Gasoline Spectra”,col=”blue”)< o:p>

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

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

To leave a comment for the author, please follow the link and comment on their blog: NIR-Quimiometria.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.