Skip to content

predict.lm with NAs

3 messages · Martin Batholdy, ronggui, Walmes Zeviani

#
Hi,

I wanted to use the predict.lm() function to compare the empirical data with the predicted values.
The problem is that I have NAs in my data.

I wanted to cbind my data.frame with the empirical values with the vector I get from predict.lm.
But they don't have the same length because predict.lm just skip NA-predictions.

Is there a way to get a vector with predicted values of the same length as the empirical values (with NAs)?


thanks!
#
see ?na.exclude

you can set na.action='na.exclude' when fit the model.
On 15 April 2010 09:06, Martin Batholdy <batholdy at googlemail.com> wrote:

  
    
#
You can use predict() by specifying a complete data.frame() for prediction to
the argument newdata=. Look:

da <- expand.grid(x1=LETTERS[1:4], x2=1:9)
da$y <- rnorm(da$x1)
da$y[sample(length(da$y), 5)] <- NA

m0 <- lm(y~x1+x2, data=da)
predict(m0) # NA not predicted
predict(m0, newdata=da) # NA predicted

Sincerely.
Walmes.

-----
..ooo0
...................................................................................................
..(....)... 0ooo...                              Walmes Zeviani
...\..(.....(.....)...     Master in Statistics and Agricultural
Experimentation
....\_)..... )../....       walmeszeviani at hotmail.com, Lavras - MG, Brasil
............
(_/............................................................................................