te
for defining tensor product smooths
in a gam
formula. Results in a construction in which the penalties are
non-overlapping multiples of identity matrices (with some rows and columns zeroed).
The construction is analogous to Chong Gu's (2002) Smoothing Spline ANOVA, but using
low rank penalized regression spline marginals. The main advantage of this construction
is that it is useable with gamm4
from package gamm4
.t2(..., k=NA,bs="cr",m=NA,d=NA,by=NA,
xt=NULL,id=NULL,sp=NULL,full=FALSE)
5^d
. If supplied as a single number then this
basis dimension is used for each basis. If supplied as an array then the elements are
the "cr"
for cubic regression spline; "cs"
for cubic
regression spline with shrinkage; "cc"
for periodic/cyclic
cubic regression spline; <NA
autoinitializes.
m
is ignored by some bases (e.g. "cr"
).d=c(2,1)
. Incompatibilities between built in basis typesid
then they will have the same smoothing paramsters, and, by defaulTRUE
then there is a separate penalty for each combination of null space column
and range space. This gives strict invariance. If FALSE
each combination of null space and
range space generates one penalty, but the coulmns oft2.smooth.spec
object defining a tensor product smooth
to be turned into a basis and penalties by the smooth.construct.tensor.smooth.spec
function. The returned object contains the following items:
smooth.spec
objects of the type returned by s
,
defining the basis from which the tensor product smooth is constructed.by
variable as text ("NA"
for none).TRUE
if the penalty is to
be ignored, FALSE
, otherwise.TRUE
is multiple penalties are to be used (default).TRUE
to re-parameterize 1-D marginal smooths in terms of function
values (defualt).id
argument supplied to te
.sp
argument supplied to te
.The re-parameterized marginal bases are then combined to produce a basis for a single function of all the covariates (dimension given by the product of the dimensions of the marginal bases). In this set up there are multiple penalty matrices --- all zero, but for a mixture of a constant and zeros on the leading diagonal. No two penalties have a non-zero entry in the same place.
Essentially the basis for the tensor product can be thought of as being constructed from a set of
products of the penalized (range) or unpenalized (null) space bases of the marginal smooths (see Gu, 2002, section 2.4).
To construct one of the set, choose either the
null space or the range space from each marginal, and from these bases construct a product basis. The result is subject to a ridge
penalty (unless it happens to be a product entirely of marginal null spaces). The whole basis for the smooth is constructed from
all the different product bases that can be constructed in this way. The separately penalized components of the smooth basis each
have an interpretation in terms of the ANOVA - decomposition of the term.
See pen.edf
for some further information.
Note that there are two ways to construct the product. When full=FALSE
then the null space bases are treated as a whole in each product,
but when full=TRUE
each null space column is treated as a separate null space. The latter results in more penalties, but is the strict
analog of the SS-ANOVA approach.
Tensor product smooths are especially useful for representing functions of covariates measured in different units, although they are typically not quite as nicely behaved as t.p.r.s. smooths for well scaled covariates.
Note also that GAMs constructed from lower rank tensor product smooths are nested within GAMs constructed from higher rank tensor product smooths if the same marginal bases are used in both cases (the marginal smooths themselves are just special cases of tensor product smooths.)
Note that tensor product smooths should not be centred (have identifiability constraints imposed) if any marginals would not need centering. The constructor for tensor product smooths ensures that this happens.
The function does not evaluate the variable arguments.
Wood, S.N. (2004) Stable and efficient multiple smoothing parameter estimation for generalized additive models. J. Amer. Statist. Ass. 99:673-686.
Alternative approaches to functional ANOVA decompositions, *not* implemented by t2 terms, are discussed in:
Belitz and Lang (2008) Simultaneous selection of variables and smoothing parameters in structured additive regression models. Computational Statistics & Data Analysis, 53(1):61-81
Lee, D-J and M. Durban (2011) P-spline ANOVA type interaction models for spatio-temporal smoothing. Statistical Modelling Wood, S.N. (2006) Low-Rank Scale-Invariant Tensor Product Smooths for Generalized Additive Mixed Models. Biometrics 62(4): 1025-1036.
te
s
,gam
,gamm
,# following shows how tensor product deals nicely with
# badly scaled covariates (range of x 5\% of range of z )
test1<-function(x,z,sx=0.3,sz=0.4)
{ x<-x*20
(pi**sx*sz)*(1.2*exp(-(x-0.2)^2/sx^2-(z-0.3)^2/sz^2)+
0.8*exp(-(x-0.7)^2/sx^2-(z-0.8)^2/sz^2))
}
n<-500
old.par<-par(mfrow=c(2,2))
x<-runif(n)/20;z<-runif(n);
xs<-seq(0,1,length=30)/20;zs<-seq(0,1,length=30)
pr<-data.frame(x=rep(xs,30),z=rep(zs,rep(30,30)))
truth<-matrix(test1(pr$x,pr$z),30,30)
f <- test1(x,z)
y <- f + rnorm(n)*0.2
b1<-gam(y~s(x,z))
persp(xs,zs,truth);title("truth")
vis.gam(b1);title("t.p.r.s")
b2<-gam(y~t2(x,z))
vis.gam(b2);title("tensor product")
b3<-gam(y~t2(x,z,bs=c("tp","tp")))
vis.gam(b3);title("tensor product")
par(old.par)
test2<-function(u,v,w,sv=0.3,sw=0.4)
{ ((pi**sv*sw)*(1.2*exp(-(v-0.2)^2/sv^2-(w-0.3)^2/sw^2)+
0.8*exp(-(v-0.7)^2/sv^2-(w-0.8)^2/sw^2)))*(u-0.5)^2*20
}
n <- 500
v <- runif(n);w<-runif(n);u<-runif(n)
f <- test2(u,v,w)
y <- f + rnorm(n)*0.2
## tensor product of 2D thin plate regression spline and 1D cr spline
b <- gam(y~t2(v,w,u,k=c(30,5),d=c(2,1),bs=c("tp","cr")))
## look at the edf per penalty. "rr" denotes interaction term
## (range space range space). "rn" is interaction of null space
## for u with range space for v,w...
pen.edf(b)
## plot results...
op <- par(mfrow=c(2,2))
vis.gam(b,cond=list(u=0),color="heat",zlim=c(-0.2,3.5))
vis.gam(b,cond=list(u=.33),color="heat",zlim=c(-0.2,3.5))
vis.gam(b,cond=list(u=.67),color="heat",zlim=c(-0.2,3.5))
vis.gam(b,cond=list(u=1),color="heat",zlim=c(-0.2,3.5))
par(op)
b <- gam(y~t2(v,w,u,k=c(30,5),d=c(2,1),bs=c("tp","cr"),full=TRUE),
method="ML")
## more penalties now. numbers in labels like "r1" indicate which
## basis function of a null space is involved in the term.
pen.edf(b)
Run the code above in your browser using DataLab