| Title: | Machinery for Processing Random Effect Formulas |
|---|---|
| Description: | Takes formulas including random-effects components (formatted as in 'lme4', 'glmmTMB', etc.) and processes them. Includes various helper functions. |
| Authors: | Ben Bolker [aut, cre] (ORCID: <https://orcid.org/0000-0002-2127-0443>), Anna Ly [ctb] (ORCID: <https://orcid.org/0000-0002-0210-0342>) |
| Maintainer: | Ben Bolker <[email protected]> |
| License: | GPL-3 |
| Version: | 0.4.5 |
| Built: | 2026-05-28 20:55:27 UTC |
| Source: | https://github.com/bbolker/reformulas |
Detect whether there are any 'specials' in a formula term
anySpecial(term, specials = findReTrmClasses(), fast = FALSE)anySpecial(term, specials = findReTrmClasses(), fast = FALSE)
term |
formula term |
specials |
values to detect |
fast |
(logical) use quick (syntactic) test for presence of specials? |
logical value
## should only detect s as the head of a function, s(...) anySpecial(~diag(1)) anySpecial(~diag) anySpecial(~diag[[1]]) anySpecial(~diag[1]) anySpecial(~s) anySpecial(~s(hello+goodbye,whatever))## should only detect s as the head of a function, s(...) anySpecial(~diag(1)) anySpecial(~diag) anySpecial(~diag[[1]]) anySpecial(~diag[1]) anySpecial(~s) anySpecial(~s(hello+goodbye,whatever))
'||' notation into separate '|' termsFrom the right hand side of a formula for a mixed-effects model, expand terms with the double vertical bar operator into separate, independent random effect terms.
expandDoubleVerts(term)expandDoubleVerts(term)
term |
a mixed-model formula |
the modified term
formula, model.frame, model.matrix.
Other utilities:
mkReTrms(),
nobars(),
subbars()
apply operator expansion (e.g. a/b -> a + a:b) to a formula term
expandGrpVar(f)expandGrpVar(f)
f |
a language object (an atom of a formula) |
expandGrpVar(quote(x*y)) expandGrpVar(quote(x/y))expandGrpVar(quote(x*y)) expandGrpVar(quote(x/y))
list of specials – taken from enum.R
findReTrmClasses()findReTrmClasses()
Format columns corresponding to std. dev. and/or variance.
format_sdvar( reStdDev, use.c = "Std.Dev.", formatter = format, digits = max(3, getOption("digits") - 2), ... ) format_corr(x, maxdim = Inf, digits = 2, maxlen = 10, ...) get_sd(x, ...)format_sdvar( reStdDev, use.c = "Std.Dev.", formatter = format, digits = max(3, getOption("digits") - 2), ... ) format_corr(x, maxdim = Inf, digits = 2, maxlen = 10, ...) get_sd(x, ...)
reStdDev |
a vector of standard deviations. |
use.c |
a character vector indicating which scales to include. |
formatter |
formatting function. |
digits |
digits for format |
... |
additional parameters |
x |
a square numeric matrix |
maxdim |
maximum number of rows/columns to display |
maxlen |
maximum number of rows to display |
"format()" the VarCorr matrix of the random effects – for
print()ing and show()ing
formatVC( varcor, digits = max(3, getOption("digits") - 2), corr_digits = max(2, digits - 2), maxdim = 10, comp = "Std.Dev.", formatter = format, useScale = attr(varcor, "useSc"), ... )formatVC( varcor, digits = max(3, getOption("digits") - 2), corr_digits = max(2, digits - 2), maxdim = 10, comp = "Std.Dev.", formatter = format, useScale = attr(varcor, "useSc"), ... )
varcor |
a |
digits |
the number of significant digits for standard deviations and variances. |
corr_digits |
the number of significant digits for correlations. |
maxdim |
maximum dimensions (numbers of standard deviations/variances and number of rows of correlation matrices) to report per random effects term. |
comp |
character vector of length one or two indicating which columns out of "Variance" and "Std.Dev." should be shown in the formatted output. |
formatter |
the |
useScale |
whether to report a scale parameter (e.g. residual standard deviation). |
... |
optional arguments for |
a character matrix of formatted VarCorr entries from varcor.
get grouping variable symbols/names
get_grpvars(formula, return_val = c("char", "symbol"))get_grpvars(formula, return_val = c("char", "symbol"))
formula |
a formula |
return_val |
return character string or raw symbol? |
form <- Reaction ~ Days + (Days | group / Subject) get_grpvars(form) get_grpvars(form, return_val = "symbol")form <- Reaction ~ Days + (Days | group / Subject) get_grpvars(form) get_grpvars(form, return_val = "symbol")
Does every level of f1 occur in conjunction with exactly one level of f2? The function is based on converting a triplet sparse matrix to a compressed column-oriented form in which the nesting can be quickly evaluated.
isNested(f1, f2)isNested(f1, f2)
f1 |
factor 1 |
f2 |
factor 2 |
TRUE if factor 1 is nested within factor 2
if (requireNamespace("lme4")) { data("Pastes", package = "lme4") with(Pastes, isNested(cask, batch)) ## => FALSE with(Pastes, isNested(sample, batch)) ## => TRUE }if (requireNamespace("lme4")) { data("Pastes", package = "lme4") with(Pastes, isNested(cask, batch)) ## => FALSE with(Pastes, isNested(sample, batch)) ## => TRUE }
From the result of findbars applied to a model formula and
and the evaluation frame, create the model matrix, etc. associated with
random-effects terms. See the description of the returned value for a
detailed list.
mkReTrms( bars, fr, drop.unused.levels = TRUE, reorder.terms = TRUE, reorder.vars = FALSE, calc.lambdat = TRUE, sparse = NULL )mkReTrms( bars, fr, drop.unused.levels = TRUE, reorder.terms = TRUE, reorder.vars = FALSE, calc.lambdat = TRUE, sparse = NULL )
bars |
a list of parsed random-effects terms |
fr |
a model frame in which to evaluate these terms |
drop.unused.levels |
(logical) drop unused factor levels? |
reorder.terms |
arrange random effects terms in decreasing order of number of groups (factor levels)? |
reorder.vars |
arrange columns of individual random effects terms in alphabetical order? |
calc.lambdat |
(logical) compute |
sparse |
(logical) set up sparse model matrices? |
Lambdat, Lind, theta, lower are likely to
be useful only for lme4; the other terms can be generally useful for
constructing mixed-effect models
a list with components
Zt |
transpose of the sparse model matrix for the random effects |
Ztlist |
list of components of the transpose of the random-effects model matrix, separated by random-effects term |
Lambdat |
transpose of the sparse relative covariance factor |
Lind |
an integer vector of indices determining the mapping of the
elements of the |
theta |
initial values of the covariance parameters |
lower |
lower bounds on the covariance parameters |
flist |
list of grouping factors used in the random-effects terms |
cnms |
a list of column names of the random effects according to the grouping factors |
Gp |
a vector indexing the association of
elements of the conditional mode vector
with random-effect terms; if |
nl |
names of the terms (in the same order as |
ord |
an integer vector giving the relationship between the order of the terms in the formula and the terms in the final object (which are ordered by the number of levels in the grouping variable, if |
Bates D, Mächler M, Bolker B, Walker S (2015). "Fitting Linear Mixed-Effects Models Using lme4." Journal of Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01
Other utilities:
expandDoubleVerts(),
nobars(),
subbars()
## (silly/impractical formula, for illustration only) form <- mpg ~ 1 + (1|gear) + (factor(cyl)|gear) + (1 + hp | carb) fr <- model.frame(subbars(form), data = mtcars) rterms <- mkReTrms(findbars(form), fr) names(rterms) ## block sizes (latent variables per block) of each term (nperblock <- lengths(rterms$cnms)) ## latent variables per term (nperterm <- diff(rterms$Gp)) with(rterms, identical(unname(nl*nperblock), nperterm)) ## illustrate reordering of terms dd <- expand.grid(a = 1:7, b = 1:3, c = 1:5, d = 1:9) dd$y <- 1 form2 <- y ~ 1 + (1|a) + (1|b) + (1|c) + (1|d) rterms2 <- mkReTrms(findbars(form2), dd, reorder.terms = TRUE) ## reorder elements into original formula order with(rterms2, cnms[order(ord)]) ## reorder splitForm output to match mkReTrms components ss <- splitForm(form2) ss$reTrmFormulas[rterms2$ord]## (silly/impractical formula, for illustration only) form <- mpg ~ 1 + (1|gear) + (factor(cyl)|gear) + (1 + hp | carb) fr <- model.frame(subbars(form), data = mtcars) rterms <- mkReTrms(findbars(form), fr) names(rterms) ## block sizes (latent variables per block) of each term (nperblock <- lengths(rterms$cnms)) ## latent variables per term (nperterm <- diff(rterms$Gp)) with(rterms, identical(unname(nl*nperblock), nperterm)) ## illustrate reordering of terms dd <- expand.grid(a = 1:7, b = 1:3, c = 1:5, d = 1:9) dd$y <- 1 form2 <- y ~ 1 + (1|a) + (1|b) + (1|c) + (1|d) rterms2 <- mkReTrms(findbars(form2), dd, reorder.terms = TRUE) ## reorder elements into original formula order with(rterms2, cnms[order(ord)]) ## reorder splitForm output to match mkReTrms components ss <- splitForm(form2) ss$reTrmFormulas[rterms2$ord]
Drop 'specials' from a formula
no_specials(term, specials = c("|", "||", "s"))no_specials(term, specials = c("|", "||", "s"))
term |
a term or formula or list thereof |
specials |
function types to drop |
a call or language object (or list) with specials removed
no_specials(findbars_x(~ 1 + s(x) + (f|g) + diag(x|y))) no_specials(~us(f|g)) no_specials(~us(f|g, extra_arg))no_specials(findbars_x(~ 1 + s(x) + (f|g) + diag(x|y))) no_specials(~us(f|g)) no_specials(~us(f|g, extra_arg))
Remove the random-effects terms from a mixed-effects formula, thereby producing the fixed-effects formula.
nobars(term) nobars_(term)nobars(term) nobars_(term)
term |
the right-hand side of a mixed-model formula |
the fixed-effects part of the formula
This function is called recursively on individual
terms in the model, which is why the argument is called term and not
a name like form, indicating a formula.
formula, model.frame, model.matrix.
Other utilities:
expandDoubleVerts(),
mkReTrms(),
subbars()
nobars(Reaction ~ Days + (Days|Subject)) ## => Reaction ~ Daysnobars(Reaction ~ Days + (Days|Subject)) ## => Reaction ~ Days
Remove all random slopes from a formula, while retaining random intercepts.
randint(form)randint(form)
form |
A formula |
The new formula
f <- ~ 1 + a + b + (a | f) + (1 + a | g) + (a + b | h ) + (1 + a + b | i) randint(f)f <- ~ 1 + a + b + (a | f) + (1 + a | g) + (a + b | h ) + (1 + a + b | i) randint(f)
extract right-hand side of a formula
RHSForm(form, as.form = FALSE)RHSForm(form, as.form = FALSE)
form |
a formula object |
as.form |
(logical) return a formula (TRUE) or as a call/symbolic object (FALSE) ? |
a language object
RHSForm(y ~ x + (1|g))RHSForm(y ~ x + (1|g))
Substitute the '+' function for the '|' and '||' function in a mixed-model formula. This provides a formula suitable for the current model.frame function.
subbars(term)subbars(term)
term |
a mixed-model formula |
the formula with all | and || operators replaced by +
This function is called recursively on individual
terms in the model, which is why the argument is called term and not
a name like form, indicating a formula.
formula, model.frame, model.matrix.
Other utilities:
expandDoubleVerts(),
mkReTrms(),
nobars()
subbars(Reaction ~ Days + (Days|Subject)) ## => Reaction ~ Days + (Days + Subject)subbars(Reaction ~ Days + (Days|Subject)) ## => Reaction ~ Days + (Days + Subject)