On Wed, 9 Jan 2002, Andrea Peters wrote:
>
> 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.
na.exclude only works with the existing dataset (from which the data were
fitted). So this is OK:
> x <- matrix(rnorm(100), ncol=10)
> beta <- rep(1, 10)
> y <- x%*%beta + rnorm(10)
> y <- x%*%beta + rnorm(10)
> x[5,5] <- NA
> mod <- lm(y ~ . - 1, data=as.data.frame(x),
na.action="na.exclude")
> predict(mod)
1 2 3 4 5 6
0.6629695 0.4075846 -2.9255085 -1.8214493 NA -0.3774940
7 8 9 10
-10.5488613 3.8361323 0.8803932 -3.8637141
If you want to predict for a new x containing missing values, do
xn <- as.data.frame(matrix(rnorm(100), ncol=10))
xn[5,5] <- NA
pred <- rep(NA, nrow(xn)); names(pred) <- row.names(xn)
xnn <- na.omit(xn)
pred[-attr(xnn, "na.action")] <- predict(mod, xnn)
pred
--
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._