Predict method for GAM fits
predict.gam.RdObtains predictions and optionally estimates standard errors of those predictions from a fitted generalized additive model object.
Arguments
- object
a fitted
Gamobject, or one of its inheritants, such as aglmorlmobject.- newdata
a 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 object. Only those predictors, referred to in the right side of the formula in object need be present by name in
newdata.- type
type of predictions, with choices
"link"(the default),"response", or"terms". The default produces predictions on the scale of the additive predictors, and withnewdatamissing,predictis simply an extractor function for this component of aGamobject. If"response"is selected, the predictions are on the scale of the response, and are monotone transformations of the additive predictors, using the inverse link function. Iftype="terms"is selected, a matrix of predictions is produced, one column for each term in the model.- dispersion
the dispersion of the GLM fit to be assumed in computing the standard errors. If omitted, that returned by 'summary' applied to the object is used
- se.fit
if
TRUE, pointwise standard errors are computed along with the predictions.- na.action
function determining what should be done with missing values in 'newdata'. The default is to predict 'NA'.
- terms
if
type="terms", theterms=argument can be used to specify which terms should be included; the default islabels(object).- ...
Placemark for additional arguments to predict
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 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. See the documentation of
predict for more details on the components returned.
When newdata are supplied, predict.Gam simply invokes
inheritance and gets predict.glm to produce the parametric part of
the predictions. For each nonparametric term, predict.Gam
reconstructs the partial residuals and weights from the final iteration of
the local scoring algorithm. The appropriate smoother is called for each
term, with the appropriate xeval argument (see s or
lo), and the prediction for that term is produced.
The standard errors are based on an approximation given in Hastie (1992).
Currently predict.Gam does not produce standard errors for
predictions at newdata.
Warning: naive use of the generic predict can produce incorrect
predictions when the newdata argument is used, if the formula in
object involves transformations such as sqrt(Age - min(Age)).
References
Hastie, T. J. (1992) Generalized additive models. Chapter 7 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
Hastie, T. and Tibshirani, R. (1990) Generalized Additive Models. London: Chapman and Hall.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. New York: Springer.
Author
Written by Trevor Hastie, following closely the design in the
"Generalized Additive Models" chapter (Hastie, 1992) in Chambers and Hastie
(1992). This version of predict.Gam is adapted from the S version to
match the corresponding predict methods for glm and lm objects
in R. The safe.predict.Gam function in S is no longer required,
primarily because a safe prediction method is in place for functions like
ns, bs, and poly.
Examples
data(gam.data)
Gam.object <- gam(y ~ s(x,6) + z, data=gam.data)
predict(Gam.object) # extract the additive predictors
#> 1 2 3 4 5 6
#> 0.80174103 1.08594662 1.31185759 -0.06022807 1.03175342 1.18776805
#> 7 8 9 10 11 12
#> -0.34196019 0.91816066 1.14184929 -0.29130708 -0.39472393 1.44861900
#> 13 14 15 16 17 18
#> -0.10965067 0.47271437 0.59043519 1.42774838 1.35225367 1.28011817
#> 19 20 21 22 23 24
#> -0.37703323 1.47743733 1.38816256 1.14343389 -0.15893913 -0.25785994
#> 25 26 27 28 29 30
#> 1.41672394 -0.26039944 0.16411975 -0.27613058 1.48809101 -0.01668207
#> 31 32 33 34 35 36
#> -0.08602490 1.51644086 -0.32403519 -0.18497716 1.30911466 -0.13643062
#> 37 38 39 40 41 42
#> -0.26455503 1.46929300 0.86226834 0.15782520 0.13536228 0.01136045
#> 43 44 45 46 47 48
#> 0.51406068 1.06271731 -0.29758246 1.45362655 1.03180473 1.49352848
#> 49 50 51 52 53 54
#> 0.77868076 1.46033287 -0.30039450 -0.33161168 0.73381412 1.47103518
#> 55 56 57 58 59 60
#> -0.32700938 1.43848948 -0.31925787 1.50499849 -0.24600411 0.43841983
#> 61 62 63 64 65 66
#> 1.44918533 -0.16657437 0.74918230 0.60880562 1.34654949 -0.33211959
#> 67 68 69 70 71 72
#> -0.34074289 0.12581442 0.38596742 0.56651304 -0.32542561 1.34193106
#> 73 74 75 76 77 78
#> 0.87463786 0.89007914 1.39620172 1.49045233 -0.27479284 1.38673535
#> 79 80 81 82 83 84
#> 1.41351249 1.45097257 -0.29389414 0.96967379 1.44159095 1.38499769
#> 85 86 87 88 89 90
#> 0.67737510 0.92695474 -0.33725414 1.52324204 -0.32514766 0.56679845
#> 91 92 93 94 95 96
#> 1.32211069 0.85795424 0.78036605 1.20135673 1.37958521 1.02704045
#> 97 98 99 100
#> 0.55291453 1.45672148 -0.32175996 0.05277586
data(gam.newdata)
predict(Gam.object, gam.newdata, type="terms")
#> s(x, 6) z
#> 1 0.44379143 0.039428080
#> 2 0.75360796 0.028567775
#> 3 0.85468111 0.017707471
#> 4 0.80512984 0.006847166
#> 5 0.51279094 -0.004013139
#> 6 0.07523574 -0.014873443
#> 7 -0.42622549 -0.025733748
#> 8 -0.80005872 -0.036594052
#> 9 -0.96321574 -0.047454357
#> 10 -1.01594919 -0.058314661
#> attr(,"constant")
#> [1] 0.634896