predict.lm
Predict Method for a Linear Model

Description

Make predictions based on an lm object.

Usage

## S3 method for class 'lm':
predict(object, newdata = NULL, se.fit = FALSE, scale = NULL, df = Inf,
    interval = "none", level = 0.95,
    type = "response", terms = NULL, na.action = na.pass,
    pred.var = res.var/weights, weights = 1, ...) 		
## S3 method for class 'mlm':
predict(object, newdata = NULL, se.fit = FALSE, na.action = na.pass,
    ...)

Arguments

object a fitted lm (or mlm) object.
newdata An environemt, data frame, or list containing the values of the predictor variables 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. The predictors referred to in the right side of the formula in object must be present by name in newdata.
se.fit if TRUE, pointwise standard errors are computed along with the predictions. This is not available for multiresponse (mlm) models and se.fit=TRUE will cause an error for them.
scale If not NULL this is used instead of the scale recorded the lm object.
df If scale is not NULL, this is used as the number of residual degrees of freedom.
interval the interval type. it can be "none" (the default), "confidence" or "prediction".
level confidence or prediction level.
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.
terms if type="terms", the terms= argument can be used to specify which terms should be included; the default is NULL. It means all terms are included. This argument is ignored when type is "response".
na.action the function to handle missing values (NAs) for newdata. The default is na.pass.
pred.var prediction variance used instead of the estimated residual variance. Only used when interval is "prediction".
weights a numeric vector. Another way to specify pred.var: the estimated residual variance will be divided by weights to produce prev.var
... further arguments passed to or from other methods

Details

This function is a method for the generic function predict for class lm. It can be invoked by callingpredict for an object x of the appropriate class, or directly by calling predict.lm regardless of the class of the object. (In the latter case an error will occur or the results will be incorrect if the object is not sufficiently like an "lm" object.) Only the listed arguments are uses for predict.mlm -- any others are silently ignored.
Value
If type = "response", the default, a vector of predictions or, if interval is "confidence" or "prediction", a matrix of predictions with columns named "fit", "lwr" and "upr" giving the fit and its lower and upper confidence or prediction intervals.
If type="terms", a matrix of term-wise fitted values is produced, with one column for each term in the model (or subset of these if the terms= argument is used). There will be no column for the intercept but its value will be attached as the attributed named "constant". The row sums of this matrix, plus the constant term, will be the same as the predicted values given in the type="response" case.
If se.fit=TRUE, the above fitted values will be the the "fit" component of a list. The other components of the returned list will be "se.fit", a vector or matrix that same shape as "fit" containing the standard errors of the predicted values, "df", the number of residual degrees of freedom, and "residual.scale", the scale of the residuals.
When type is "terms" and interval is "confidence" or "prediction", things are further rearranged. You will get a list with matrix components (with one column per term) "fit", "se.fit", "lrw", and "upr" and scalar components "df" and "residual.scale".
Warning
predict can produce incorrect predictions when the newdata argument is used if the formula in object involves data-dependenttransformations, such as sqrt(Age - min(Age)). However, certain data-dependent transformations known to lm, such as poly, scale, and splines::ns are safe to use because lm uses makepredictcall to store the data-dependent information for the transformation in the attr(terms,"predvars") part of its output.
See Also
lm, glm, fitted, predict, predict.glm, makepredictcall.
Examples
fit <- lm(Fuel ~ Weight + Disp. + Type, data=Sdatasets::fuel.frame)
predict(fit)
predict(fit, newdata=data.frame(Weight=2750, Disp.=110, Type="Small"))
predict(fit, interval = "prediction",
    newdata=data.frame(Weight=2700, Disp.=300, Type="Sporty"))
predict(fit, type = "terms")
Package stats version 6.1.1-7
Package Index