Parse a formula into fixed formula and random effect terms, treating 'special' terms appropriately

splitForm(
  formula,
  defaultTerm = "norm",
  allowFixedOnly = TRUE,
  allowNoSpecials = TRUE,
  debug = FALSE
)

Arguments

formula

a formula containing special random effect terms

defaultTerm

default type for non-special RE terms

allowFixedOnly

(logical) are formulas with no RE terms OK?

allowNoSpecials

(logical) are formulas with only standard RE terms OK?

debug

(logical) debug?

Value

a list containing elements fixedFormula; reTrmFormulas list of x | g formulas for each term; reTrmAddArgs list of function+additional arguments, i.e. list() (non-special), foo() (no additional arguments), foo(addArgs) (additional arguments); reTrmClasses (vector of special functions/classes, as character)

Details

Taken from Steve Walker's lme4ord, ultimately from the flexLambda branch of lme4 https://github.com/stevencarlislewalker/lme4ord/blob/master/R/formulaParsing.R. Mostly for internal use.

Author

Steve Walker

Examples

splitForm(~x+y)                     ## no specials or RE
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> NULL
#> 
#> $reTrmAddArgs
#> NULL
#> 
#> $reTrmClasses
#> NULL
#> 
splitForm(~x+y+(f|g))               ## no specials
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> f | g
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us"
#> 
splitForm(~x+y+diag(f|g))           ## one special
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> f | g
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us"
#> 
splitForm(~x+y+(diag(f|g)))         ## 'hidden' special
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> f | g
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us"
#> 
splitForm(~x+y+(f|g)+cs(1|g))       ## combination
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> f | g
#> 
#> $reTrmFormulas[[2]]
#> 1 | g
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> $reTrmAddArgs[[2]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us" "us"
#> 
splitForm(~x+y+(1|f/g))             ## 'slash'; term
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> 1 | g:f
#> 
#> $reTrmFormulas[[2]]
#> 1 | f
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> $reTrmAddArgs[[2]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us" "us"
#> 
splitForm(~x+y+(1|f/g/h))             ## 'slash'; term
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> 1 | h:g:f
#> 
#> $reTrmFormulas[[2]]
#> 1 | g:f
#> 
#> $reTrmFormulas[[3]]
#> 1 | f
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> $reTrmAddArgs[[2]]
#> us()
#> 
#> $reTrmAddArgs[[3]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us" "us" "us"
#> 
splitForm(~x+y+(1|(f/g)/h))             ## 'slash'; term
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> 1 | h:g:f
#> 
#> $reTrmFormulas[[2]]
#> 1 | g:f
#> 
#> $reTrmFormulas[[3]]
#> 1 | f
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> $reTrmAddArgs[[2]]
#> us()
#> 
#> $reTrmAddArgs[[3]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us" "us" "us"
#> 
splitForm(~x+y+(f|g)+cs(1|g)+cs(a|b,stuff))  ## complex special
#> $fixedFormula
#> ~x + y
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> f | g
#> 
#> $reTrmFormulas[[2]]
#> 1 | g
#> 
#> $reTrmFormulas[[3]]
#> a | b
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> $reTrmAddArgs[[2]]
#> us()
#> 
#> $reTrmAddArgs[[3]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us" "us" "us"
#> 
splitForm(~(((x+y))))               ## lots of parentheses
#> $fixedFormula
#> ~(((x + y)))
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> NULL
#> 
#> $reTrmAddArgs
#> NULL
#> 
#> $reTrmClasses
#> NULL
#> 
splitForm(~1+rr(f|g,n=2))
#> $fixedFormula
#> ~1
#> <environment: 0x55d0f15ee058>
#> 
#> $reTrmFormulas
#> $reTrmFormulas[[1]]
#> f | g
#> 
#> 
#> $reTrmAddArgs
#> $reTrmAddArgs[[1]]
#> us()
#> 
#> 
#> $reTrmClasses
#> [1] "us"
#>