R-alpha: Anyone got "predict" to work in R?

Douglas Bates (bates@stat.wisc.edu)
Thu, 29 Aug 96 16:35 CDT


Message-Id: <m0uwEk5-0000TzC@franz.stat.wisc.edu>
Date: Thu, 29 Aug 96 16:35 CDT
From: Douglas Bates <bates@stat.wisc.edu>
To: R-testers <r-testers@stat.math.ethz.ch>
Subject: R-alpha: Anyone got "predict" to work in R?

The reason that I started looking at match.call() in R with a
... argument was to try to decide what to do with the predict
function.

I have a fitted linear regression model called fm7.1 The only
independent variable is called "residents".  I want to predict for a
closely spaced set of values for "residents".  The predict function as
documented in the white book indicates I should use
 predict(fm7.1, newdata = data.frame(residents = seq(0, 250, length = 51)))
or
 predict(fm7.1, newdata = list(residents = seq(0, 250, length = 51)))
Both of these end up creating X as a 1 by 1 matrix of lists early in
the predict function.  I don't think that is what is expected.

The R function starts off
 "predict" <-
 function (object, ...) 
 {
	 namelist <- list(...)
	 names(namelist) <- substitute(...)
	 m <- length(namelist)
	 X <- as.matrix(namelist[[1]])
	 if (m > 1) 
		 for (i in (2:m)) X <- cbind(X, namelist[[i]])
	 if (object$intercept) 
		 X <- cbind(rep(1, NROW(X)), X)
	 k <- NCOL(X)
	 if (length(object$coef) != k) 
		 stop("Wrong number of predictors")
	 predictor <- X %*% object$coef
which leads me to believe that I am expected to use
 predict(fm7.1, residents = seq(0, 250, length = 51))
or something like that.  That second line modifies the already
existing names on the list in such a way that they will not match the
coefficients. 

I think the better course, although perhaps not as efficient it to
generate a new model frame and model matrix using the terms from the
fitted model.  This version counts on getting all the values of the
new variables in the right order and does not allow for general model
formulae.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-