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!
predict.lm with NAs
3 messages · Martin Batholdy, ronggui, Walmes Zeviani
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:
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!
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Wincent Rong-gui HUANG Doctoral Candidate Dept of Public and Social Administration City University of Hong Kong http://asrr.r-forge.r-project.org/rghuang.html
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 ............ (_/............................................................................................
View this message in context: http://n4.nabble.com/predict-lm-with-NAs-tp1840661p1886457.html Sent from the R help mailing list archive at Nabble.com.