remove_fixef: Remove specified fixed-effect terms from a model formula

remove_fixefR Documentation

Remove specified fixed-effect terms from a model formula

Description

As the Title says.

Usage

remove_fixef(formula, remove, nofixef = NULL, keep_intercept = TRUE, keep_offset = TRUE)

Arguments

formula

The model formula from which terms should be removed.

remove

The fixed-effect terms to be removed, specified either as a formula from which fixed-effect terms will be deduced (any random effet in such a formula will be ignored), or as a vector of character strings. The variable names should match those in the formula argument (not to be confused with the labels of fitted coefficients of fixed effect, when these differ, as for factor terms). Permutation of variables in interaction terms are handled (see Examples).

nofixef

Optional expression or formula allowing control of terms included in the returned formula, beyond fixed effects not first removed according to the remove argument (see Details). By default, these additional terms are all the offsets and random effects from the original formula. See Details for non-default usage.

keep_intercept

Boolean: whether to keep an original intercept in the returned formula (does not add one if not originally present).

keep_offset

Boolean: whether to keep the offsets in the returned formula.

Details

The following toy example explains the results with a non-default nofixef:

  form <- ~ offset() + a*b + (1|grp)
  remove_fixef(form, "a:b", nofixef=form)

which gives ~1 + a + b + offset(o) + a * b + (1 | grp).

First, fixed effect terms are extracted from the formula. Next, the terms implied by remove are removed, yielding an expression containing only other fixed-effect terms (here, 1 + a + b). Then, this expression is joined to the terms included in nofixef. The nofixef argument by default contains only the offset and random effect terms from the original formula (hence its name), but this example shows that anything in nofixef, including fixed effects, is included in the result.

Value

A model formula. The response variable is retained in it if it was present in the input formula.

Examples

remove_fixef( y ~ a*b, ~ a*b) #  y ~ 1, different from:
# remove_fixef(y ~ a*b, "a*b") # Warning and nothing removed 
# (because a*b not in terms(formula), which instead contains a, b and a:b)

remove_fixef( y ~ a*b, ~ a*b) # => y ~ 1, same result as:
remove_fixef( y ~ a*b, ~ b*a)   

remove_fixef( y ~ a*b, "a:b") # Same result as:
remove_fixef( y ~ a*b, "b:a") # a:b removed

# Remove all fixed effects term by setting 'remove' equal to 'formula':
form <- ~ offset() + a*b + (1|grp)
remove_fixef(form, form)
remove_fixef(form, form, keep_intercept = FALSE, keep_offset = FALSE)

spaMM documentation built on Sept. 10, 2026, 1:07 a.m.