Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract components from mixed model (lme4) formula

Tags:

r

formula

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?

like image 681
herbps10 Avatar asked Dec 27 '12 04:12

herbps10


2 Answers

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"
like image 187
sebastian-c Avatar answered Oct 01 '22 00:10

sebastian-c


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
like image 45
dardisco Avatar answered Oct 01 '22 02:10

dardisco