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
Lazy evaluation. See V&R's S Programming for a good explanation. But, in brief, the default values for xlab and ylab are not evaluated until they are needed. If plot(..., xlab=...) appears after the assignments to x and y, then x and y are no longer the original argument expressions but numeric vectors. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of William Briggs > Sent: Thursday, February 10, 2005 1:29 PM > To: r-help at stat.math.ethz.ch > Subject: [R] rewrite of scatter.smooth to handle NAs > > > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
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