From ripley at stats.ox.ac.uk  Mon Mar  8 13:46:16 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon Mar  8 13:46:17 2004
Subject: [Rd] Bug in points.formula (PR#6652)
In-Reply-To: <20040308114357.384B0EDF2@slim.kubism.ku.dk>
Message-ID: <Pine.LNX.4.44.0403081235330.6205-100000@gannet.stats>

What I think we need is the number of rows of the model frame *before
subsetting*, so that any further arguments (and there are none such in
Martin's example) of the same length as the response get subsetted.

Berwin's fix is ugly, and I think it can be avoided by using model.frame
to do the work by manipulating ... rather carefully so e.g. col gets read 
from the model frame and hence subsetted -- that would also allow col to 
be written in terms of other variables in the model frame.

I need to spend more time thinking about this in detail.

Brian

On Mon, 8 Mar 2004 maechler@stat.math.ethz.ch wrote:

> >>>>> "BeT" == Berwin A Turlach <berwin@maths.uwa.edu.au>
> >>>>>     on Mon,  8 Mar 2004 09:17:51 +0100 (CET) writes:
> 
>     BeT> Dear all, I noticed the following bug in points.formula
> 
>     <...>
>     BeT> Error in if (length(x) == l) x[s] else x : argument is
>     BeT> of length zero
> 
> Thank you, Berwin.
> The bug is still present in current "R-1.9.0 alpha"
> and can be demonstrated with the slightly simpler code
> 
> roller <-
>     data.frame(weight = c(1.9, 3.1, 3.3, 4.8, 5.3, 6.1, 6.4, 7.6, 9.8, 12.4),
>                depression = c(2, 1, 5, 5, 20, 20, 23, 10, 30, 25))
> plot( depression ~ weight, data=roller, type="n")
> with(roller, points( depression~weight, subset=1:7 )) # ok
> with(roller, points( depression~weight, subset=8:10)) # ok
> with(roller, points( depression~weight, subset=8:10, col=2))## --> Error
> 
>     BeT> This seems to be due to the fact that the data used was
>     BeT> attached before the call and not specified in the call
>     BeT> via the "data" argument. 
> 
> Yes, using "data =" doesn't show the problem,
> but as you see, the problem also shows for the more recommended
> way of using with() instead of attach()..
> 
>     BeT> The following patch seems to fix the problem.
> 
> *** /home/berwin/lang/R/Courses/3S6/plot.R.orig	Tue Sep  2 23:43:42 2003
> --- /home/berwin/lang/R/Courses/3S6/plot.R	Mon Mar  8 16:06:32 2004
> ***************
> *** 312,318 ****
>       mf <- eval(m, parent.frame())
>       if (!missing(subset)) {
>   	s <- eval(m$subset, data, parent.frame())
> ! 	l <- nrow(data)
>   	dosub <- function(x) if (length(x) == l) x[s] else x
>   	dots <- lapply(dots, dosub)
>       }
> --- 312,320 ----
>       mf <- eval(m, parent.frame())
>       if (!missing(subset)) {
>   	s <- eval(m$subset, data, parent.frame())
> !         mtmp <- m
> !         mtmp$subset <- NULL
> ! 	l <- nrow(eval(mtmp, parent.frame()))
>   	dosub <- function(x) if (length(x) == l) x[s] else x
>   	dots <- lapply(dots, dosub)
>       }
> 
> -------------------
> 
> Yes, but the resulting code becomes even more ugly.
> doing a second  eval(m, ..) just with a slightly modified 'm'...
> 
> Though I'll have to leave it to others to find if there is a
> better patch...
> 
> Thanks again, Berwin!
> Martin
> 
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
> 
> 

-- 
Brian D. Ripley,                  ripley@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


[Rd] Bug in points.formula (PR#6652)
Prof Brian Ripley