EVOLUTION-MANAGER
Edit File: random.effects.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: Random effects 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 random.effects {mgcv}"><tr><td>random.effects {mgcv}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Random effects in GAMs</h2> <h3>Description</h3> <p> 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. <code><a href="gamm.html">gamm</a></code> and <code>gamm4</code> from the <code>gamm4</code> package operate in this way. </p> <p>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 <code><a href="gam.html">gam</a></code> by making use of <code>s(...,bs="re")</code> terms in a model: see <code><a href="smooth.construct.re.smooth.spec.html">smooth.construct.re.smooth.spec</a></code>, for full details. The basic idea is that, e.g., <code>s(x,z,g,bs="re")</code> generates an i.i.d. Gaussian random effect with model matrix given by <code>model.matrix(~x:z:g-1)</code> — 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 <code>g</code> is a factor then <code>s(g,bs="re")</code> produces a random coefficient for each level of <code>g</code>, with the random coefficients all modelled as i.i.d. normal. If <code>g</code> is a factor and <code>x</code> is numeric, then <code>s(x,g,bs="re")</code> produces an i.i.d. normal random slope relating the response to <code>x</code> for each level of <code>g</code>. If <code>h</code> is another factor then <code>s(h,g,bs="re")</code> produces the usual i.i.d. normal <code>g</code> - <code>h</code> 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 <code>xt</code> argument of <code>s</code>. See <a href="smooth.construct.re.smooth.spec.html">smooth.construct.re.smooth.spec</a> for details and example. </p> <p>Alternatively, but less straightforwardly, the <code>paraPen</code> argument to <code><a href="gam.html">gam</a></code> can be used: see <code><a href="gam.models.html">gam.models</a></code>. If smoothing parameter estimation is by ML or REML (e.g. <code>gam(...,method="REML")</code>) then this approach is a completely conventional likelihood based treatment of random effects. </p> <p><code>gam</code> 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 <code>gam</code> is often faster and more reliable than <code>gamm</code> or <code>gamm4</code>, when the number of random effects is modest. </p> <p>To facilitate the use of random effects with <code>gam</code>, <code><a href="gam.vcomp.html">gam.vcomp</a></code> is a utility routine for converting smoothing parameters to variance components. It also provides confidence intervals, if smoothness estimation is by ML or REML. </p> <p>Note that treating random effects as smooths does not remove the usual problems associated with testing variance components for equality to zero: see <code><a href="summary.gam.html">summary.gam</a></code> and <code><a href="anova.gam.html">anova.gam</a></code>. </p> <h3>Author(s)</h3> <p>Simon Wood <simon.wood@r-project.org> </p> <h3>References</h3> <p>Wood, S.N. (2013) A simple test for random effects in regression models. Biometrika 100:1005-1010 </p> <p>Wood, S.N. (2011) Fast stable restricted maximum likelihood and marginal likelihood estimation of semiparametric generalized linear models. Journal of the Royal Statistical Society (B) 73(1):3-36 </p> <p>Wood, S.N. (2008) Fast stable direct fitting and smoothness selection for generalized additive models. Journal of the Royal Statistical Society (B) 70(3):495-518 </p> <p>Wood, S.N. (2006) Low rank scale invariant tensor product smooths for generalized additive mixed models. Biometrics 62(4):1025-1036 </p> <h3>See Also</h3> <p><code><a href="gam.vcomp.html">gam.vcomp</a></code>, <code><a href="gam.models.html">gam.models</a></code>, <code><a href="smooth.terms.html">smooth.terms</a></code>, <code><a href="smooth.construct.re.smooth.spec.html">smooth.construct.re.smooth.spec</a></code>, <code><a href="gamm.html">gamm</a></code></p> <h3>Examples</h3> <pre> ## see also examples for gam.models, gam.vcomp, gamm ## and smooth.construct.re.smooth.spec ## simple comparison of lme and gam require(mgcv) require(nlme) b0 <- lme(travel~1,data=Rail,~1|Rail,method="REML") b <- gam(travel~s(Rail,bs="re"),data=Rail,method="REML") intervals(b0) gam.vcomp(b) anova(b) plot(b) ## simulate example... dat <- gamSim(1,n=400,scale=2) ## simulate 4 term additive truth fac <- sample(1:20,400,replace=TRUE) b <- rnorm(20)*.5 dat$y <- dat$y + b[fac] dat$fac <- as.factor(fac) rm1 <- gam(y ~ s(fac,bs="re")+s(x0)+s(x1)+s(x2)+s(x3),data=dat,method="ML") gam.vcomp(rm1) fv0 <- predict(rm1,exclude="s(fac)") ## predictions setting r.e. to 0 fv1 <- predict(rm1) ## predictions setting r.e. to predicted values </pre> <hr /><div style="text-align: center;">[Package <em>mgcv</em> version 1.8-28 <a href="00Index.html">Index</a>]</div> </body></html>