Exactly how the random effects are implemented is best seen by example. Consider the model
term s(x,z,bs="re")
. This will result in the model matrix component corresponding to ~x:z-1
being added to the model matrix for the whole model. The coefficients associated with the model matrix
component are assumed i.i.d. normal, with unknown variance (to be estimated). This assumption is
equivalent to an identity penalty matrix (i.e. a ridge penalty) on the coefficients. Because such a
penalty is full rank, random effects terms do not require centering constraints.
If the nature of the random effect specification is not clear, consider a couple more examples:
s(x,bs="re")
results in model.matrix(~x-1)
being appended to the overall model matrix,
while s(x,v,w,bs="re")
would result in model.matrix(~x:v:w-1)
being appended to the model
matrix. In both cases the corresponding model coefficients are assumed i.i.d. normal, and are hence
subject to ridge penalties.
If the random effect precision matrix is of the form \(\sum_j \lambda_j S_j\) for known matrices \(S_j\) and unknown parameters \(\lambda_j\), then a list containing the \(S_j\) can be supplied in the xt
argument of
s
. In this case an array rank
should also be supplied in xt
giving the ranks of the \(S_j\) matrices. See simple example below.
Note that smooth id
s are not supported for random effect terms. Unlike most smooth terms, side
conditions are never applied to random effect terms in the event of nesting (since they are identifiable
without side conditions).
Random effects implemented in this way do not exploit the sparse structure of many random effects, and
may therefore be relatively inefficient for models with large numbers of random effects, when gamm4
or gamm
may be better alternatives. Note also that gam
will not support
models with more coefficients than data.
The situation in which factor variable random effects intentionally have unobserved levels requires special handling.
You should set drop.unused.levels=FALSE
in the model fitting function, gam
, bam
or gamm
, having first ensured that any fixed effect factors do not contain unobserved levels.