gam
is used to fit generalized additive models, specified by
giving a symbolic description of the additive predictor and a
description of the error distribution. gam
uses the
backfitting algorithm to combine different smoothing or
fitting methods. The methods currently supported are local regression
and smoothing splines.
1 2 3 4 5 6 7  gam(formula, family = gaussian, data, weights, subset, na.action,
start, etastart, mustart, control = gam.control(...),
model=TRUE, method, x=FALSE, y=TRUE, ...)
gam.fit(x, y, smooth.frame, weights = rep(1,nobs), start = NULL,
etastart = NULL, mustart = NULL, offset = rep(0, nobs), family = gaussian(),
control = gam.control())

formula 
a formula expression as for other regression models, of
the form 
family 
a description of the error distribution and link
function to be used in the model. This can be a character string
naming a family function, a family function or the result of a call
to a family function. (See 
data 
an optional data frame containing the variables
in the model. If not found in 
weights 
an optional vector of weights to be used in the fitting process. 
subset 
an optional vector specifying a subset of observations to be used in the fitting process. 
na.action 
a function which indicates what should happen
when the data contain 
start 
starting values for the parameters in the additive predictor. 
etastart 
starting values for the additive predictor. 
mustart 
starting values for the vector of means. 
offset 
this can be used to specify an a priori known component to be included in the additive predictor during fitting. 
control 
a list of parameters for controlling the fitting
process. See the documentation for 
model 
a logical value indicating whether model frame
should be included as a component of the returned value. Needed if

method 
the method to be used in fitting the parametric part of
the model.
The default method 
x, y 
For For 
smooth.frame 
for 
... 
further arguments passed to or from other methods. 
The gam model is fit using the local scoring algorithm, which
iteratively fits weighted additive models by backfitting. The
backfitting algorithm is a GaussSeidel method for fitting additive
models, by iteratively smoothing partial residuals. The algorithm
separates the parametric from the nonparametric part of the fit, and
fits the parametric part using weighted linear least squares within the
backfitting algorithm. This version of gam
remains faithful to
the philosophy of GAM models as outlined in the references below.
An object gam.slist
(currently set to
c("lo","s","random")
) lists the smoothers supported by
gam
. Corresponding to each of these is a smoothing function
gam.lo
, gam.s
etc that take particular arguments and
produce particular output, custom built to serve as building blocks in
the backfitting algorithm. This allows users to add their own smoothing
methods. See the documentation for these methods for further information.
In addition, the object gam.wlist
(currently set to
c("s","lo")
) lists the smoothers for which efficient backfitters
are provided. These are invoked if all the smoothing methods are of one
kind (either all "lo"
or all "s"
).
gam
returns an object of class gam
, which inherits from
both glm
and lm
.
Gam objects can be examined by print
, summary
,
plot
, and anova
. Components can be extracted using
extractor functions predict
, fitted
, residuals
,
deviance
, formula
, and family
. Can be modified
using update
. It has all the components of a glm
object,
with a few more. This also means it can be queried, summarized etc by
methods for glm
and lm
objects. Other generic functions
that have methods for gam
objects are step
and
preplot
.
The following components must be included in a legitimate ‘gam’ object.
The residuals, fitted values, coefficients and effects should be extracted
by the generic functions of the same name, rather than
by the "$"
operator.
The family
function returns the entire family object used in the fitting, and deviance
can be used to extract the deviance of the fit.
coefficients 
the coefficients of the parametric part of the 
additive.predictors 
the additive fit, given by the product of the model matrix and the coefficients, plus the columns of the 
fitted.values 
the fitted mean values, obtained by transforming the component 
smooth, nl.df, nl.chisq, var 
these four characterize the nonparametric aspect of the fit.

smooth.frame 
This is essentially a subset of the model frame
corresponding to the smooth terms, and has the ingredients needed for
making predictions from a 
residuals 
the residuals from the final weighted additive fit; also known as residuals, these are typically not interpretable without rescaling by the weights. 
deviance 
up to a constant, minus twice the maximized loglikelihood. Similar to the residual sum of squares. Where sensible, the constant is chosen so that a saturated model has deviance zero. 
null.deviance 
The deviance for the null model, comparable with

iter 
the number of local scoring iterations used to compute the estimates. 
family 
a threeelement character vector giving the name of the family, the link, and the variance function; mainly for printing purposes. 
weights 
the working weights, that is the weights in the final iteration of the local scoring fit. 
prior.weights 
the case weights initially supplied. 
df.residual 
the residual degrees of freedom. 
df.null 
the residual degrees of freedom for the null model. 
The object will also have the components of a lm
object:
coefficients
, residuals
, fitted.values
,
call
, terms
, and some
others involving the numerical fit. See lm.object
.
Written by Trevor Hastie, following closely the design in the
"Generalized Additive Models" chapter (Hastie, 1992) in Chambers and
Hastie (1992), and the philosophy in Hastie and Tibshirani (1991).
This version of gam
is adapted from the S
version to match the glm
and lm
functions in R.
Note that this version of gam
is different from the function
with
the same name in the R library mgcv
, which uses only smoothing
splines with a focus on automatic smoothing parameter selection via
GCV. Some of the functions in package gam
will not work if
package mgcv
is loaded (and detaching it is not enough; you
will need to restart the session).
Hastie, T. J. (1991) 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.
1 2 3 4 5 6 7 8 9 10 11 12  data(kyphosis)
gam(Kyphosis ~ s(Age,4) + Number, family = binomial, data=kyphosis,
trace=TRUE)
data(airquality)
gam(Ozone^(1/3) ~ lo(Solar.R) + lo(Wind, Temp), data=airquality, na=na.gam.replace)
gam(Kyphosis ~ poly(Age,2) + s(Start), data=kyphosis, family=binomial, subset=Number>2)
data(gam.data)
gam.object < gam(y ~ s(x,6) + z,data=gam.data)
summary(gam.object)
plot(gam.object,se=TRUE)
data(gam.newdata)
predict(gam.object,type="terms",newdata=gam.newdata)

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.
Please suggest features or report bugs with the GitHub issue tracker.
All documentation is copyright its authors; we didn't write any of that.