[R] rewrite of scatter.smooth to handle NAs

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Feb 10 23:32:45 CET 2005


You need evaluate deparse(substitute(x)) _before_ you change x.
so have

 	xlab; ylab

early in the body of your function.

However, NEWS in R-devel says


     o	scatter.smooth() and loess.smooth() now handle missing values
 	in their inputs.

so why reinvent that wheel?


On Thu, 10 Feb 2005, William Briggs wrote:

>
> 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?

Because it is documented that way!

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list