[R] lm(), na.exclude and predict()

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Aug 9 08:38:56 CEST 2003


On Fri, 8 Aug 2003, Nurnberg-LaZerte wrote:

> I'm trying to get predict.lm() to return an NA for each NA row in it's
> input vector, so the output is the same length as the input.

Don't call predict.lm directly, please.  Use predict().

> I thought that using na.action=na.exclude with lm() would do that. But
> apparently not ??

That tells R to fit with na.exclude, not to predict with na.exclude.
Prediction uses a separate setting of na.action.

> df <- data.frame(x=c(NA,1,2,3,NA),y=c(0,2,3,4,0))
> tl <- lm(y~x,df,na.action=na.exclude)
> predict.lm(tl,data.frame(x=c(2.5,NA,3,4,5)))
>   1   3   4   5
> 3.5 4.0 5.0 6.0
> 
> Any suggestions?

In R-devel

> predict(tl,data.frame(x=c(2.5,NA,3,4,5)))
  1   2   3   4   5 
3.5  NA 4.0 5.0 6.0 

since there is a na.action argument to the predict method defaulting to 
na.pass.

In R 1.7.1

> op <- options(na.action=na.pass)
> predict(tl,data.frame(x=c(2.5,NA,3,4,5)))
  1   2   3   4   5 
3.5  NA 4.0 5.0 6.0 
> par(op)

as the session-wide default is used.

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