The smooth components of GAMs can be viewed as random effects for estimation purposes. This means that more conventional
random effects terms can be incorporated into GAMs in two ways. The first method converts all the smooths into fixed and random components
suitable for estimation by standard mixed modelling software. Once the GAM is in this form then conventional random effects are easily added,
and the whole model is estimated as a general mixed model. `gamm`

and `gamm4`

from the `gamm4`

package operate in this way.

The second method represents the conventional random effects in a GAM in the same way that the smooths are represented --- as penalized
regression terms. This method can be used with `gam`

by making use of `s(...,bs="re")`

terms in a model: see
`smooth.construct.re.smooth.spec`

, for full details. The basic idea is that, e.g., `s(x,z,g,bs="re")`

generates an i.i.d. Gaussian
random effect with model matrix given by `model.matrix(~x:z:g-1)`

--- in principle such terms can take any number of arguments. This simple
approach is sufficient for implementing a wide range of commonly used random effect structures. For example if `g`

is a factor then
`s(g,bs="re")`

produces a random coefficient for each level of `g`

, with the random coefficients all modelled as i.i.d. normal. If `g`

is a factor and `x`

is numeric, then `s(x,g,bs="re")`

produces an i.i.d. normal random slope relating the response to `x`

for each level of `g`

. If `h`

is another factor then `s(h,g,bs="re")`

produces the usual i.i.d. normal `g`

- `h`

interaction. Note that a rather useful approximate test for zero random effect is also implemented for such terms based on Wood (2013). If the precision
matrix is known to within a multiplicative constant, then this can be supplied via the `xt`

argument of `s`

. See smooth.construct.re.smooth.spec for details and example.

Alternatively, but less straightforwardly, the `paraPen`

argument to `gam`

can be used:
see `gam.models`

. If smoothing parameter estimation is by ML or REML (e.g. `gam(...,method="REML")`

) then this approach is
a completely conventional likelihood based treatment of random effects.

`gam`

can be slow for fitting models with large numbers of random effects, because it does not exploit the sparsity that is often a feature
of parametric random effects. It can not be used for models with more coefficients than data. However `gam`

is often faster and more reliable
than `gamm`

or `gamm4`

, when the number of random effects is modest.

To facilitate the use of random effects with `gam`

, `gam.vcomp`

is a utility routine for converting
smoothing parameters to variance components. It also provides confidence intervals, if smoothness estimation is by ML or REML.

Note that treating random effects as smooths does not remove the usual problems associated with testing variance components for equality to zero: see `summary.gam`

and `anova.gam`

.