lme4 (version 1.0-6)

modular: Modular functions for mixed model fits

Description

Modular functions for mixed model fits

Usage

lFormula(formula, data = NULL, REML = TRUE, subset,
    weights, na.action, offset, contrasts = NULL,
    control = lmerControl(), ...)

mkLmerDevfun(fr, X, reTrms, REML = TRUE, start = NULL, verbose = 0, control = lmerControl(), ...)

optimizeLmer(devfun, optimizer = "Nelder_Mead", restart_edge = FALSE, start = NULL, verbose = 0L, control = list())

glFormula(formula, data = NULL, family = gaussian, subset, weights, na.action, offset, contrasts = NULL, mustart, etastart, control = glmerControl(), ...)

mkGlmerDevfun(fr, X, reTrms, family, nAGQ = 1L, verbose = 0L, control = glmerControl(), ...)

optimizeGlmer(devfun, optimizer = "bobyqa", restart_edge = FALSE, verbose = 0L, control = list(), nAGQ = 1L, stage = 1, start = NULL)

updateGlmerDevfun(devfun, reTrms, nAGQ = 1L)

Arguments

...
other potential arguments.
control
a list giving (for [g]lFormula) all options (see lmerControl for running the model; (for mkLmerDevfun,mkGlmerDevfun) options for inner optimization step; (for optim
fr
A model frame containing the variables needed to create an lmerResp or glmResp instance
X
fixed-effects design matrix
reTrms
information on random effects structure (see mkReTrms)
REML
(logical) fit restricted maximum likelihood model?
start
starting values
verbose
print output?
devfun
a deviance function, as generated by mkLmerDevfun
nAGQ
number of Gauss-Hermite quadrature points
stage
optimization stage (1: nAGQ=0, optimize over theta only; 2: nAGQ possibly >0, optimize over theta and beta)
formula
a two-sided linear formula object describing both the fixed-effects and fixed-effects part of the model, with the response on the left of a ~ operator and the terms, separated by + operators, on the right. Random-effects
data
an optional data frame containing the variables named in formula. By default the variables are taken from the environment from which lmer is called. While data is optional, the package authors strongly
subset
an optional expression indicating the subset of the rows of data that should be used in the fit. This can be a logical vector, or a numeric vector indicating which observation numbers are to be included, or a character vector of the r
weights
an optional vector of prior weights to be used in the fitting process. Should be NULL or a numeric vector.
na.action
a function that indicates what should happen when the data contain NAs. The default action (na.omit, inherited from the 'factory fresh' value of getOption("na.action")) strips any observations with any missi
offset
this can be used to specify an a priori known component to be included in the linear predictor during fitting. This should be NULL or a numeric vector of length equal to the number of cases. One or more
contrasts
an optional list. See the contrasts.arg of model.matrix.default.
optimizer
character - name of optimizing function(s). A character vector or list of functions: length 1 for lmer or glmer, possibly length 2 for glmer). The built-in optimizers are
restart_edge
logical - should the optimizer attempt a restart when it finds a solution at the boundary (i.e. zero random-effect variances or perfect +/-1 correlations)?
family
a GLM family, see glm and family.
mustart
optional starting values on the scale of the conditional mean, as in glm; see there for details.
etastart
optional starting values on the scale of the unbounded predictor as in glm; see there for details.

Value

  • lFormula, glFormula: A list containing components,
  • frmodel frame
  • Xfixed-effect design matrix
  • reTrmslist containing information on random effects structure: result of mkReTrms
  • REML(lFormula only): logical flag: use restricted maximum likelihood? (Copy of argument.)
  • mkLmerDevfun, mkGlmerDevfun: A function to calculate deviance (or restricted deviance) as a function of the theta (random-effect) parameters (for GlmerDevfun, of beta (fixed-effect) parameters as well). These deviance functions have an environment containing objects required for their evaluation. CAUTION: The output object of mk(Gl|L)merDevfun is an environment containing reference class objects (see ReferenceClasses, merPredD-class, lmResp-class), which behave in ways that may surprise many users. For example, if the output of mk(Gl|L)merDevfun is naively copied, then modifications to the original will also appear in the copy (and vice versa). To avoid this behavior one must make a deep copy (see ReferenceClasses for details). optimizeLmer: Results of an optimization.

Details

These functions make up the internal components of a(n) [gn]lmer fit.
  • [g]lFormulatakes the arguments that would normally be passed to[g]lmer, checking for errors and processing the formula and data input to create
  • mk(Gl|L)merDevfuntakes the output of the previous step (minus theformulacomponent) and creates a deviance function
  • optimize(Gl|L)mertakes a deviance function and optimizes overtheta(or overthetaandbeta, ifstageis set to 2 foroptimizeGlmer
  • updateGlmerDevfuntakes the first stage of a GLMM optimization (withnAGQ=0, optimizing overthetaonly) and produces a second-stage deviance function
  • mkMerModtakes theenvironmentof a deviance function, the results of an optimization, a list of random-effect terms, a model frame, and a model all and produces a[g]lmerModobject

Examples

Run this code
### Fitting a linear mixed model in 4 modularized steps

## 1.  Parse the data and formula:
lmod <- lFormula(Reaction ~ Days + (Days|Subject), sleepstudy)
names(lmod)
## 2.  Create the deviance function to be optimized:
(devfun <- do.call(mkLmerDevfun, lmod))
ls(environment(devfun)) # the environment of devfun contains objects required for its evaluation
## 3.  Optimize the deviance function:
opt <- optimizeLmer(devfun)
opt[1:3]
## 4.  Package up the results:
mkMerMod(environment(devfun), opt, lmod$reTrms, fr = lmod$fr)


### Same model in one line
lmer(Reaction ~ Days + (Days|Subject), sleepstudy)


### Fitting a generalized linear mixed model in six modularized steps

## 1.  Parse the data and formula:
glmod <- glFormula(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)
names(glmod)
## 2.  Create the deviance function for optimizing over theta:
(devfun <- do.call(mkGlmerDevfun, glmod))
ls(environment(devfun)) # the environment of devfun contains lots of info
## 3.  Optimize over theta using a rough approximation (i.e. nAGQ = 0):
(opt <- optimizeGlmer(devfun))
## 4.  Update the deviance function for optimizing over theta and beta:
(devfun <- updateGlmerDevfun(devfun, glmod$reTrms))
## 5.  Optimize over theta and beta:
opt <- optimizeGlmer(devfun, stage=2)
opt[1:3]
## 6.  Package up the results:
mkMerMod(environment(devfun), opt, glmod$reTrms, fr = glmod$fr)


### Same model in one line
glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)

Run the code above in your browser using DataCamp Workspace