# R-alpha: predict.lm -- who ..?

**Ross Ihaka
**
ihaka@stat.auckland.ac.nz

*Mon, 15 Sep 1997 22:16:26 +1200 (NZST)*

Here is my (incomplete) version. This will work for full-rank models,
and I suspect also for singular models (the comment is just a reminder
to myself that I was thinking about this).
I set out to change the underlying structure so that case names
would be propagated through to to the fitted values. This doesn't
happen at the moment because they are not handled within "model.matrix".
A quick fix would be to replace dimnames(x) below by row.names(mf).
There is no computation of standard errors below, but the present
version has something like the right computation.
Sorry that this is just a hint, but I'm really under pressure at
present. Anyone got any (applied) exam questions on linear models
or glms handy?
Ross
predict.lm <-
function (fit, newdata)
{
mf <- model.frame(formula(fit), data = newdata)
# recreate the design matrix
x <- model.matrix(terms(fit), mf)
# grab the coefficients (order is correct?)
b <- coef(fit)
b[is.na(b)] <- 0
# compute predictions
p <- x %*% b
# attach name information
if(is.matrix(b)) {
dimnames(p) <- list(dimnames(x)[[1]], dimnames(b)[[2]])
}
else {
p <- as.vector(p)
names(p) <- dimnames(x)[[1]]
}
p
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel 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-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-