Peter Dutton
Peter Dutton

Reputation: 307

Formula manipulation in R

I'm trying to do something similar to the MASS:stepAIC function, for Gaussian models with both fixed and mixed effects. The idea is to make it completely generalizable to many fixed and mixed effects, since these tasks can be performed by hand trivially for a few.

I want to be able to write the formula as the input as you would for lmer:

`Y ~ x1 + x2 + (a|b) + (1|c)

I am unable to extract the information I need from the formula class. In addition I will need to be able to put a select number of the variable back into the lm and lmer functions. So I want to be able to extract the parts of the formula into:

data      fixed effects       mixed effects
Y              x1                 a|b
               x2                 1|c

I then need to be able to send an arbitrary set of fixed and mixed effects automatically to lm:

lm(y ~ x1)

Upvotes: 4

Views: 882

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270248

If we can assume precisely the form (variable|variable) for the mixed effect terms then:

library(gsubfn)
fo <- Y ~ x1 + x2 + (a|b) + (1|c)

mixed.vec <- strapplyc(format(fo), "[(] *(\\w+) *[|] *(\\w+) *[)]")[[1]]
mixed <- matrix(mixed.vec, byrow = TRUE, nc = 2)
fixed <- setdiff(all.vars(fo)[-1], mixed)

which gives the following:

> mixed
     [,1] [,2]
[1,] "a"  "b" 
[2,] "1"  "c" 
> fixed
[1] "x1" "x2"

Here mixed is a matrix whose first column holds the variables before the | and the second column holds the corresponding variables after the | .

Upvotes: 2

Related Questions