predict.gam(object, newdata, type, se.fit = F, terms, safeEvalFrame = sys.parent(), ...)
gam
object or one of its inheritants, such as a
glm
object or an
lm
object.
object
need be present by name in
newdata
.
"link"
(the default),
"response"
, and
"terms"
.
The default produces predictions on the scale of the additive predictors: when
newdata
is missing and
type="link"
, the
predict
method is simply an extractor function for the fitted values of a
gam
object.
If
type="response"
, the predictions are on the scale of the response, and are monotone transformations of the additive predictors using the inverse link function.
If
type="terms"
, a matrix of predictions is produced, containing one column for each term in the model.
se.fit=FALSE
.
type="terms"
, the
terms
argument can be used to specify which terms should be included. The default is
labels(object)
.
safe.predict.gam
is evaluated. This should be the frame containing the data used to fit the model, if the data is not in a permanent database.
se.fit=TRUE
.
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 one is 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 the class gam. It can be invoked by calling
for an object of the appropriate class, or by calling
directly, regardless of the class of the object.
The
predict.gam
function is a safe method of prediction for the classes
gam
,
glm
, and
lm
.
Naive use of the generic
predict
method can produce incorrect predictions when the
newdata
argument is used, if the formula in
object
involves data-dependent transformations.
Examples of such transformations include
poly(Age,3)
and
sqrt(Age-min(Age))
.
The
predict.gam
function overcomes these problems by taking the following steps.
A combined data frame is constructed containing the predictors in
object
, using both the data from the
object
fit, as well as
newdata
.
From this, a combined model frame and model matrix is constructed, and
object
is refitted using the top portion of both of these (belonging to the fitting set).
The GAM iterations are not repeated; rather, one final IRLS step is performed using the working weights and response from the final iteration of the original
object
fit.
In this way, it is guaranteed that any coefficients that are estimated can be applied to both the fitting or prediction portions of the model matrix.
trainId <- 1:61 # Fit gam model to training data gamob <- gam(Kyphosis ~ s(Age) + s(Number), data=kyphosis[trainId,], family=binomial) # Extract the additive predictors predict(gamob) # Make predictions for validation data predict(gamob, newdata=kyphosis[-trainId,], type="terms") trainId <- 1:66 # Fit lm with poly term to training data: lmobj <- lm(NOx ~ poly(C, E, 4), data=ethanol[trainId,]) # Safe prediction for lm object with validation data predict.gam(lmobj, newdata=ethanol[-trainId,])