Extract random formula from nlme objects

560 Views Asked by At

I'm trying to extract the random structure from models constructed using lme, but I can't seem to get anything other than the fixed formula. E.g.,

library(nlme)
fm1 <- lme(distance ~ age, Orthodont, random = ~ age | Subject)
deparse(terms(fm1))
# "distance ~ age"

This is possible for lmer using findbars():

library(lmerTest)
fm2 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
findbars(formula(fm2))
# [[1]]
# Days | Subject

I want to be able to extract:

# ~ age | Subject
# (Days | Subject)

I could potentially get at this using regexpr but I would also like this to apply to more complex structures (multiple random slopes, nested random variables, etc.), and that might include additive or random slopes. Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

You can access these by

fm1$call$fixed
# distance ~ age
fm1$call$random
# ~age | Subject