mkterm: Assembling Model Terms for Smoothing Spline ANOVA Models

View source: R/mkterm.R

mktermR Documentation

Assembling Model Terms for Smoothing Spline ANOVA Models

Description

Assemble numerical functions for calculating model terms in a smoothing spline ANOVA model.

Usage

mkterm(mf, type)

Arguments

mf

Model frame of the model formula.

type

List specifying the type of spline for each variable.

Details

For a factor x, type$x is ignored; mkrk.ordinal is used if is.ordered(x)==TRUE and mkrk.nominal is used otherwise. Factors with 3 or more levels are penalized.

For a numerical vector x, type$x is of the form type.x for type.x="cubic", "linear", or of the form list(type.x, range) for type.x="per", "cubic.per", "linear.per", "cubic", "linear"; "per" is short for "cubic.per". See mkfun.poly for the functions used. For type.x missing, the default is "cubic". For range missing with type.x="cubic", "linear", the default is c(min(x),max(x))+c(-1,1)*(max(x)-mimn(x))*.05.

For a numerical matrix x, type$x is of the form type.x or list(type.x, order) for type.x="tp", "sphere", or of the form list("tp",list(order=order,mesh=mesh,weight=weight)). See mkfun.tp for the functions used. For type.x missing, the default is "tp". For order missing, the default is 2. For mesh and weight missing with type.x="tp" and order given, the defaults are mesh=x and weight=1.

For a numerical vector or numerical matrix x, one may also use type$x of the form list("custom",list(nphi=nphi,mkphi=mkphi,mkrk=mkrk,env=env)); nphi is the null space dimension excluding the constant, and mkphi is ignored if nphi=0. See examples below. This feature allows the use of other marginal constructions; one may modify mkphi.cubic or mkphi.tp.p for mkphi and modify mkrk.cubic or mkrk.sphere for mkrk.

Value

A list object with an element labels containing the labels of all model terms. For each of the model terms, there is an element holding the numerical functions for calculating the unpenalized and penalized parts within the term.

Background

Tensor-product splines are constructed based on the model formula and the marginal reproducing kernels, as described in Gu (2002, Sec. 2.4). The marginal variables can be factors, numerical vectors, and numerical matrices, as specified in the details section.

One-way ANOVA decompositions are built in the supported marginal constructions, in which one has the constant, a "nonparametric contrast," and possibly also a "parametric contrast." To the "nonparametric contrast" there corresponds a reproducing kernal rk, and to a "parametric contrast" there corresponds a set of null space basis phi. The reproducing kernels and null space basis on the product domain can be constructed from the marginal rk and phi in a systematic manner.

The marginal one-way ANOVA structures induce a multi-way ANOVA structure on the product domain, with model terms consisting of unpenalized "parametric contrasts" and/or penalized "nonparametric contrasts." One only needs to construct rk's and phi's associated with the model terms implied by the model formula.

Note

For a numerical vector x in ssden, the default range is domain$x.

For a numerical matrix x with type.x="sphere", it is assumed that dim(x)[2]==2, x[,1] between [-90,90] the latitude in degrees, and x[,2] between [-180,180] the longitude in degrees.

For backward compatibility, one may set type="cubic", "linear", or "tp", but then the default parameters can not be overridden; the type is simply duplicated for each variable.

References

Gu, C. (2013), Smoothing Spline ANOVA Models (2nd Ed). New York: Springer-Verlag.

Examples

## cubic marginals
x1 <- rnorm(100); x2 <- rnorm(100); y <- 3+5*sin(x1-2*x2)+rnorm(x1)
fit <- ssanova0(y~x1*x2)
## the same fit
fit1 <- ssanova0(y~x1*x2,type=list(x1="cubic"))
## the same fit one more time
par <- list(nphi=1,mkphi=mkphi.cubic,mkrk=mkrk.cubic,
            env=c(min(x2),max(x2))+c(-1,1)*(max(x2)-min(x2))*.05)
fit2 <- ssanova0(y~x1*x2,type=list(x2=list("custom",par)))
## Clean up
## Not run: rm(x1,x2,y,fit,fit1,par,fit2)

## cubic and thin-plate marginals
x1 <- rnorm(100); x2 <- matrix(rnorm(200),100,2)
y <- 3+5*sin(x1-2*x2[,1]*x2[,2])+rnorm(x1)
fit <- ssanova0(y~x1*x2)
## the same fit
fit1 <- ssanova0(y~x1*x2,type=list(x2="tp"))
## the same fit one more time
mkphi.tp1 <- function(x) mkphi.tp(x$dm,x$ord,x$mesh,x$wt)
mkrk.tp1 <- function(x) mkrk.tp(x$dm,x$ord,x$mesh,x$wt)
env <- list(dm=2,ord=2,mesh=x2,wt=1)
par <- list(nphi=2,mkphi=mkphi.tp1,mkrk=mkrk.tp1,env=env)
fit2 <- ssanova0(y~x1*x2,type=list(x2=list("custom",par)))
## Clean up
## Not run: rm(x1,x2,y,fit,fit1,mkphi.tp1,mkrk.tp1,env,par,fit2)

gss documentation built on Oct. 12, 2024, 1:08 a.m.

Related to mkterm in gss...