Fit a linear mixed-effects model (LMM) to data, via REML or maximum likelihood.
lmer(formula, data = NULL, REML = TRUE, control = lmerControl(), start = NULL, verbose = 0L, subset, weights, na.action, offset, contrasts = NULL, devFunOnly = FALSE)
a two-sided linear formula object describing both the
fixed-effects and random-effects part of the model, with the
response on the left of a
an optional data frame containing the variables named in
logical scalar - Should the estimates be chosen to optimize the REML criterion (as opposed to the log-likelihood)?
a list (of correct class, resulting from
integer scalar. If
an optional expression indicating the subset of the rows
an optional vector of ‘prior weights’ to be used
in the fitting process. Should be
a function that indicates what should happen when the
this can be used to specify an a priori known
component to be included in the linear predictor during
fitting. This should be
an optional list. See the
logical - return only the deviance evaluation function. Note that because the deviance function operates on variables stored in its environment, it may not return exactly the same values on subsequent calls (but the results should always be within machine tolerance).
formula argument is specified as a character
vector, the function will attempt to coerce it to a formula.
However, this is not recommended (users who want to construct
formulas by pasting together components are advised to use
reformulate); model fits
will work but subsequent methods such as
update may fail.
When handling perfectly collinear predictor variables
(i.e. design matrices of less than full rank),
[gn]lmer is not quite as sophisticated
as some simpler modeling frameworks such as
glm. While it does
automatically drop collinear variables (with a message
rather than a warning), it does not automatically fill
NA values for the dropped coefficients;
these can be added via
This information can also be retrieved via
the deviance function returned when
TRUE takes a single numeric vector argument, representing
theta vector. This vector defines the scaled
variance-covariance matrices of the random effects, in the
Cholesky parameterization. For models with only simple
(intercept-only) random effects,
theta is a vector of the
standard deviations of the random effects. For more complex or
multiple random effects, running
theta vector for a fitted model and examining
the names of the vector is probably the easiest way to determine
the correspondence between the elements of the
and elements of the lower triangles of the Cholesky factors of the
An object of class
merMod (more specifically,
an object of subclass
lmerMod), for which many methods
are available (e.g.
In earlier version of the lme4 package, a
method argument was
used. Its functionality has been replaced by the
lmer(.) allowed a
family argument (to effectively
glmer(.)). This has been deprecated in summer 2013,
and been disabled in spring 2019.
lm for linear models;
glmer for generalized linear; and
nlmer for nonlinear mixed models.
## linear mixed models - reference values from older code (fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)) summary(fm1)# (with its own print method; see class?merMod % ./merMod-class.Rd str(terms(fm1)) stopifnot(identical(terms(fm1, fixed.only=FALSE), terms(model.frame(fm1)))) attr(terms(fm1, FALSE), "dataClasses") # fixed.only=FALSE needed for dataCl. ## Maximum Likelihood (ML), and "monitor" iterations via 'verbose': fm1_ML <- update(fm1, REML=FALSE, verbose = 1) (fm2 <- lmer(Reaction ~ Days + (Days || Subject), sleepstudy)) anova(fm1, fm2) sm2 <- summary(fm2) print(fm2, digits=7, ranef.comp="Var") # the print.merMod() method print(sm2, digits=3, corr=FALSE) # the print.summary.merMod() method (vv <- vcov.merMod(fm2, corr=TRUE)) as(vv, "corMatrix")# extracts the ("hidden") 'correlation' entry in @factors ## Fit sex-specific variances by constructing numeric dummy variables ## for sex and sex:age; in this case the estimated variance differences ## between groups in both intercept and slope are zero ... data(Orthodont,package="nlme") Orthodont$nsex <- as.numeric(Orthodont$Sex=="Male") Orthodont$nsexage <- with(Orthodont, nsex*age) lmer(distance ~ age + (age|Subject) + (0+nsex|Subject) + (0 + nsexage|Subject), data=Orthodont)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.