SCRIPT for NIR "DEMO" Tutorial – 001

[This article was first published on NIR-Quimiometría, 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.

########### 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)”)

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

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)