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

This time I´m going to use my own data to develop a model to predict some fatty acid in the solid fat (pork).
Samples had been analyzed in a NIT (Near Infrared Transmittance) instrument. The range of the wavelengths is from 850 to 1048 nm (100 data points). This area cover usually second overtones.
I am quite novice in R, and it takes some time (in my case) to have the data organize in R to starts working with it.
We have together the X matrix (NIT absorbance’s) with the Y matrix (constituents). First of all was to import the”txt” file and create a data frame.
The TXT file has been exported from the software of the instrument.
> NIT<-fat[,7:106]
> C16_0<-fat[,1]
> C16_1<-fat[,2]
> C18_0<-fat[,3]
> C18_1<-fat[,4]
> C18_2<-fat[,5]
> C18_3<-fat[,6]
> fattyac<-data.frame(C16_0=I(C16_0),C16_1=I(C16_1),C18_0=I(C18_0),
+ C18_1=I(C18_1),C18_2=I(C18_2),C18_3=I(C18_3),NIT=I(NIT))
> names(fattyac)
 “C16_0” “C16_1” “C18_0” “C18_1” “C18_2” “C18_3” “NIT”

At this point we are ready to see the raw spectra.
We can see a high scatter, so a remove scatter pretreatment is necessary. Let´s try the MSC.
First we have to prepare the data, if not we will get some errors:
> NITm<-as.matrix(NIT)
> fattyac<-data.frame(C16_0=I(C16_0),C16_1=I(C16_1),C18_0=I(C18_0),
+ C18_1=I(C18_1),C18_2=I(C18_2),C18_3=I(C18_3),NITm=I(NITm))
> library(ChemometricsWithR)
> fat_msc<-msc(fattyac\$NITm)
>  matplot(wavelengths,t(fat_msc),lty=1,
+ pch=21,xlab=”nm”,ylab=”abs”)