I have a rather specific regular expressions problem that is causing me some grief. I have removed one or more fixed effects from a mixed model (either lme
or lme4
), and wish to remove the corresponding random slope(s). However, depending on the random structure, this may leave behind unnecessary +
symbols or, worse, leave nothing preceding the |
.
Take a list of random effects formulae from lme
and lme4
obtained using lme.model$call$random
and findbars(formula(lme4.model))
respectively:
random.structures = list(
"~ b | random1",
"(b | random1)",
"~ b + x1 | random1",
"(b + x1 | random1)",
"~ x1 + b| random1",
"(x1 + b| random1)",
"~ b + x1 + c | random1",
"(b+ x1 + c | random1)",
"~b + x1 + x2 | random1",
"(b + x1 + x2 | random1)",
"~ x1 + x2 + b | random1",
"(x1 + x2 + b | random1)"
)
I have removed the variables b
and c
from the fixed effects formula using dropterms
. Since they no longer exist as fixed effects, their random slopes should not be allowed to vary.
b
and c
can be removed from the random formulae above using the following line:
random.structures = lapply(random.structures, function(i) gsub("b|c", "", i))
Now, I wish to remove all leftover +
symbols, i.e., those that do not link variables.
Then, in the event there is a blank space between ~
or (
and |
, I wish to insert a 1
.
The desired output is
random.structures2 = list(
"~ 1 | random1",
"(1 | random1)",
"~ x1 | random1",
"(x1 | random1)",
"~ x1 | random1",
"(x1 | random1)",
"~ x1 | random1",
"(x1 | random1)",
"~ x1 + x2 | random1",
"(x1 + x2 | random1)",
"~ x1 + x2 | random1",
"(x1 + x2 | random1)"
)
I have fiddled with gsub
but just can't seem to get it right. For instance, this works:
gsub("(.*)\\+\\ |(.*)\\+(\\|)", "\\1", random.structures[[3]])
# Accounting for space or lack of space between + and |
But not for this:
gsub("(.*)\\+\\ |(.*)\\+(\\|)", "\\1", random.structures[[7]])
Alternately, if there is a preexisting function like dropterms
for random structures, I'm all in!
Similarly, I can't reliable insert a 1
in the blank space inbetween ~ |
or ( |
.