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.





 

No hay comentarios:

Publicar un comentario