\HeaderA{predict.lm}{Predict method for Linear Model Fits}{predict.lm}
\aliasA{predict.mlm}{predict.lm}{predict.mlm}
\keyword{regression}{predict.lm}
\begin{Description}\relax
Predicted values based on linear model object.
\end{Description}
\begin{Usage}
\begin{verbatim}
## S3 method for class 'lm':
predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf, 
        interval = c("none", "confidence", "prediction"),
        level = 0.95, type = c("response", "terms"),
        terms = NULL, na.action = na.pass, pred.var = res.var/weights,
        weights = 1, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{object}] Object of class inheriting from \code{"lm"}
\item[\code{newdata}] An optional data frame in which to look for variables with
which to predict.  If omitted, the fitted values are used.
\item[\code{se.fit}] A switch indicating if standard errors are required.
\item[\code{scale}] Scale parameter for std.err. calculation
\item[\code{df}] Degrees of freedom for scale
\item[\code{interval}] Type of interval calculation.
\item[\code{level}] Tolerance/confidence level
\item[\code{type}] Type of prediction (response or model term).
\item[\code{terms}] If \code{type="terms"}, which terms (default is all terms)
\item[\code{na.action}] function determining what should be done with missing
values in \code{newdata}.  The default is to predict \code{NA}.
\item[\code{pred.var}] the variance(s) for future observations to be assumed
for prediction intervals.  See Details.
\item[\code{weights}] variance weights for prediction. This can be a numeric
vector or a one-sided model formula. In the latter case, it is
interpreted as an expression evaluated in \code{newdata}
\item[\code{...}] further arguments passed to or from other methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{predict.lm} produces predicted values, obtained by evaluating
the regression function in the frame \code{newdata} (which defaults to
\code{model.frame(object)}.  If the logical \code{se.fit} is
\code{TRUE}, standard errors of the predictions are calculated.  If
the numeric argument \code{scale} is set (with optional \code{df}), it
is used as the residual standard deviation in the computation of the
standard errors, otherwise this is extracted from the model fit.
Setting \code{intervals} specifies computation of confidence or
prediction (tolerance) intervals at the specified \code{level}, sometimes 
referred to as narrow vs. wide intervals.

If the fit is rank-deficient, some of the columns of the design matrix
will have been dropped.  Prediction from such a fit only makes sense
if \code{newdata} is contained in the same subspace as the original
data.  That cannot be checked accurately, so a warning is issued.

If \code{newdata} is omitted the predictions are based on the data
used for the fit.  In that case how cases with missing values in the
original fit is determined by the \code{na.action} argument of that
fit.  If \code{na.action = na.omit} omitted cases will not appear in
the residuals, whereas if \code{na.action = na.exclude} they will
appear (in predictions, standard errors or interval limits),
with residual value \code{NA}.  See also \code{\LinkA{napredict}{napredict}}.

The prediction intervals are for a single observation at each case in
\code{newdata} (or by default, the data used for the fit) with error
variance(s) \code{pred.var}. This can be a multiple of \code{res.var},
the estimated
value of \eqn{\sigma^2}{sigma^2}: the default is to assume that future
observations have the same error variance as those
used for fitting. If \code{weights} is supplied, the inverse of this
is used as a scale factor. For a weighted fit, if the prediction
is for the original data frame, \code{weights} defaults to the weights
used for the  model fit, with a warning since it might not be the
intended result. If the fit was weighted and newdata is given, the
default is to assume constant prediction variance, with a warning.
\end{Details}
\begin{Value}
\code{predict.lm} produces a vector of predictions or a matrix of
predictions and bounds with column names \code{fit}, \code{lwr}, and
\code{upr} if \code{interval} is set.  If \code{se.fit} is
\code{TRUE}, a list with the following components is returned: 
\begin{ldescription}
\item[\code{fit}] vector or matrix as above
\item[\code{se.fit}] standard error of predicted means
\item[\code{residual.scale}] residual standard deviations
\item[\code{df}] degrees of freedom for residual
\end{ldescription}
\end{Value}
\begin{Note}\relax
Variables are first looked for in \code{newdata} and then searched for
in the usual way (which will include the environment of the formula
used in the fit).  A warning will be given if the
variables found are not of the same length as those in \code{newdata}
if it was supplied.

Offsets specified by \code{offset} in the fit by \code{\LinkA{lm}{lm}}
will not be included in predictions, whereas those specified by an
offset term in the formula will be.

Notice that prediction variances and prediction intervals always refer
to \emph{future} observations, possibly corresponding to the same
predictors as used for the fit. The variance of the \emph{residuals}
will be smaller.

Strictly speaking, the formula used for prediction limits assumes that
the degrees of freedom for the fit are the same as those for the
residual variance.  This may not be the case if \code{res.var} is
not obtained from the fit.
\end{Note}
\begin{SeeAlso}\relax
The model fitting function \code{\LinkA{lm}{lm}}, \code{\LinkA{predict}{predict}},
\code{\LinkA{SafePrediction}{SafePrediction}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(graphics)

## Predictions
x <- rnorm(15)
y <- x + rnorm(15)
predict(lm(y ~ x))
new <- data.frame(x = seq(-3, 3, 0.5))
predict(lm(y ~ x), new, se.fit = TRUE)
pred.w.plim <- predict(lm(y ~ x), new, interval="prediction")
pred.w.clim <- predict(lm(y ~ x), new, interval="confidence")
matplot(new$x,cbind(pred.w.clim, pred.w.plim[,-1]),
        lty=c(1,2,2,3,3), type="l", ylab="predicted y")

## Prediction intervals, special cases
##  The first three of these throw warnings
w <- 1 + x^2
fit <- lm(y ~ x)
wfit <- lm(y ~ x, weights = w)
predict(fit, interval = "prediction")
predict(wfit, interval = "prediction")
predict(wfit, new, interval = "prediction")
predict(wfit, new, interval = "prediction", weights = (new$x)^2)
predict(wfit, new, interval = "prediction", weights = ~x^2)
\end{ExampleCode}
\end{Examples}

