Simple factor smooth interactions, which are efficient when used with
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
See the discussion of
by variables in
gam.models for more general alternatives
for factor smooth interactions (including interactions of tensor product smooths with factors).
1 2 3 4
a list containing just the data (including any
a list containing any knots supplied for smooth basis setup.
This class produces a smooth for each level of a single factor variable. Within a
formula this is done with something like
s(x,fac,bs="fs"), which is almost equivalent to
select=TRUE). 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).
The class is particularly useful for use with
gamm, where estimation efficiently exploits
the nesting of the smooth within the factor. Note however that: i)
gamm only allows one conditioning
factor for smooths, so
s(x)+s(z,fac,bs="fs")+s(v,fac,bs="fs") is OK, but
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
gamm with an attribute
"gamm" attached to the smooth specification object. The result differs from that resulting from the case where this is
gamm4 from the
gamm4 package suffers from none of the restrictions that apply to
"fs" terms can be used without side-effects. Constructor is still called with a smooth specification object having a
Any singly penalized basis can be used to smooth at each factor level. The default is
"tp", but alternatives can
be supplied in the
xt argument of
k argument to
s(...,bs="fs") refers to the basis dimension to
use for each level of the factor variable.
Note one computational bottleneck: currently
gamm4) 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
The plot method for this class has two schemes.
scheme==0 is in colour, while
scheme==1 is black and white.
An object of class
"fs.interaction" or a matrix mapping the coefficients of the factor smooth interaction to the smooths themselves. The contents of an
"fs.interaction" object will depend on whether or not
smooth.construct was called with an object with attribute
gamm: see below.
Simon N. Wood firstname.lastname@example.org with input from Matteo Fasiolo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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.
Loading required package: nlme This is mgcv 1.8-27. For overview type 'help("mgcv-package")'. Family: gaussian Link function: identity Formula: y ~ s(x0) + s(x1, fac, bs = "fs", k = 5) + s(x2, k = 20) Parametric coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 7.2938 0.1382 52.76 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Approximate significance of smooth terms: edf Ref.df F p-value s(x0) 3.222 3.222 13.47 9.94e-09 *** s(x1,fac) 37.070 NA NA NA s(x2) 10.251 10.251 93.50 < 2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 R-sq.(adj) = 0.739 Scale est. = 4.0572 n = 500
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.