Make Predictions from a Fitted GAM Object

DESCRIPTION:

Provides a safe method of prediction from a fitted GAM object.

USAGE:

predict.gam(object, newdata, type, se.fit = F, terms, 
            safeEvalFrame = sys.parent(), ...) 

REQUIRED ARGUMENTS:

object
fitted gam object or one of its inheritants, such as a glm object or an lm object.

OPTIONAL ARGUMENTS:

newdata
data frame containing the values at which predictions are required. This argument can be missing, in which case predictions are made at the same values used to compute the model object. Only those predictors that appear on the right side of the formula in object need be present by name in newdata.
type
type of predictions. Choices include "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
logical value: should pointwise standard errors be computed along with the predictions? By default, se.fit=FALSE.
terms
if type="terms", the terms argument can be used to specify which terms should be included. The default is labels(object).
safeEvalFrame
frame number in which the call to 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.

VALUE:

a vector or matrix of predictions, or a list consisting of the predictions and their standard errors if 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.

DETAILS:

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.

SEE ALSO:

, , , .

EXAMPLES:

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,])