28 mar. 2012

SCRIPT for NIR "DEMO" Tutorial - 001

########### RAW NIR Demo Data##################################
demo<-read.table("demo.txt",header=TRUE)
VIS<-demo[,6:355]
VIS<-as.matrix(VIS)
NIR<-demo[,356:1055]
NIR<-as.matrix(NIR)
ALL<-demo[,6:1055]
ALL<-as.matrix(ALL)
Protein<-demo[,1]
Fat<-demo[,2]
Ash<-demo[,3]
DM<-demo[,4]
Moisture<-demo[,5]
demo_raw<-data.frame(Protein=I(Protein),Fat=I(Fat),Ash=I(Ash),DM=I(DM),
+ Moisture=I(Moisture),VIS=I(VIS),NIR=I(NIR),ALL=I(ALL))
wave_vis<-seq(400,1098,by=2)
wave_nir<-seq(1100,2498,by=2)
wave_all<-seq(400,2498,by=2)
matplot(wave_vis,t(demo_raw$VIS),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
matplot(wave_nir,t(demo_raw$NIR),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
matplot(wave_all,t(demo_raw$ALL),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
########### MSC (Multiple Scatter Correction) ##########################
library(ChemometricsWithR)
VIS_msc<-msc(NIR)
VIS_msc<-as.matrix(VIS_msc)
NIR_msc<-msc(NIR)
NIR_msc<-as.matrix(NIR_msc)
ALL_msc<-msc(ALL)
ALL_msc<-as.matrix(ALL_msc)
demo_msc<-data.frame(Protein=I(Protein),Fat=I(Fat),Ash=I(Ash),DM=I(DM),
+ Moisture=I(Moisture),VIS=I(VIS_msc),NIR=I(NIR_msc),ALL=I(ALL_msc))
matplot(wave_vis,t(demo_msc$VIS),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
matplot(wave_nir,t(demo_msc$NIR),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
matplot(wave_all,t(demo_msc$ALL),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
########### SNV (Standard Normal Variate )####################################
tVIS<-t(VIS)
tNIR<-t(NIR)
tVISsnv<-scale(tVIS,center=TRUE,scale=TRUE)
tNIRsnv<-scale(tNIR,center=TRUE,scale=TRUE)
VIS_snv<-t(tVISsnv)
VIS_snv<-as.matrix(VIS_snv)
NIR_snv<-t(tNIRsnv)
NIR_snv<-as.matrix(NIR_snv)
tALL<-t(ALL)
tALLsnv<-scale(tALL,center=TRUE,scale=TRUE)
ALL_snv<-t(tALLsnv)
ALL_snv<-as.matrix(ALL_snv)
demo_snv<-data.frame(Protein=I(Protein),Fat=I(Fat),Ash=I(Ash),DM=I(DM),
+ Moisture=I(Moisture),VIS=I(VIS_snv),NIR=I(NIR_snv),ALL=I(ALL_snv))
matplot(wave_nir,t(demo_snv$NIR),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
matplot(wave_vis,t(demo_snv$VIS),lty=1,pch=".",xlab="nm",ylab="log(1/R)")
matplot(wave_all,t(demo_snv$ALL),lty=1,pch=".",xlab="nm",ylab="log(1/R)")

No hay comentarios:

Publicar un comentario