Parse formula
parse_formula.RdParse a formula into its terms lhs ~ rhs where rhs can have two components separated by |.
For example, y ~ x | z has rhs = ~ x | z where x is typically described as the grouping term and z is the blocking term.
A fast, unsafe alternative to modeltools::ParseFormula().
Arguments
- formula
(formula)
The formula that should be parsed.- specials
(character:
NULL)
A character vector of functions informulathat should be flagged in the specials attribute whenterms.formula()is applied. Has no effect unless expansion runs.- data
(data frame)
Used to infer the meaning of the special symbol.informula. Can be adata.frameorlist.- terms
(scalar logical:
FALSE)
Whether or not to parse formula usingstats::terms.formula(). Expansion is also performed whenspecialsordatais non-NULL, even iftermsisFALSE.
Value
A named list with components:
formula: the (possiblyterms.formula-expanded) input formula.lhs: a one-sided formula containing the response, orNULLifformulais one-sided.rhs: a one-sided formula containing the full right-hand side.rhs_group: a one-sided formula containing the term left of|, orrhsif no top-level|is present.rhs_block: a one-sided formula containing the term right of|, orNULLif no top-level|is present.
Examples
#----------------------------------------------------------------------------
# parse_formula() example
#----------------------------------------------------------------------------
library(bkmodel)
# One-sided formula
parse_formula(~x)
#> $formula
#> ~x
#> <environment: 0x5ed292892260>
#>
#> $lhs
#> NULL
#>
#> $rhs
#> ~x
#> <environment: 0x5ed292892260>
#>
#> $rhs_group
#> ~x
#> <environment: 0x5ed292892260>
#>
#> $rhs_block
#> NULL
#>
# Two-sided formula
parse_formula(y ~ x)
#> $formula
#> y ~ x
#> <environment: 0x5ed292892260>
#>
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#>
#> $rhs
#> ~x
#> <environment: 0x5ed292892260>
#>
#> $rhs_group
#> ~x
#> <environment: 0x5ed292892260>
#>
#> $rhs_block
#> NULL
#>
# Two-sided formula with a top-level `|` splitting the rhs into group and block
parse_formula(y ~ x | z)
#> $formula
#> y ~ x | z
#> <environment: 0x5ed292892260>
#>
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#>
#> $rhs
#> ~x | z
#> <environment: 0x5ed292892260>
#>
#> $rhs_group
#> ~x
#> <environment: 0x5ed292892260>
#>
#> $rhs_block
#> ~z
#> <environment: 0x5ed292892260>
#>
# Dot expansion against the names in `data`
parse_formula(y ~ ., data = data.frame(y = 1, a = 1, b = 1))
#> $formula
#> y ~ a + b
#> <environment: 0x5ed292892260>
#>
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#>
#> $rhs
#> ~a + b
#> <environment: 0x5ed292892260>
#>
#> $rhs_group
#> ~a + b
#> <environment: 0x5ed292892260>
#>
#> $rhs_block
#> NULL
#>
# Functions named in `specials` are preserved through term expansion
parse_formula(y ~ s(x) + z, specials = "s")
#> $formula
#> y ~ s(x) + z
#> <environment: 0x5ed292892260>
#>
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#>
#> $rhs
#> ~s(x) + z
#> <environment: 0x5ed292892260>
#>
#> $rhs_group
#> ~s(x) + z
#> <environment: 0x5ed292892260>
#>
#> $rhs_block
#> NULL
#>