[R] predict.lm with (logical) NA vector

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Nov 10 09:44:53 CET 2003


On Mon, 10 Nov 2003, Edzer J. Pebesma wrote:

> I was surprised by the following (R 1.8.0):
> 
> R> lm.fit = lm(y~x, data.frame(x=1:10, y=1:10))
> R> predict(lm.fit, data.frame(x = rep(NA, 10)))
>              1              2              3              4              5
> -1.060998e-314 -1.060998e-314 -1.060998e-314 -1.060998e-314 -1.060998e-314
>              6              7              8              9             10
>   0.000000e+00  1.406440e-269  6.715118e-265  4.940656e-323  1.782528e-265
> R> predict(lm.fit, data.frame(x = as.numeric(rep(NA, 10))))
>  1  2  3  4  5  6  7  8  9 10
> NA NA NA NA NA NA NA NA NA NA
> 
> shouldn't the first predict() call return NA's, or else issue an error 
> message?

The prediction methods do not in general check that new variables you give 
are of the correct type: the type used in the fit is not recorded in the 
model object.  In this case a logical column will `work' provided it has 
two values (even with NAs).  We can probably trap this exact case, but 
there will remain a lot of scope for user error.

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