[R-sig-ME] syntax lmer random effects
HDoran at air.org
Thu Jul 26 19:52:24 CEST 2007
Here are a few small examples given what you have provided below. To use lmer with an intercept only and random intercepts:
fit1 <- lmer(z ~ 1 + (1|ID), data, method='ML')
Now, to fit a model with a fixed effect, 'x', and to allow for the intercept and for the variable x to vary randomly, use the following:
fit2 <- lmer(z ~ x + (x|ID), data, method='ML')
Notice that the random terms are always (x|ID), where x is the variable you want to have as the random effect, followed by a "pipe" (|) and then the grouping variable. In this example the grouping variable is ID. So, you have a random variable x, "given", or "conditional on" ID.
Also notice I use method='ML'. You said you wanted to compare models and these models differ in their fixed effects. REML is the default estimation procedure, and so you need this portion of code.
> -----Original Message-----
> From: r-sig-mixed-models-bounces at r-project.org
> [mailto:r-sig-mixed-models-bounces at r-project.org] On Behalf
> Of Nathan Leon Pace, MD, MStat
> Sent: Thursday, July 26, 2007 1:39 PM
> To: r-sig-mixed-models at r-project.org
> Subject: [R-sig-ME] syntax lmer random effects
> Even after reading the various help files, I don¹t really
> understand the formatting of the grouping factor.
> My model is z ~ x*y with 1 grouping factor. I wish to compare
> models with only an intercept random effect, random effects
> for intercept and x, random effects for intercept and y,
> random effects for intercept plus x and y (independent and
> not independent), and random effects for all fixed effects
> (independent and not independent).
> My data frame has 360 rows with 24 levels of the grouping factor
> > sessionInfo()
> R version 2.5.1 (2007-06-27)
> attached base packages:
>  "stats" "graphics" "grDevices" "utils"
> "datasets" "methods"
> other attached packages:
> lme4 Matrix lattice
> "0.99875-4" "0.999375-0" "0.16-2"
> Insights will be appreciated.
> Nathan Leon Pace, MD, MStat
> University of Utah
> n.l.pace at utah.edu
> W: 801.581.6393
> F: 801.581.4367
> M: 801.205.1019
> [[alternative HTML version deleted]]
More information about the R-sig-mixed-models