# "Correlation / Covariance" Spectrum (This time with "R")

[This article was first published on

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

**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.

I treat this matter with other software´s, and of course you can do the same with “R”.

Once I have the spectra of my samples with a math treatment, I want to draw a correlation spectrum to see which wavelengths have better correlation with the constituent of interest.

Once I have the spectra of my samples with a math treatment, I want to draw a correlation spectrum to see which wavelengths have better correlation with the constituent of interest.

In this example I want to see the correlation of the wavelengths treated with MSC (Multiple Scatter Correction) respect to the Moisture value of the Demo file, but only in the NIR range (1100 to 2498 nm = 700 data points).

>Xmsc<-demoNIR_msc$NIRmsc

>Ymoi<-demoNIR_msc$Moisture

>cor_spec<-cor(Ymoi,Xmsc[,1:700])

>matplot(wave_nir,t(cor_spec),lty=1,pch=”*”,

>Ymoi<-demoNIR_msc$Moisture

>cor_spec<-cor(Ymoi,Xmsc[,1:700])

>matplot(wave_nir,t(cor_spec),lty=1,pch=”*”,

+ xlab=”data points”,ylab=”log(1/R)”)

#We merge the correlation spectrum with

#the sample spectra treated with MSC.

>cor_spec1<-rbind(cor_spec,waveNIR_msc)

>matplot(wave_nir,t(cor_spec1),lty=1,pch=”*”,

>cor_spec1<-rbind(cor_spec,waveNIR_msc)

>matplot(wave_nir,t(cor_spec1),lty=1,pch=”*”,

+ xlab=”data points”,ylab=”log(1/R)”)

We can se the correlation plot between -1.00 and 1.00. We are interested in positive correlations, so it is easy to see where the bands for moisture are (combination band 1940nm aprox. and first overtone 1450 nm aprox.).

We can calculate also the covariance spectrum for moisture:

>cov_spec<-cov(Ymoi,Xmsc[,1:700])

>matplot(wave_nir,t(cov_spec),lty=1,pch=”*”,

>matplot(wave_nir,t(cov_spec),lty=1,pch=”*”,

+ xlab=”data points”,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.