predict.glm
Predict Method for a Generalized Linear Model
Description
Computes predicted values and their standard errors for a fitted glm model.
Usage
## S3 method for class 'glm':
predict(object, newdata = NULL, type = c("link", "response",
"terms"), se.fit = FALSE, dispersion = NULL, terms = NULL,
na.action = na.pass, ...)
Arguments
object |
a fitted glm object. The output of the glm function.
|
newdata |
a data frame containing the values at which predictions are required.
If this argument is missing, predictions are made at
the same values used to compute the object.
Only those predictor columns referred to in the right side of the formula in
object need to be present by name in newdata.
|
type |
the type of predictions to make, with choices "link" (the default),
"response", or "terms".
- If you specify the default, "link", predictions are produced on the scale of the additive
predictors. If newdata is missing, predict() is simply an
extractor function for the line linear.predictors component of a glm object.
- If you specify "response", the predictions are on the scale of the
response, the inverse link function of the "link" predictions.
- If you specify type="terms", a
matrix of predictions on the additive scale is produced, each column giving the deviations
from the overall mean (of the original data's response, on the additive scale), which is
given by the attribute "constant".
|
se.fit |
a logical value. If TRUE, pointwise standard errors are computed along with the predictions.
(These are usually not appropriate for type="response".) The default is FALSE.
|
dispersion |
the dispersion value to use when computing standard errors. If NULL, use the estimated
(or postulated) dispersion for the fitted model given by summary(object)$dispersion.
|
terms |
if type="terms", you can use the terms= argument to specify which terms should be included.
The default is NULL, meaning that all terms are included.
|
na.action |
the function to handle missing values (NAs) for newdata. The default is na.pass.
|
... |
unrecognized arguments are silently ignored.
|
Details
This function is a method for the generic function predict for class glm.
It is intended to be invoked by calling predict for an object x of the appropriate class.
It can be called directly by calling predict.glm regardless of the class of the object, but
unless that object is very similar to a glm object, it gives ridiculous results.
Value
The structure of the return value depends on the arguments type and se.fit.
First we deal with the default case of se.fit=FALSE.
If type is "link" or "response", a vector of predictions is returned.
If type="terms", a matrix of fitted terms is returned,
with one column for each term in the model (or subset of these if the terms= argument is used).
There is never a column for the intercept: if the model includes an intercept each of the columns is
centered so that its 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.
(If there is no intercept in the model, this attribute will have the value 0.)
When
se.fit is TRUE then the above value is stored as the
fit component of the list
returned by
predict.glm and the other elements of this list are
se.fit |
standard errors of the values in the fit component.
|
residual.scale |
the scale used for computing se.fit.
|
Warning
predict.glm can produce incorrect predictions when the newdata
argument is used if the formula in object involves
data-dependent transformations, such as sqrt(Age - min(Age)).
However, certain commonly-used data-dependent transformations known to lm,
such as poly, scale, and splines::bs, are safe to use because glm
(via model.frame) uses makepredictcall to store the data-dependent information
for the transformation in the attr(terms,"predvars") part of its output.
See Also
Examples
additiveFit <- glm(breaks ~ wool + tension, data=warpbreaks, family=poisson)
predWoolData <- expand.grid(wool=c("A","B"), tension=c("L","M","H"))
cbind(predWoolData,
predicted=predict(additiveFit, newdata=predWoolData, type="response"))
d <- data.frame(Dose = c( 1, 2, 4, 8, 16, 32),
Alive = c(36, 38, 64, 95, 94, 97),
Dead = c(64, 62, 36, 5, 6, 3))
dFit <- glm(cbind(Alive, Dead) ~ Dose, data=d, family=binomial)
dPred <- data.frame(Dose = seq(0, 40, by=4))
cbind(dPred, predicted = predict(dFit, type="response", newdata=dPred))