EVOLUTION-MANAGER
Edit File: smooth.construct.re.smooth.spec.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: Simple 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 smooth.construct.re.smooth.spec {mgcv}"><tr><td>smooth.construct.re.smooth.spec {mgcv}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Simple random effects in GAMs</h2> <h3>Description</h3> <p><code><a href="gam.html">gam</a></code> can deal with simple independent random effects, by exploiting the link between smooths and random effects to treat random effects as smooths. <code>s(x,bs="re")</code> implements this. Such terms can can have any number of predictors, which can be any mixture of numeric or factor variables. The terms produce a parametric interaction of the predictors, and penalize the corresponding coefficients with a multiple of the identity matrix, corresponding to an assumption of i.i.d. normality. See details. </p> <h3>Usage</h3> <pre> ## S3 method for class 're.smooth.spec' smooth.construct(object, data, knots) ## S3 method for class 'random.effect' Predict.matrix(object, data) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>object</code></td> <td> <p>For the <code>smooth.construct</code> method a smooth specification object, usually generated by a term <code>s(x,...,bs="re",)</code>. For the <code>predict.Matrix</code> method an object of class <code>"random.effect"</code> produced by the <code>smooth.construct</code> method.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>a list containing just the data (including any <code>by</code> variable) required by this term, with names corresponding to <code>object$term</code> (and <code>object$by</code>). The <code>by</code> variable is the last element.</p> </td></tr> <tr valign="top"><td><code>knots</code></td> <td> <p>generically a list containing any knots supplied for basis setup — unused at present.</p> </td></tr> </table> <h3>Details</h3> <p>Exactly how the random effects are implemented is best seen by example. Consider the model term <code>s(x,z,bs="re")</code>. This will result in the model matrix component corresponding to <code>~x:z-1</code> 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. </p> <p>If the nature of the random effect specification is not clear, consider a couple more examples: <code>s(x,bs="re")</code> results in <code>model.matrix(~x-1)</code> being appended to the overall model matrix, while <code>s(x,v,w,bs="re")</code> would result in <code>model.matrix(~x:v:w-1)</code> 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. </p> <p>If the random effect precision matrix is of the form <i>sum_j p_j S_j</i> for known matrices <i>S_j</i> and unknown parameters <i>p_j</i>, then a list containing the <i>S_j</i> can be supplied in the <code>xt</code> argument of <code><a href="s.html">s</a></code>. In this case an array <code>rank</code> should also be supplied in <code>xt</code> giving the ranks of the <i>S_j</i> matrices. See simple example below. </p> <p>Note that smooth <code>id</code>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). </p> <p>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 <code>gamm4</code> or <code><a href="gamm.html">gamm</a></code> may be better alternatives. Note also that <code><a href="gam.html">gam</a></code> will not support models with more coefficients than data. </p> <p>The situation in which factor variable random effects intentionally have unobserved levels requires special handling. You should set <code>drop.unused.levels=FALSE</code> in the model fitting function, <code><a href="gam.html">gam</a></code>, <code><a href="bam.html">bam</a></code> or <code><a href="gamm.html">gamm</a></code>, having first ensured that any fixed effect factors do not contain unobserved levels. </p> <h3>Value</h3> <p> An object of class <code>"random.effect"</code> or a matrix mapping the coefficients of the random effect to the random effects themselves. </p> <h3>Author(s)</h3> <p> Simon N. Wood <a href="mailto:simon.wood@r-project.org">simon.wood@r-project.org</a></p> <h3>References</h3> <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> <h3>See Also</h3> <p><code><a href="gam.vcomp.html">gam.vcomp</a></code>, <code><a href="gamm.html">gamm</a></code></p> <h3>Examples</h3> <pre> ## see ?gam.vcomp require(mgcv) ## simulate simple random effect example set.seed(4) nb <- 50; n <- 400 b <- rnorm(nb)*2 ## random effect r <- sample(1:nb,n,replace=TRUE) ## r.e. levels y <- 2 + b[r] + rnorm(n) r <- factor(r) ## fit model.... b <- gam(y ~ s(r,bs="re"),method="REML") gam.vcomp(b) ## example with supplied precision matrices... b <- c(rnorm(nb/2)*2,rnorm(nb/2)*.5) ## random effect now with 2 variances r <- sample(1:nb,n,replace=TRUE) ## r.e. levels y <- 2 + b[r] + rnorm(n) r <- factor(r) ## known precision matrix components... S <- list(diag(rep(c(1,0),each=nb/2)),diag(rep(c(0,1),each=nb/2))) b <- gam(y ~ s(r,bs="re",xt=list(S=S,rank=c(nb/2,nb/2))),method="REML") gam.vcomp(b) summary(b) </pre> <hr /><div style="text-align: center;">[Package <em>mgcv</em> version 1.8-28 <a href="00Index.html">Index</a>]</div> </body></html>