Last chance! 50% off unlimited learning
Sale ends in
gam
model formulae. te
produces a full tensor product smooth, while ti
produces a tensor product interaction, appropriate when the main effects (and any lower
interactions) are also present.The functions do not evaluate the smooth - they exists purely to help set up a model using tensor product based smooths. Designed to construct tensor products from any marginal smooths with a basis-penalty representation (with the restriction that each marginal smooth must have only one penalty).
te(..., k=NA,bs="cr",m=NA,d=NA,by=NA,fx=FALSE,
mp=TRUE,np=TRUE,xt=NULL,id=NULL,sp=NULL)
ti(..., k=NA,bs="cr",m=NA,d=NA,by=NA,fx=FALSE,
np=TRUE,xt=NULL,id=NULL,sp=NULL,mc=NULL)
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; <m
for each margin. For marginals that taked=c(2,1)
. Incompatibilities between built in basis typesTRUE
) or a penalized regression spline (FALSE
).TRUE
to use multiple penalties for the smooth. FALSE
to use only
a single penalty: single penalties are not recommended and are deprecated - they tend to allow only rather
wiggly models.TRUE
to use the `normal parameterization' for a tensor
product smooth. This represents any 1-d marginal smooths
via parameters that are function values at `knots',
spread evenly through the data. The parameterization makes the penalties
easilid
then they will have the same smoothing paramsters, and, by defaulti
smooths you can specify which marginals should have centering constraints
applied, by supplying 0/1 or FALSE
/TRUE
values for each marginal in this vector. By default
all marginals are constrained, which is whtensor.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
.TRUE
if the term was generated by ti
, FALSE
otherwise.mc
supplied to ti
.tensor.prod.model.matrix
and tensor.prod.penalties
, to produce
a single model matrix for the smooth, but multiple penalties (one for each marginal basis). The basis dimension
of the whole smooth is the product of the basis dimensions of the marginal smooths.
An option for operating with a single penalty (The Kronecker product of the marginal penalties) is provided, but
it is rarely of practical use, and is deprecated: the penalty is typically so rank deficient that
even the smoothest resulting model will have rather high estimated degrees of freedom. 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.
It is sometimes useful to investigate smooth models with a main-effects + interactions structure, for example
ti
terms, which produce tensor product interactions from which the main effects have been excluded, under the assumption that they will be included separately. For example the ~ ti(x) + ti(z) + ti(x,z)
would produce the above main effects + interaction structure. This is much better than attempting the same thing with s
or te
terms representing the interactions (although mgcv does not forbid it). Technically ti
terms are very simple: they simply construct tensor product bases from marginal smooths to which identifiability constraints (usually sum-to-zero) have already been applied: correct nesting is then automatic (as with all interactions in a GLM framework).
The `normal parameterization' (np=TRUE
) re-parameterizes the marginal
smooths of a tensor product smooth so that the parameters are function values
at a set of points spread evenly through the range of values of the covariate
of the smooth. This means that the penalty of the tensor product associated
with any particular covariate direction can be interpreted as the penalty of
the appropriate marginal smooth applied in that direction and averaged over
the smooth. Currently this is only done for marginals of a single
variable. This parameterization can reduce numerical stability when used
with marginal smooths other than "cc"
, "cr"
and "cs"
: if
this causes problems, set np=FALSE
.
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.
s
,gam
,gamm
,
smooth.construct.tensor.smooth.spec
# following shows how tensor pruduct deals nicely with
# badly scaled covariates (range of x 5\% of range of z )
require(mgcv)
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~te(x,z))
vis.gam(b2);title("tensor product")
b3 <- gam(y~ ti(x) + ti(z) + ti(x,z))
vis.gam(b3);title("tensor anova")
## now illustrate partial ANOVA decomp...
vis.gam(b3);title("full anova")
b4 <- gam(y~ ti(x) + ti(x,z,mc=c(0,1))) ## note z constrained!
vis.gam(b4);title("partial anova")
plot(b4)
par(old.par)
## now with a multivariate marginal....
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 Duchon spline and 1D cr spline
m <- list(c(1,.5),rep(0,0)) ## example of list form of m
b <- gam(y~te(v,w,u,k=c(30,5),d=c(2,1),bs=c("ds","cr"),m=m))
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)
Run the code above in your browser using DataLab