# [R] predictions under NA

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Sep 17 11:50:36 CEST 2001

```If fitting by lm (and glm, at least), na.action=na.exclude will
do what you want.  It keeps enough track of modifications to make 2)
correct.  Some other fitting procedures (in MASS, for example) do use 1).

na.exclude is fairly new (1.3.0), but it is on the help page for na.omit.

You do need to use the proper extractor functions: you should not use
fit\$fitted.values but fitted(fit) or predicted(fit), except perhaps when
writing R internals.

On Mon, 17 Sep 2001, Christian Hoffmann wrote:

> Hi all,
>
> Maybe I missed something in the help pages, but I am wondering if there is
> a staight forward solution to the following problem:
>
> Given several predictors x_i and a regressand y, containing NAs
> (symbolically):
>
> y_1 = (none of the x_1i contain NA) %*% beta + eps_1
> y_2 = (some of the x_2i contain NA) %*% beta + eps_2
> y_3 = (none of the x_3i contain NA) %*% beta + eps_3
> ....
> y_n = (none of the x_ni contain NA) %*% beta + eps_n
>
> Fitting a (linear) model lm to the data will use complete cases only (if
> not failing), so that  "fitted.values" may contain only
>
> lm\$fitted.values[1]  corresponding to y_1
> lm\$fitted.values[2]  corresponding to y_3
> ....
> lm\$fitted.values[n-#(of-complete-cases)]  corresponding to y_n
>
> This situation may be inconvenient. To get the full array of predictions I
> would have  two possibilities (at least):
>
> 1. expected <- model.matrix(x) %*% lm\$coefficients
>
> 2. judicious use of "complete.cases" and others do something like
>   expexted[complete.cases] <- lm\$fitted.values
>   expexted[-complete.cases] <- NA
>
> 1. Seems straight forward and fool proof, but may be computational overkill
> in the case of very few NAs.
> 2. May not be fool proof because of (hidden, at first glance unrecognized)
> internals in lm (like rearrangements ?)
>
> Does anybody have any thoughts on this?
>
> Thank you very much.
>
> --christian
>
>
>
>
>
> Dr.sc.math.Christian W. Hoffmann
> Mathematics and Statistical Computing
> Landscape Modeling and Web Applications
> Swiss Federal Research Institute WSL
> Zuercherstrasse 111
> CH-8903 Birmensdorf, Switzerland
> phone: ++41-1-739 22 77    fax: ++41-1-739 22 15
> e-mail: christian.hoffmann_at_wsl.ch__prevent_spamming
> www: http://www.wsl.ch/staff/christian.hoffmann/
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

--
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```