[R] na.action in predict.lm

Andrea Peters Peters.Andrea at imbe.imed.uni-erlangen.de
Wed Jan 9 18:08:24 CET 2002


I would like to predict a matrix containing missing values according to a
fitted linear model.
The predicted values must have the same length as the number of
observations in newdata, where missing predicted values (due to missing
explanatory values) are replaced by NA. How can I achieve this? I tried
the following example:

> x <- matrix(rnorm(100), ncol=10)
> beta <- rep(1, 10)
> y <- x%*%beta + rnorm(10)
> 
> options(na.action="na.exclude")
> mod <- lm(y ~ . - 1, data=as.data.frame(x))
> 
> x[5,5] <- NA
> x <- as.data.frame(x)                  
> predict(mod, newdata=x)
         1          2          3          4          6          7 
-0.6161748  1.3659464 -3.2987223  1.2891959  2.1753231 -3.9714629
         8          9         10
-0.8349864  2.1076517 -2.3992661 


The desired output would look like:
         1          2          3          4   5          6          7 
-0.6161748  1.3659464 -3.2987223  1.2891959  NA  2.1753231 -3.9714629
         8          9         10
-0.8349864  2.1076517 -2.3992661 


I already read the help pages of na.action, options, napredict etc., but
I couldn't figure out how to solve this problem.

Many thanks in advance for helpful comments.
Andrea

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



More information about the R-help mailing list