\HeaderA{smooth.construct.tp.smooth.spec}{Penalized thin plate regression splines in GAMs}{smooth.construct.tp.smooth.spec}
\aliasA{smooth.construct.ts.smooth.spec}{smooth.construct.tp.smooth.spec}{smooth.construct.ts.smooth.spec}
\aliasA{tprs}{smooth.construct.tp.smooth.spec}{tprs}
\keyword{models}{smooth.construct.tp.smooth.spec}
\keyword{regression}{smooth.construct.tp.smooth.spec}
\begin{Description}\relax
\code{\LinkA{gam}{gam}} can use isotropic smooths of any number of variables, specified via terms like
\code{s(x,z,bs="tp",m=3)} (or just \code{s(x,z)} as this is the default basis). These terms are based on thin plate 
regression splines. \code{m} specifies the order of the derivatives in the thin plate spline penalty.

Thin plate regression splines are constructed by starting with the
basis and penalty for a full thin plate spline and then truncating this basis in
an optimal manner, to obtain a low rank smoother. Details are given in
Wood (2003). One key advantage of the approach is that it avoids
the knot placement problems of conventional regression spline
modelling, but it also has the advantage that smooths of lower rank
are nested within smooths of higher rank, so that it is legitimate to
use conventional hypothesis testing methods to compare models based on
pure regression splines. Note that the basis truncation does not change the 
meaning of the thin plate spline penalty (it penalizes exactly what it 
would have penalized for a full thin plate spline). 

The t.p.r.s. basis and penalties can become expensive to
calculate for large datasets. For this reason the default behaviour is to
randomly subsample \code{max.knots} unique data locations if there are more than \code{max.knots}
such, and to use the sub-sample for basis construction. The sampling is
always done with the same random seed to ensure repeatability (does not
reset R RNG). \code{max.knots} is 3000, by default. Both seed and
\code{max.knots} can be modified using the \code{xt} argument to \code{s}.
Alternatively the user can supply knots from which to construct a basis. 

The \code{"ts"} smooths are t.p.r.s. with a small multiple of the identity matrix added to the 
spline penalty so that the term can be shrink to zero, and not just to a function in the t.p.r.s.
penalty null space.
\end{Description}
\begin{Usage}
\begin{verbatim}
## S3 method for class 'tp.smooth.spec':
smooth.construct(object, data, knots)
## S3 method for class 'ts.smooth.spec':
smooth.construct(object, data, knots)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{object}] a smooth specification object, usually generated by a term \code{s(...,bs="tp",...)} or 
\code{s(...,bs="ts",...)}
\item[\code{data}] a list containing just the data (including any \code{by} variable) required by this term, 
with names corresponding to \code{object\$term} (and \code{object\$by}). The \code{by} variable 
is the last element.
\item[\code{knots}] a list containing any knots supplied for basis setup --- in same order and with same names as \code{data}. 
Can be \code{NULL}
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The default basis dimension for this class is \code{k=10*3\textasciicircum{}(d-1)} where \code{d} is the number
of covariates for the term.

The default is to set \code{m} (the order of derivative in the thin plate spline penalty) to the smallest value satisfying \code{2m > d+1} where \code{d} if the number of covariates of the term: this yields `visually smooth' functions. In any case \code{2m>d} must be satisfied. 


The constructor is not normally called directly, but is rather used internally by \code{\LinkA{gam}{gam}}. 
To use for basis setup it is recommended to use \code{\LinkA{smooth.construct2}{smooth.construct2}}.  

For these classes the specification \code{object} will contain
information on how to handle large datasets in their \code{xt} field. The default is to randomly
subsample 3000 `knots' from which to produce a tprs basis, if the number of
unique predictor variable combinations in excess of 3000. The default can be
modified via the \code{xt} argument to \code{\LinkA{s}{s}}. This is supplied as a
list with elements \code{max.knots} and \code{seed} containing a number
to use in place of 3000, and the random number seed to use (either can be
missing). 

For these bases \code{knots} has two uses. Firstly, as mentioned already, for large datasets 
the calculation of the \code{tp} basis can be time-consuming. The user can retain most of the advantages of the t.p.r.s. 
approach by supplying  a reduced set of covariate values from which to obtain the basis - 
typically the number of covariate values used will be substantially 
smaller than the number of data, and substantially larger than the basis dimension, \code{k}. This approach is 
the one taken automatically if the number of unique covariate values (combinations) exceeds \code{max.knots}.
The second possibility 
is to avoid the eigen-decomposition used to find the t.p.r.s. basis altogether and simply use 
the basis implied by the chosen knots: this will happen if the number of knots supplied matches the 
basis dimension, \code{k}. For a given basis dimension the second option is 
faster, but gives poorer results (and the user must be quite careful in choosing knot locations).
\end{Details}
\begin{Value}
An object of class \code{"tprs.smooth"} or \code{"ts.smooth"}. In addition to the usual elements of a 
smooth class documented under \code{\LinkA{smooth.construct}{smooth.construct}}, this object will contain:
\begin{ldescription}
\item[\code{shift}] A record of the shift applied to each covariate in order to center it around zero and 
avoid any co-linearity problems that might otehrwise occur in the penalty null space basis of the term. 
\item[\code{Xu}] A matrix of the unique covariate combinations for this smooth (the basis is constructed by first stripping 
out duplicate locations).
\item[\code{UZ}] The matrix mapping the t.p.r.s. parameters back to the parameters of a full thin plate spline.
\item[\code{null.space.dimension}] The dimension of the space of functions that have zero wiggliness according to the 
wiggliness penalty for this term.
\end{ldescription}
\end{Value}
\begin{Author}\relax
Simon N. Wood \email{simon.wood@r-project.org}
\end{Author}
\begin{References}\relax
Wood, S.N. (2003) Thin plate regression splines. J.R.Statist.Soc.B 65(1):95-114
\end{References}
\begin{Examples}
\begin{ExampleCode}
## see ?gam
\end{ExampleCode}
\end{Examples}

