From ripley at stats.ox.ac.uk  Thu May  8 23:28:03 2003
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu May  8 23:28:19 2003
Subject: [Rd] predict function (PR#2958)
In-Reply-To: <200305082047.h48KlT2b022422@pubhealth.ku.dk>
Message-ID: <Pine.LNX.4.44.0305082220220.26570-100000@gannet.stats>

Try

> model.frame(~1, data.frame(x = 1:5))
NULL data frame with 1 rows

The C code never considers that case (no variables).

Fixed in R-patched.

On Thu, 8 May 2003 mh.smith@niwa.co.nz wrote:

> Full_Name: Murray H Smith
> Version: 1.6.1
> OS: Windows
> Submission from: (NULL) (202.36.29.1)

Still in 1.7.0, BTW, but we do suggest you upgrade as scores of bugs have 
been fixed since 1.6.1.
 
> This is report is more of a matter of completeness rather than an outright bug.
> The predict function does not handle the prediction from the constant model
> appropriately. It also differs from Splus in this respect.
> 
> The length of the vector (or first dimension of the matrix, if type = "terms" is
> used) for the output from the predict function should equal the number of rows
> in newdata, whether or not the model is the constant model.
> 
> > predict(lm(y ~ 1, data = data.frame(y = rep(0:3, c(5,9,7,1)))), 
> +   newdata = data.frame(x = 1:5))  
> [1] 1.181818
> 
> >  predict(glm(y ~ 1, family = poisson, data = data.frame(y = 
> +   rep(0:3, c(5,9,7,1)))), newdata = data.frame(x = 1:5), type = "r")  
> [1] 1.181818
> 
> Since there are 5 rows in the newdata data.frame the result should be the vector
> of length 5.
> 
> [1] 1.181818  1.181818  1.181818  1.181818  1.181818
> 
> .
> 
> As an aside it might also be nice to also avoid having to deal with a special
> case by defaulting the model formula
> 
>     ~ poly(x, 0)
> 
> to
> 
>     ~ 1
> 
> with perhaps a warning rather than producing an error.

That's not so easy. The formula really is ~ 1 + poly(x, d), and there is 
no simple way to have a term which contributes nothing.  poly(x, 0) could 
be a zero-column matrix, but the subsequent code will not handle that.

It's much easier for you to handle redundant terms yourself.

-- 
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] predict function (PR#2958)
Prof Brian Ripley