[R] rewrite of scatter.smooth to handle NAs

William Briggs wib2004 at med.cornell.edu
Thu Feb 10 22:29:12 CET 2005


I rewrote scatter.smooth to handle missing values, but I have a question 
about a move I had to make.  Here's the code:

Mscatter.smooth<-function (x, y, span = 2/3, degree = 1, family = 
c("symmetric",
     "gaussian"), xlab = deparse(substitute(x)), ylab = 
deparse(substitute(y)),
     ylim = range(y, prediction$y), evaluation = 50, ...)
{
     if (inherits(x, "formula")) {
         if (length(x) < 3)
             stop("need response in formula")
         thiscall <- match.call()
         thiscall$x <- x[[3]]
         thiscall$y <- x[[2]]
         return(invisible(eval(thiscall, sys.parent())))
     }
##################
     plot(x, y, xlab = xlab, ylab = ylab, ...)
	i<-complete.cases(x,y)
	x<-x[i]
	y<-y[i]
##################
     prediction <- loess.smooth(x, y, span, degree, family, evaluation)
     lines(prediction)
     invisible()
}

The changes I made are between the '###########'.  The idea was only to 
pass complete cases on to loess.smooth and thus avoice the NA error 
you'd usually get by calling scatter.smooth with missing values.

I had to move the plot above the loess.smooth, whereas in the orginal 
scatter.smooth is was just below to take adavantage of prediction$y to 
set the ylim in the plotting function.

But if I try to plot after creating the complete cases, the names of the 
x and y variables disappear and instead the plotting labels on the x and 
y axis look like, for example, 'c(1,2,1.2,5.2, ...)', that is, the 
vector of values being plotted.

Why does that happen?

Matt
-- 
William (Matt) Briggs
Assistant Professor of Biostatistics
Division of General Internal Medicine
Weill Medical College of Cornell University
525 East 68th St., Box #46
New York, NY 10021
Tel: 212-628-0128; Fax: 212-746-8965

http://drclamp.wmbriggs.com




More information about the R-help mailing list