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] |
Maintainer: | Ben Bolker <[email protected]> |
License: | GPL-3 |
Version: | 0.4.0 |
Built: | 2024-11-02 18:17:06 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
expandGrpVar(f)
expandGrpVar(f)
f |
a language object (an atom of a formula) expandGrpVar(quote(x*y)) expandGrpVar(quote(x/y)) |
list of specials – taken from enum.R
findReTrmClasses()
findReTrmClasses()
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 )
mkReTrms( bars, fr, drop.unused.levels = TRUE, reorder.terms = TRUE, reorder.vars = FALSE, calc.lambdat = TRUE )
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 |
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 |
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()
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(findbars_x(~ 1 + s(x) + (f|g) + diag(x|y))) no_specials(~us(f|g))
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 ~ Days
nobars(Reaction ~ Days + (Days|Subject)) ## => Reaction ~ Days
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)