EVOLUTION-MANAGER
Edit File: smooth.construct.fs.smooth.spec.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Factor smooth interactions in GAMs</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for smooth.construct.fs.smooth.spec {mgcv}"><tr><td>smooth.construct.fs.smooth.spec {mgcv}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Factor smooth interactions in GAMs</h2> <h3>Description</h3> <p>Simple factor smooth interactions, which are efficient when used with <code><a href="gamm.html">gamm</a></code>. This smooth class allows a separate smooth for each level of a factor, with the same smoothing parameter for all smooths. It is an alternative to using factor <code>by</code> variables. </p> <p>See the discussion of <code>by</code> variables in <code><a href="gam.models.html">gam.models</a></code> for more general alternatives for factor smooth interactions (including interactions of tensor product smooths with factors). </p> <h3>Usage</h3> <pre> ## S3 method for class 'fs.smooth.spec' smooth.construct(object, data, knots) ## S3 method for class 'fs.interaction' Predict.matrix(object, data) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>object</code></td> <td> <p>For the <code>smooth.construct</code> method a smooth specification object, usually generated by a term <code>s(x,...,bs="fs",)</code>. May have a <code>gamm</code> attribute: see details. For the <code>predict.Matrix</code> method an object of class <code>"fs.interaction"</code> produced by the <code>smooth.construct</code> method.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>a list containing just the data (including any <code>by</code> variable) required by this term, with names corresponding to <code>object$term</code>.</p> </td></tr> <tr valign="top"><td><code>knots</code></td> <td> <p> a list containing any knots supplied for smooth basis setup.</p> </td></tr> </table> <h3>Details</h3> <p>This class produces a smooth for each level of a single factor variable. Within a <code><a href="gam.html">gam</a></code> formula this is done with something like <code>s(x,fac,bs="fs")</code>, which is almost equivalent to <code>s(x,by=fac,id=1)</code> (with the <code>gam</code> argument <code>select=TRUE</code>). The terms are fully penalized, with separate penalties on each null space component: for this reason they are not centred (no sum-to-zero constraint). </p> <p>The class is particularly useful for use with <code><a href="gamm.html">gamm</a></code>, where estimation efficiently exploits the nesting of the smooth within the factor. Note however that: i) <code>gamm</code> only allows one conditioning factor for smooths, so <code>s(x)+s(z,fac,bs="fs")+s(v,fac,bs="fs")</code> is OK, but <code>s(x)+s(z,fac1,bs="fs")+s(v,fac2,bs="fs")</code> is not; ii) all aditional random effects and correlation structures will be treated as nested within the factor of the smooth factor interaction. To facilitate this the constructor is called from <code><a href="gamm.html">gamm</a></code> with an attribute <code>"gamm"</code> attached to the smooth specification object. The result differs from that resulting from the case where this is not done. </p> <p>Note that <code>gamm4</code> from the <code>gamm4</code> package suffers from none of the restrictions that apply to <code>gamm</code>, and <code>"fs"</code> terms can be used without side-effects. Construcor is still called with a smooth specification object having a <code>"gamm"</code> attribute. </p> <p>Any singly penalized basis can be used to smooth at each factor level. The default is <code>"tp"</code>, but alternatives can be supplied in the <code>xt</code> argument of <code>s</code> (e.g. <code>s(x,fac,bs="fs",xt="cr")</code> or <code>s(x,fac,bs="fs",xt=list(bs="cr")</code>). The <code>k</code> argument to <code>s(...,bs="fs")</code> refers to the basis dimension to use for each level of the factor variable. </p> <p>Note one computational bottleneck: currently <code><a href="gamm.html">gamm</a></code> (or <code>gamm4</code>) will produce the full posterior covariance matrix for the smooths, including the smooths at each level of the factor. This matrix can get large and computationally costly if there are more than a few hundred levels of the factor. Even at one or two hundred levels, care should be taken to keep down <code>k</code>. </p> <p>The plot method for this class has two schemes. <code>scheme==0</code> is in colour, while <code>scheme==1</code> is black and white. </p> <h3>Value</h3> <p> An object of class <code>"fs.interaction"</code> or a matrix mapping the coefficients of the factor smooth interaction to the smooths themselves. The contents of an <code>"fs.interaction"</code> object will depend on whether or not <code>smooth.construct</code> was called with an object with attribute <code>gamm</code>: see below. </p> <h3>Author(s)</h3> <p> Simon N. Wood <a href="mailto:simon.wood@r-project.org">simon.wood@r-project.org</a></p> <h3>See Also</h3> <p><code><a href="gam.models.html">gam.models</a></code>, <code><a href="gamm.html">gamm</a></code></p> <h3>Examples</h3> <pre> library(mgcv) set.seed(0) ## simulate data... f0 <- function(x) 2 * sin(pi * x) f1 <- function(x,a=2,b=-1) exp(a * x)+b f2 <- function(x) 0.2 * x^11 * (10 * (1 - x))^6 + 10 * (10 * x)^3 * (1 - x)^10 n <- 500;nf <- 25 fac <- sample(1:nf,n,replace=TRUE) x0 <- runif(n);x1 <- runif(n);x2 <- runif(n) a <- rnorm(nf)*.2 + 2;b <- rnorm(nf)*.5 f <- f0(x0) + f1(x1,a[fac],b[fac]) + f2(x2) fac <- factor(fac) y <- f + rnorm(n)*2 ## so response depends on global smooths of x0 and ## x2, and a smooth of x1 for each level of fac. ## fit model (note p-values not available when fit ## using gamm)... bm <- gamm(y~s(x0)+ s(x1,fac,bs="fs",k=5)+s(x2,k=20)) plot(bm$gam,pages=1) summary(bm$gam) ## Could also use... ## b <- gam(y~s(x0)+ s(x1,fac,bs="fs",k=5)+s(x2,k=20),method="ML") ## ... but its slower (increasingly so with increasing nf) ## b <- gam(y~s(x0)+ t2(x1,fac,bs=c("tp","re"),k=5,full=TRUE)+ ## s(x2,k=20),method="ML")) ## ... is exactly equivalent. </pre> <hr /><div style="text-align: center;">[Package <em>mgcv</em> version 1.8-28 <a href="00Index.html">Index</a>]</div> </body></html>