15 may 2012

Improving script_002: “Monitor”

I read in an article that Ian Cowe said that what normally chemometricians do is to look to the graphics, of course interpret those graphics. So I still go on trying to develop a function can help me to understand the graphics and all the statistics there are behind.
I add some more lines to the monitor function:
plot(x~y,main="X-Y plot",xlab="predicted",ylab="reference")
abline(0,1,col="blue")
abline(intercept,slope,col="red")
abline(intercept+(2.5*sep),slope,col="red",lty=4)
abline(intercept-(2.5*sep),slope,col="red",lty=4)
I can do the same for the residual plot.
There are two warning lines which advice if the residual exceeds 2,5*SEP value. That is the T value warning limit.
Another line can be add, called the T value action limit (3*SEP).
Graphics show the 0-1 abline (blue) and the calculated slope-intercept abline (red). Limits are with dashed red lines.
We can see that almost both lines red and blue are almost over-plotted in this case.

14 may 2012

Livestock Food Productivity: Farmeron Foundations #3

Farmeron is a company founded by Matija Kopic. Really an interesting company.


On Line: Reflectance Sample presentation

This is a way to present the sample to the instrument to work "on line" in reflectance mode. Diode Array detectors guarantee to acquire spectra every few seconds, and if the model is working properly all the production is under control.

10 may 2012

Should I adjust the Bias?

A bias or systematic error is quite common when monitoring predictions vs reference data. Anyway we must have certain control limits to decide if the Bias is significant or not.
Procedures (as for example ISO 12099 )give details about how to calculate the Bias Control Limits (BCL). The idea is a "T test" to calculate if the differences between the mean predicted values mean and the mean  reference values are significant different from cero.
This limits will be a certain percentage of the SEP (Standard Error of Prediction).
We can add all this calculations into R and improve the Monitor function to receive a warning if the adjustment of the Bias is or not necessary.
I record this video: Should I adjust the Bias? to see how fast is R to run these monitor, and how we can customize the plots the way we like.
Function can be expanded with more warnings, like (for example) if it is necessary to adjust the slope.

6 may 2012

Improving script_001: “Monitor”

After having a look to this video: http://www.screenr.com/UxH8 from rtwotutorials, and reading some tutorials, I decided to modified the script from the previous post: Practicing Script with “ R”: Monitor ,
 in order to make it more robust .
If there are NA values in our X and Y variables, the results for all the statistics will be NA (see also the video: http://www.screenr.com/loS8), that is not nice, so it´s better to write a warning and stop the analysis to check our data set.
So the new script is:
monitor3<-function(x,y){
 x1<-is.na(x)
 y1<-is.na(y)
 if(mean(x1|y1)>0){
         print("There are NA values in X or Y, remove these samples for calculation")
        }else{
 n<-length(y)
 res<-y-x
 par(mfrow=c(2,2))
 hist(res,col="blue")   
 plot(x~y,xlab="predicted",ylab="reference")
 abline(0,1,col="blue")
 l<-seq(1:n)
 plot(res~l,col=2)
 abline(h=0,col="blue")
 boxplot(x,y,col="green")
 {rmsep<-sqrt(sum((y-x)^2)/n)
 cat("RMSEP:",rmsep,"\n")}
 {(bias<-mean(res))
 cat("Bias :",bias,"\n")}
 {sep<-sd(res)
 cat("SEP  :",sep,"\n")}
 {r<-cor(x,y)
 cat("Corr :",r,"\n")}
 {rsq<-(r^2)
 cat("RSQ  :",rsq,"\n")}
 }
 }


After having some samples with NA values (not reference value for that sample or not predicted value) the output will be:
"There are NA values in X or Y, remove these samples for calculation"

If not it will continue with the calculations:

RMSEP: 0.4373108
Bias : 0.03814815
SEP  : 0.4439425
Corr : 0.4864254
RSQ  : 0.2366097
Possible improvements I think right now:

The function gives me the position of the samples with NA values, so location is easier
or
 that the function removes these samples and the calculations can be done without them.