Extended Processing of "Fake" Formulas

Description

Create a simplified formula from a formula, a Formula, or a list of formulas. The result retains the variables and transformations needed to build a model.frame while separating out special model terms. This is mainly used internally when gamlss2 prepares formulas for fitting.

Usage

fake_formula(formula, specials = NULL,
  nospecials = FALSE, onlyspecials = FALSE)

Arguments

formula A formula, Formula, or a list of formulas.
specials Character, vector of names of special functions in the formula, see terms.formula.
nospecials Logical, should variables of special model terms be part of the "fake formula"?
onlyspecials Logical, should only the special model terms be returned?

Details

The function is primarily an internal helper for formula preprocessing in gamlss2, but it can also be useful for inspecting how complex formulas are decomposed before fitting.

Special model terms such as s(), te(), ti(), re(), or package-specific specials are identified and handled separately from ordinary variables. Depending on the control arguments:

  • if nospecials = FALSE, variables used inside registered special terms are reintroduced into the returned formula so they remain available in the model frame;

  • if nospecials = TRUE, the returned formula excludes the special terms and their variables;

  • if onlyspecials = TRUE, the function returns the names of the detected special terms instead of a formula object.

For multi-part or multi-parameter formulas, the output preserves the overall formula structure.

Value

Depending on the input, the function returns a formula or Formula. If onlyspecials = TRUE, a character vector or list of special model term names is returned.

See Also

gamlss2

Examples

library("gamlss2")

## basic formula
f <- y ~ x1 + x2 + log(x3)
ff <- fake_formula(f)
print(ff)
y ~ x1 + x2 + log(x3)
## including special model terms
f <- y ~ x1 + s(x2) + x3 + te(log(x3), x4)
ff <- fake_formula(f)
print(ff)
~x1 + x3 + x2 + log(x3) + x4
## multiple parts on the right-hand side
f <- y ~ x1 + s(x2) + x3 + te(log(x3), x4) | x2 + sqrt(x5)
ff <- fake_formula(f)
print(ff)
y ~ x1 + x3 + x2 + log(x3) + x4 | x2 + sqrt(x5)
## collapse all formula parts
print(formula(ff, collapse = TRUE))
y ~ x1 + x3 + x2 + log(x3) + x4 + (x2 + sqrt(x5))
print(formula(ff, collapse = TRUE, update = TRUE))
y ~ x1 + x3 + x2 + log(x3) + x4 + sqrt(x5)
## list of formulas
f <- list(
  y ~ x1 + s(x2) + x3 + te(log(x3), x4),
    ~ x2 + sqrt(x5),
    ~ z2 + x1 + exp(x3)
)
ff <- fake_formula(f)
print(ff)
y ~ x1 + x3 + x2 + log(x3) + x4 | x2 + sqrt(x5) | z2 + x1 + exp(x3)
## extract separate parts on the right-hand side
formula(ff, rhs = 1)
y ~ x1 + x3 + x2 + log(x3) + x4
formula(ff, rhs = 2)
y ~ x2 + sqrt(x5)
formula(ff, rhs = 3)
y ~ z2 + x1 + exp(x3)
## formula with multiple responses and multiple parts
f <- y1 | y2 | y3 ~ x1 + s(x2) + x3 + te(log(x3), x4) | x2 + ti(x5)
ff <- fake_formula(f)
print(ff)
y1 | y2 | y3 ~ x1 + x3 + x2 + log(x3) + x4 | x2 + x5
## list of formulas with multiple responses
f <- list(
  y1 ~ x1 + s(x2) + x3 + te(log(x3), x4),
  y2 ~ x2 + sqrt(x5),
  y3 ~ z2 + x1 + exp(x3) + s(x10)
)
ff <- fake_formula(f)

## extract only without special terms
ff <- fake_formula(f, nospecials = TRUE)
print(ff)
y1 | y2 | y3 ~ x1 + x3 | x2 + sqrt(x5) | z2 + x1 + exp(x3)
## extract only special terms
ff <- fake_formula(f, onlyspecials = TRUE)
print(ff)
[[1]]
[1] "s(x2)"          "te(log(x3),x4)"

[[2]]
character(0)

[[3]]
[1] "s(x10)"