predict()
on a
lm
Object
lm
object and returns a matrix of
predictions.
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", ...)
lm
object.
newdata
.
"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.
TRUE
, pointwise standard errors are computed along with the predictions.
type="terms"
, the
terms=
argument can be used to specify which terms should be included; the default is
labels(object)
.
TRUE
, lower and upper confidence intervals are computed for the fit. Not available for
type="terms"
.
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.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.
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.
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.
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.
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)))