I'm trying to write a function in R that accepts a formula such as the following:
y ~ 1 + sex + age + (1 | school) + (1 | school:section)
Is there an easy way to extract the various components of this formula for use in my function? For example, I would like to be able to get the left hand side, each of the variables, the random effects variables and how they are nested, etc.
Is there an easier way to do this than walking the formula parse tree?
If you want a solution which doesn't require regex, I suggest you consider terms
.
form <- y ~ 1 + sex + age + (1 | school) + (1 | school:section)
terms(form)
## y ~ 1 + sex + age + (1 | school) + (1 | school:section)
## attr(,"variables")
## list(y, sex, age, 1 | school, 1 | school:section)
## attr(,"factors")
## sex age 1 | school 1 | school:section
## y 0 0 0 0
## sex 1 0 0 0
## age 0 1 0 0
## 1 | school 0 0 1 0
## 1 | school:section 0 0 0 1
## attr(,"term.labels")
## [1] "sex" "age" "1 | school" "1 | school:section"
## attr(,"order")
## [1] 1 1 1 1
## attr(,"intercept")
## [1] 1
## attr(,"response")
## [1] 1
## attr(,".Environment")
## <environment: R_GlobalEnv>
Furthermore you can extract attributes from it using attributes
:
attributes(terms(form))$term.labels
## [1] "sex" "age" "1 | school" "1 | school:section"
To expand on @Ben Bolker's suggestion:
f1 <- formula(y ~ 1 + sex + age + (1 | school) + (1 | school:section))
The left hand side (assuming one variable on left):
all.vars(terms(f1))[1] # character
The variables:
all.vars(delete.response(terms(f1))) # character
The random effects:
lme4:::findbars(f1) # returns list of language items
There's also the formula.tools
package for this, although it doesn't have methods specifically for mixed-effects models:
library(formula.tools)
lhs(f1)
r1 <- rhs.vars(f1) # gives fixed and random effects as character
r1[grepl("\\|", r1)] # character vector of random effects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With