Extended Processing of "Fake" Formulas

Description

Create a "fake" formula from a formula, a Formula, or a list of formulas. The function extracts all necessary variables (transformation of variables), to build a model.frame. The function also extracts all special model terms within the formulas, the information can be used to setup any special model term specification list.

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?

Value

Depending on the input formula, the function returns a formula or Formula. If onlyspecials = TRUE a 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)"