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.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())
response ~ predictors
. See the documentation of
lm
and formula
for details. Built-in nonparametric
smoothing terms are indicated by s
for smoothing splines or
lo
for loess
smooth terms. See the documentation for
s
and lo
for their arguments. Additional smoothers can be
added by creating the appropriate interface functions. Interactions with
nonparametric smooth terms are not fully supported, but will not produce
errors; they will simply produce the usual parametric interaction.family
for details of
family functions.)data
, the variables are taken
from environment(formula)
, typically the environment from
which gam
is called.NA
s. The default is set by
the na.action
setting of options
, and is
na.fail
if that is unset. The “factory-fresh”
default is na.omit
. A special method
na.gam.replace
allows for mean-imputation of missing
values (assumes missing at random), and works gracefully with gam
gam.control
for details. These can also be set as arguments to gam()
itself.
gam
is called and predicted from inside a user
function. Default is TRUE
."glm.fit"
uses iteratively reweighted
least squares (IWLS). The only current alternative is
"model.frame"
which returns the model frame and does no fitting.gam
:
logical values indicating whether the response
vector and model matrix used in the fitting process
should be returned as components of the returned value. For gam.fit
: x
is a model matrix of dimension n
* p
, and y
is a vector of observations of length n
.
gam.fit
only. This is essentially a
subset of the model frame corresponding to the smooth terms, and has
the ingredients needed for smoothing each variable in the backfitting
algorithm. The elements of this frame are produced by the formula
functions lo
and 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. additive.predictors
, which multiply the
columns of the model
matrix.
The names of the coefficients are the names of the
single-degree-of-freedom effects (the columns of the
model matrix).
If the model is overdetermined there will
be missing values in the coefficients corresponding to inestimable
coefficients.
$smooth
component.
additive.predictors
using the inverse link function.
smooth
is a matrix of smooth terms, with a column corresponding to each smooth term in the model; if no smooth terms are in the gam
model, all these components will be missing.
Each column corresponds to the strictly nonparametric part of the term, while the parametric part is obtained from the model matrix.
nl.df
is a vector giving the approximate degrees of freedom for each column of smooth
. For smoothing splines specified by s(x)
, the approximate df
will be the trace of the implicit smoother matrix minus 2.
nl.chisq
is a vector containing a type of score test for the removal of each of the columns of smooth
.
var
is a matrix like smooth
, containing the approximate pointwise variances for the columns of smooth
.
gam
objectdeviance
. The null model will include the offset, and an
intercept if there is one in the modellm
object:
coefficients
, residuals
, fitted.values
,
call
, terms
, and some
others involving the numerical fit. See lm.object
.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"
).glm
, family
, lm
.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)
Run the code above in your browser using DataLab