Rev 7872 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{smooth.construct}\alias{smooth.construct}\alias{smooth.construct2}\alias{user.defined.smooth}%- Also NEED an `\alias' for EACH other topic documented here.\title{Constructor functions for smooth terms in a GAM}\description{Smooth terms in a GAM formula are turned into smooth specification objects ofclass \code{xx.smooth.spec} during processing of the formula. Each of these objects isconverted to a smooth object using an appropriate \code{smooth.construct} function. New smooth classescan be added by writing a new \code{smooth.construct} method function and a corresponding\code{\link{Predict.matrix}} method function (see example code below).In practice, \code{smooth.construct} is usually called via \code{smooth.construct2} and the wrapperfunction \code{\link{smoothCon}}, in order to handle \code{by} variables andcentering constraints (see the \code{\link{smoothCon}} documentation ifyou need to handle these things directly, for a user defined smooth class).}\usage{smooth.construct(object,data,knots)smooth.construct2(object,data,knots)}%- maybe also `usage' for other objects documented here.\arguments{\item{object}{ is a smooth specification object, generated by an \code{\link{s}} or \code{\link{te}} term in a GAMformula. Objects generated by \code{s} terms have class \code{xx.smooth.spec} where \code{xx} is given by the\code{bs} argument of \code{s} (this convention allows the user to add their own smoothers).If \code{object} is not class \code{tensor.smooth.spec} it will have the following elements:\describe{\item{term}{The names of the covariates for this smooth, in an array.}\item{bs.dim}{ Argument \code{k} of the \code{s} term generating the object. This is the dimension of the basisused to represent the term (or, arguably, 1 greater than the basis dimension for \code{cc} terms).\code{bs.dim<0} indicates that the constructor should set this to the default value.}\item{fixed}{\code{TRUE} if the term is to be unpenalized, otherwise \code{FALSE}.}\item{dim}{the number covariates of which this smooth is a function.}\item{p.order}{the order of the smoothness penalty or \code{NA} for autoselection of this. This is argument\code{m} of the \code{s} term that generated \code{object}.}\item{by}{the name of any \code{by} variable to multiply this term as supplied as an argument to \code{s}.\code{"NA"} if there is no such term.}\item{label}{A suitable label for use with this term.}\item{xt}{An object containing information that may be needed for basis setup(used, e.g. by \code{"tp"} smooths to pass optional information on big datasethandling).}\item{id}{Any identity associated with this term --- used for linking basesand smoothing parameters. \code{NULL} by default, indicating no linkage.}\item{sp}{Smoothing parameters for the term. Any negative are estimated,otherwise they are fixed at the supplied value. Unless \code{NULL} (default),over-rides \code{sp} argument to \code{\link{gam}}.}}If \code{object} is of class \code{tensor.smooth.spec} then it was generated by a \code{te} term in the GAM formula,and specifies a smooth of several variables with a basis generated as a tensor product of lower dimensional bases.In this case the object will be different and will have the following elements:\describe{\item{margin}{is a list of smooth specification objects of the type listed above, defining the bases which havetheir tensor product formed in order to construct this term.}\item{term}{is the array of names of the covariates that are arguments of the smooth.}\item{by}{is the name of any \code{by} variable, or \code{"NA"}.}\item{fx}{is an array, the elements of which indicate whether (\code{TRUE}) any of the margins in thetensor product should be unpenalized.}\item{label}{A suitable label for use with this term.}\item{dim}{is the number of covariates of which this smooth is a function.}\item{mp}{\code{TRUE} if multiple penalties are to be used.}\item{np}{\code{TRUE} if 1-D marginal smooths are to be re-parameterized in terms offunction values.}\item{id}{Any identity associated with this term --- used for linking basesand smoothing parameters. \code{NULL} by default, indicating no linkage.}\item{sp}{Smoothing parameters for the term. Any negative are estimated,otherwise they are fixed at the supplied value. Unless \code{NULL} (default),over-rides \code{sp} argument to \code{\link{gam}}.}}}\item{data}{For \code{smooth.construct} a data frame or list containing the evaluation of the elements of \code{object$term},with names given by \code{object$term}. The last entry will be the \code{by} variable, if \code{object$by}is not \code{"NA"}. For \code{smooth.construct2} \code{data} need only be an object within which \code{object$term}can be evaluated, the variables can be in any order, and there can be irrelevant variables present as well. }\item{knots}{an optional data frame or list containing the knots relating to \code{object$term}.If it is \code{NULL} then the knot locations are generated automatically. The structure of \code{knots} shouldbe as for \code{data}, depending on whether \code{smooth.construct} or \code{smooth.construct2} is used.}}\value{The input argument \code{object}, assigned a new class to indicate what type of smooth it is and with at least thefollowing items added:\item{X}{The model matrix from this term. This may have an \code{"offset"}attribute: a vector of length \code{nrow(X)} containing any contribution ofthe smooth to the model offset term. \code{by} variables do not need to bedealt with here, but if they are then an item \code{by.done} must be added tothe \code{object}.}\item{S}{A list of positive semi-definite penalty matrices that apply to this term. The list will be emptyif the term is to be left un-penalized.}\item{rank}{An array giving the ranks of the penalties.}\item{null.space.dim}{The dimension of the penalty null space (before centering).}The following items may be added:\item{C}{The matrix defining any identifiability constraints on the term, for use when fitting. If this is \code{NULL} then\code{smoothCon} will add an identifiability constraint that each term shouldsum to zero over the covariate values. Set to a zero row matrix if noconstraints are required. If a supplied \code{C} has an attribute \code{"always.apply"} then it is never ignored, even if any\code{by} variables of a smooth imply that no constraint is actually needed. Code for creating \code{C} should check whetherthe specification object already contains a zero row matrix, and leave this unchanged if it is (since this signifiesno constraint should be produced). }\item{Cp}{An optional matrix supplying alternative identifiability constraints for use when predicting. By default thefitting constrants are used. This option is useful when some sort of simple sparse constraint is required for fitting, but theusual sum-to-zero constraint is required for prediction so that, e.g. the CIs for model components are as narrow as possible. }\item{no.rescale}{if this is non-NULL then the penalty coefficient matrix of the smooth will not berescaled for enhaced numerical stability (rescaling is the default, because \code{\link{gamm}} requires it).Turning off rescaling is useful if the values of the smoothing parameters should be interpretable in a model,for example because they are inverse variance components. }\item{df}{the degrees of freedom associated with this term (whenunpenalized and unconstrained). If this is null then \code{smoothCon} will set it to the basisdimension. \code{smoothCon} will reduce this by the number of constraints.}\item{te.ok}{\code{0} if this term should not be used as a tensor product marginal, \code{1} ifit can be used and plotted, and \code{2} is it can be used but not plotted. Set to \code{1} if \code{NULL}.}\item{plot.me}{Set to \code{FALSE} if this smooth should not be plotted by \code{\link{plot.gam}}. Set to \code{TRUE} if \code{NULL}.}\item{side.constrain}{Set to \code{FALSE} to ensure that the smooth is never subject to side constraints as a result of nesting. }\item{L}{smooths may depend on fewer `underlying' smoothing parameters than there are elements of\code{S}. In this case \code{L} is the matrix mapping the vector of underlying log smoothingparameters to the vector of logs of the smoothing parameters actually multiplying the \code{S[[i]]}.\code{L=NULL} signifies that there is one smoothing parameter per \code{S[[i]]}. }Usually the returned object will also include extra information required to define the basis, and used by\code{\link{Predict.matrix}} methods to make predictions using the basis. Seethe \code{Details} section for links to the information included for the built in smooth classes.\code{tensor.smooth} returned objects will additionally have each element ofthe \code{margin} list updated in the same way. \code{tensor.smooths} alsohave a list, \code{XP}, containing re-parameterization matrices for any 1-D marginal termsre-parameterized in terms of function values. This list will have \code{NULL}entries for marginal smooths that are not re-parameterized, and is only longenough to reach the last re-parameterized marginal in the list.}\details{ There are built in methods for objects with the following classes:\code{tp.smooth.spec} (thin plate regression splines: see \code{\link{tprs}});\code{ts.smooth.spec} (thin plate regression splines with shrinkage-to-zero);\code{cr.smooth.spec} (cubic regression splines: see \code{\link{cubic.regression.spline}};\code{cs.smooth.spec} (cubic regression splines with shrinkage-to-zero);\code{cc.smooth.spec} (cyclic cubic regression splines);\code{ps.smooth.spec} (Eilers and Marx (1986) style P-splines: see \code{\link{p.spline}});\code{cp.smooth.spec} (cyclic P-splines);\code{ad.smooth.spec} (adaptive smooths of 1 or 2 variables: see \code{\link{adaptive.smooth}});\code{re.smooth.spec} (simple random effect terms);\code{mrf.smooth.spec} (Markov random field smoothers for smoothing over discrete districts);\code{tensor.smooth.spec} (tensor product smooths).There is an implicit assumption that the basis only depends on the knots and/or the set of uniquecovariate combinations; i.e. that the basis is the same whether generated fromthe full set of covariates, or just the unique combinations of covariates.Plotting of smooths is handled by plot methods for smooth objects. A default \code{mgcv.smooth} methodis used if there is no more specific method available. Plot methods can be added for specific smooth classes, seesource code for \code{mgcv:::plot.sos.smooth}, \code{mgcv:::plot.random.effect}, \code{mgcv:::plot.mgcv.smooth}for example code.}\references{Wood, S.N. (2003) Thin plate regression splines. J.R.Statist.Soc.B 65(1):95-114Wood, S.N. (2006) Low rank scale invariant tensor product smooths forgeneralized additive mixed models. Biometrics 62(4):1025-1036The code given in the example is based on the smooths advocated in:Ruppert, D., M.P. Wand and R.J. Carroll (2003) Semiparametric Regression. CambridgeUniversity Press.However if you want p-splines, rather than splines with derivative based penalties,then the built in "ps" class is probably a marginally better bet. It's based onEilers, P.H.C. and B.D. Marx (1996) Flexible Smoothing with B-splines and Penalties.Statistical Science, 11(2):89-121}\author{Simon N. Wood \email{simon.wood@r-project.org}}\seealso{ \code{\link{s}},\code{\link{get.var}}, \code{\link{gamm}}, \code{\link{gam}},\code{\link{Predict.matrix}},\code{\link{smoothCon}}, \code{\link{PredictMat}}}\section{WARNING}{User defined smooth objects should avoid having attributes names\code{"qrc"} or \code{"nCons"} as these are used internally to provideconstraint free parameterizations.}\examples{## Adding a penalized truncated power basis class and methods## as favoured by Ruppert, Wand and Carroll (2003)## Semiparametric regression CUP. (No advantage to actually## using this, since mgcv can happily handle non-identity## penalties.)smooth.construct.tr.smooth.spec<-function(object,data,knots) {## a truncated power spline constructor method function## object$p.order = null space dimensionm <- object$p.order[1]if (is.na(m)) m <- 2 ## defaultif (m<1) stop("silly m supplied")if (object$bs.dim<0) object$bs.dim <- 10 ## defaultnk<-object$bs.dim-m-1 ## number of knotsif (nk<=0) stop("k too small for m")x <- data[[object$term]] ## the datax.shift <- mean(x) # shift used to enhance stabilityk <- knots[[object$term]] ## will be NULL if none suppliedif (is.null(k)) # space knots through data{ n<-length(x)k<-quantile(x[2:(n-1)],seq(0,1,length=nk+2))[2:(nk+1)]}if (length(k)!=nk) # right number of knots?stop(paste("there should be ",nk," supplied knots"))x <- x - x.shift # basis stabilizing shiftk <- k - x.shift # knots treated the same!X<-matrix(0,length(x),object$bs.dim)for (i in 1:(m+1)) X[,i] <- x^(i-1)for (i in 1:nk) X[,i+m+1]<-(x-k[i])^m*as.numeric(x>k[i])object$X<-X # the finished model matrixif (!object$fixed) # create the penalty matrix{ object$S[[1]]<-diag(c(rep(0,m+1),rep(1,nk)))}object$rank<-nk # penalty rankobject$null.space.dim <- m+1 # dim. of unpenalized space## store "tr" specific stuff ...object$knots<-k;object$m<-m;object$x.shift <- x.shiftobject$df<-ncol(object$X) # maximum DoF (if unconstrained)class(object)<-"tr.smooth" # Give object a classobject}Predict.matrix.tr.smooth<-function(object,data) {## prediction method function for the `tr' smooth classx <- data[[object$term]]x <- x - object$x.shift # stabilizing shiftm <- object$m; # spline order (3=cubic)k<-object$knots # knot locationsnk<-length(k) # number of knotsX<-matrix(0,length(x),object$bs.dim)for (i in 1:(m+1)) X[,i] <- x^(i-1)for (i in 1:nk) X[,i+m+1] <- (x-k[i])^m*as.numeric(x>k[i])X # return the prediction matrix}# an example, using the new class....require(mgcv)set.seed(100)dat <- gamSim(1,n=400,scale=2)b<-gam(y~s(x0,bs="tr",m=2)+s(x1,bs="ps",m=c(1,3))+s(x2,bs="tr",m=3)+s(x3,bs="tr",m=2),data=dat)plot(b,pages=1)b<-gamm(y~s(x0,bs="tr",m=2)+s(x1,bs="ps",m=c(1,3))+s(x2,bs="tr",m=3)+s(x3,bs="tr",m=2),data=dat)plot(b$gam,pages=1)# another example using tensor products of the new classdat <- gamSim(2,n=400,scale=.1)$datab <- gam(y~te(x,z,bs=c("tr","tr"),m=c(2,2)),data=dat)vis.gam(b)}\keyword{models} \keyword{smooth} \keyword{regression}%-- one or more ...