s {mgcv} | R Documentation |
Function used in definition of smooth terms within
gam
model formulae. The function does not evaluate a (spline)
smooth - it exists purely to help set up a model using spline based smooths.
s(..., k=-1,fx=FALSE,bs="tp",m=NA,by=NA,xt=NULL,id=NULL,sp=NULL,pc=NULL)
... |
a list of variables that are the covariates that this
smooth is a function of. Transformations whose form depends on
the values of the data are best avoided here: e.g. |
k |
the dimension of the basis used to represent the smooth term.
The default depends on the number of variables that the smooth is a
function of. |
fx |
indicates whether the term is a fixed d.f. regression
spline ( |
bs |
a two letter character string indicating the (penalized) smoothing basis to use.
(eg |
m |
The order of the penalty for this term (e.g. 2 for
normal cubic spline penalty with 2nd derivatives when using
default t.p.r.s basis). |
by |
a numeric or factor variable of the same dimension as each covariate.
In the numeric vector case the elements multiply the smooth, evaluated at the corresponding
covariate values (a ‘varying coefficient model’ results). For the numeric |
xt |
Any extra information required to set up a particular basis. Used
e.g. to set large data set handling behaviour for |
id |
A label or integer identifying this term in order to link its smoothing
parameters to others of the same type. If two or more terms have the same
|
sp |
any supplied smoothing parameters for this term. Must be an array of the same
length as the number of penalties for this smooth. Positive or zero elements are taken as fixed
smoothing parameters. Negative elements signal auto-initialization. Over-rides values supplied in
|
pc |
If not |
The function does not evaluate the variable arguments. To use this function to specify use of
your own smooths, note the relationships between the inputs and the output object and see the example
in smooth.construct
.
A class xx.smooth.spec
object, where xx
is a basis identifying code given by
the bs
argument of s
. These smooth.spec
objects define smooths and are turned into
bases and penalties by smooth.construct
method functions.
The returned object contains the following items:
term |
An array of text strings giving the names of the covariates that the term is a function of. |
bs.dim |
The dimension of the basis used to represent the smooth. |
fixed |
TRUE if the term is to be treated as a pure regression spline (with fixed degrees of freedom); FALSE if it is to be treated as a penalized regression spline |
dim |
The dimension of the smoother - i.e. the number of covariates that it is a function of. |
p.order |
The order of the t.p.r.s. penalty, or 0 for auto-selection of the penalty order. |
by |
is the name of any |
label |
A suitable text label for this smooth term. |
xt |
The object passed in as argument |
id |
An identifying label or number for the smooth, linking it to other
smooths. Defaults to |
sp |
array of smoothing parameters for the term (negative for
auto-estimation). Defaults to |
Simon N. Wood simon.wood@r-project.org
Wood, S.N. (2003) Thin plate regression splines. J.R.Statist.Soc.B 65(1):95-114
Wood S.N. (2017) Generalized Additive Models: An Introduction with R (2nd edition). Chapman and Hall/CRC Press.
http://www.maths.bris.ac.uk/~sw15190/
# example utilising `by' variables library(mgcv) set.seed(0) n<-200;sig2<-4 x1 <- runif(n, 0, 1);x2 <- runif(n, 0, 1);x3 <- runif(n, 0, 1) fac<-c(rep(1,n/2),rep(2,n/2)) # create factor fac.1<-rep(0,n)+(fac==1);fac.2<-1-fac.1 # and dummy variables fac<-as.factor(fac) f1 <- exp(2 * x1) - 3.75887 f2 <- 0.2 * x1^11 * (10 * (1 - x1))^6 + 10 * (10 * x1)^3 * (1 - x1)^10 f<-f1*fac.1+f2*fac.2+x2 e <- rnorm(n, 0, sqrt(abs(sig2))) y <- f + e # NOTE: smooths will be centered, so need to include fac in model.... b<-gam(y~fac+s(x1,by=fac)+x2) plot(b,pages=1)