Use predict() on a lm Object

DESCRIPTION:

Extracts the fitted values from a lm object and returns a matrix of predictions.

USAGE:

predict.lm(object, newdata, type = c("response", "terms"),  
           se.fit = F, terms = labels(object), ci.fit = F, pi.fit = F, 
           conf.level = 0.95, conf.type = "p", ...)  

REQUIRED ARGUMENTS:

object
a fitted lm object.

OPTIONAL ARGUMENTS:

newdata
a data frame containing the values at which predictions are required. This argument can be missing, in which case predictions are made at the same values used to compute the object. Only those predictors referred to in the right side of the formula in object need be present by name in newdata.
type
type of predictions, with choices "response" (the default) or "terms". If "response" is selected, the predictions are on the scale of the response. If type="terms" is selected, the predictions are broken down into the contributions from each term. A matrix of predictions is produced, one column for each term in the model.
se.fit
if TRUE, pointwise standard errors are computed along with the predictions.
terms
if type="terms", the terms= argument can be used to specify which terms should be included; the default is labels(object).
ci.fit
if TRUE, lower and upper confidence intervals are computed for the fit. Not available for type="terms".
pi.fit
if TRUE, lower and upper prediction intervals are computed. These are confidence intervals for new observations, which will be wider than the confidence intervals for the fit. Not available for type="terms".
conf.level
confidence level for computing confidence and prediction intervals.
conf.type
type of confidence and prediction intervals to compute. If conf.type="p", pointwise intervals are computed. If conf.type="s", simultaneous intervals are computed. The simultaneous intervals adjust for the fact that we are estimating a whole set of values, and hence are wider than the pointwise intervals.

VALUE:

by default, a vector or matrix of predictions. May also be a list consisting of the predictions, standard errors, confidence intervals, and/or prediction intervals. If type="terms", a matrix of fitted terms is produced, with one column for each term in the model (or subset of these if the terms= argument is used). There is no column for the intercept, if present in the model, and each of the terms is centered so that their average over the original data is zero. The matrix of fitted terms has a "constant" attribute which, when added to the sum of these centered terms, gives the additive predictor.

DETAILS:

This function is a method for the generic function for class lm. It can be invoked by calling for an object x of the appropriate class, or directly by calling regardless of the class of the object.

WARNING:

predict can produce incorrect predictions when the newdata argument is used if the formula in object involves data-dependenttransformations, such as poly(Age, 3) or sqrt(Age - min(Age)). To overcome this for lm objects, use the predict.gam method explicitly.

SEE ALSO:

, , , , , , , .

EXAMPLES:

attach(car.all) 
attach(fuel.frame) 
fuel.lm <- lm(Fuel ~ Weight + Disp.) 
ok.vec <-  is.na(Mileage) & !(is.na(Weight) | is.na(Disp.)) 
ok.rows <- row.names(car.all)[1:10] 
new.cars <- car.all[ok.rows, c("Weight", "Disp.")] 
fuel.pred <- predict(fuel.lm, as.data.frame(data.matrix(new.cars)))