# Improving script_001: “Monitor”

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

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

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

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.