\HeaderA{gam.convergence}{GAM convergence and performance issues}{gam.convergence}
\aliasA{gam.performance}{gam.convergence}{gam.performance}
\keyword{models}{gam.convergence}
\keyword{smooth}{gam.convergence}
\keyword{regression}{gam.convergence}
\begin{Description}\relax
When fitting GAMs there is a tradeoff between speed of fitting and probability of
fit convergence. The default fitting options specified by
\code{\LinkA{gam.method}{gam.method}} (as the default for argument \code{method} of
\code{\LinkA{gam}{gam}}), always opt for certainty of convergence over speed of
fit. In the Generalized Additive Model case it means using
`outer' iteration in preference to `performance iteration': see
\code{\LinkA{gam.outer}{gam.outer}} for details. 

It is possible for the default `outer' iteration to fail when finding intial
smoothing parameters using a few steps of performance iteration (if you get a
convergence failure message from \code{magic} when outer iterating, then this
is what has happened): lower \code{outerPIsteps} in \code{\LinkA{gam.control}{gam.control}}
to fix this. 

There are three things that you can try to speed up GAM fitting. (i) if you have large 
numbers of smoothing parameters in the generalized case, then try the \code{"bfgs"} method as the \code{outer} 
option in \code{\LinkA{gam.method}{gam.method}}: this can be faster than the default. (ii) Change the
\code{method} argument to \code{\LinkA{gam}{gam}} so that `performance iteration' is
used in place of the default outer iteration. See the \code{gam="perf"} option
under \code{\LinkA{gam.method}{gam.method}}, for example. Usually performance iteration
converges well and it can sometimes be quicker than the default outer iteration. 
(iii) For large datasets it may be worth changing
the smoothing basis to use \code{bs="cr"} (see \code{\LinkA{s}{s}} for details)
for 1-d smooths, and to use \code{\LinkA{te}{te}} smooths in place of
\code{\LinkA{s}{s}} smooths for smooths of more than one variable. This is because
the default thin plate regression spline basis \code{"tp"} is costly to set up
for large datasets (much over 1000 data, say).

If the GAM estimation process fails to converge when using performance
iteration, then switch to outer iteration via the \code{method} argument of
\code{\LinkA{gam}{gam}} (see \code{\LinkA{gam.method}{gam.method}}). If it still fails, try
increasing the number of IRLS iterations (see \code{\LinkA{gam.control}{gam.control}}) or
perhaps experiment with the convergence tolerance. 

If you still have problems, it's worth noting that a GAM is just a (penalized)
GLM and the IRLS scheme used to estimate GLMs is not guaranteed to
converge. Hence non convergence of a GAM may relate to a lack of stability in
the basic IRLS scheme. Therefore it is worth trying to establish whether the IRLS iterations
are capable of converging. To do this fit the problematic GAM with all smooth
terms specified with \code{fx=TRUE} so that the smoothing parameters are all
fixed at zero. If this `largest' model can converge then, then the maintainer 
would quite like to know about your problem! If it doesn't converge, then its
likely that your model is just too flexible for the IRLS process itself. Having tried
increasing \code{maxit} in \code{gam.control}, there are several other
possibilities for stabilizing the iteration. It is possible to try (i) setting lower bounds on the
smoothing parameters using the \code{min.sp} argument of \code{gam}: this may
or may not change the model being fitted; (ii)
reducing the flexibility of the model by reducing the basis dimensions
\code{k} in the specification of \code{s} and \code{te} model terms: this
obviously changes the model being fitted somewhat; (iii)
introduce a small regularization term into the fitting via the \code{irls.reg}
argument of \code{gam.control}: this option obviously changes the nature of
the fit somewhat, since parameter estimates are pulled towards zero by doing
this. 

Usually, a major contributer to fitting difficulties is that the
model is a very poor description of the data.
\end{Description}
\begin{Author}\relax
Simon N. Wood \email{simon.wood@r-project.org}
\end{Author}

