21 abr 2012

R^2 Spectrum

We have seen in the previous post, how to calculate the correlation spectrum, but other simple way to show  how the bands correlate to the constituent of interest is to calculate R^2. This way we remove the negative part of the correlation spectrum.
Xmsc<-NIR_msc
Ymoi<-demo_raw$Moisture
cor_spec<-cor(Ymoi,Xmsc[,1:700])
rsq_spec<-(cor(Ymoi,Xmsc[,1:700]))^2
cov_spec<-cov(Ymoi,Xmsc[,1:700])*50
matplot(wave_nir,t(cor_spec),lty=1,pch="*",xlab="nm",ylab="log(1/R)")
matplot(wave_nir,t(rsq_spec),lty=1,pch="*",xlab="nm",ylab="log(1/R)")
matplot(wave_nir,t(cov_spec),lty=1,pch="*",xlab="nm",ylab="log(1/R)")
#We merge the R /R^2/Cov spectrum with the sample spectra treated with MSC.
cor_spec<-rbind(cor_spec,NIR_msc)
rsq_spec<-rbind(rsq_spec,NIR_msc)
cov_spec<-rbind(cov_spec,NIR_msc)
matplot(wave_nir,t(cor_spec),lty=1,pch="*",xlab="nm",ylab="log(1/R)")
matplot(wave_nir,t(rsq_spec),lty=1,pch="*",xlab="nm",ylab="log(1/R)")
matplot(wave_nir,t(cov_spec),lty=1,pch="*",xlab="nm",ylab="log(1/R)")

In order to see better the Covariance Spectrum, I multiplied by a factor,We can see how the covariance spectrum gives sharp bands an gives us a better idea where the variation due to moisture is.

18 abr 2012

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

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.
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="*",
+ 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="*",
+ 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="*",
+ xlab="data points",ylab="log(1/R)")


See also:
R^2 Spectrum

17 abr 2012

More Spectra patterns (1ª derivative)

In the case of the first derivative for the absortion band, the maximum becomes a cero crossing.
Using SG filters, we can calculate it with R, and to see, like in the last posts, the Corrgram matrix.
Corrgram for the first derivative for this band:
Let´s see the three corrgram patterns together: (MSC, 1ª derivative, 2ª derivative)

Math Spectra Patterns

I was working today with "R" to get more patterns with the Corrgram. In the demo raw spectra I wanted this time to look to a band as much Gaussian as possible. I select it and trim the spectra to that region treated with the MSC ("Multiple Scatter Correction").
After I decided to apply another Math treatment (SG filter 2ª derivative). With the 2ª derivative the peak maximum in MSC, change to peak minimum.
The idea of this post is to look to the patterns and understand this with the wavelengths correlation matrix:
With MSC Treatment:
With SG 2ª derivative:

13 abr 2012

CORRGRAM: Correlation Matrix (Wavelengths)

With the "Corrgram" package we can see patterns that can help us to recognize possible inter-correlations in a big matrix. This could be the case to see the correlation to every wavelength respect to all others. This way we can see the high correlation respect to their neighbors and between the different overtones.  
I have selected just 40 (in steps of 2nm) consecutives wavelengths in this case.