[R] updating arguments of formulae

Moreno Ignazio Coco M.I.Coco at sms.ed.ac.uk
Thu Dec 10 13:35:24 CET 2009


Dear R-Community,

I am relatively new with R, so sorry for things which for you might be  
obvious...
I am trying to automatically update lmer formulae.

the variables of the model are:

depM= my dependent measure
Sb2= a random factor
OS = a predictor
VR= another predictor

So, I am building the first model with random intercept only:

model = lmer(depM ~ (1 |Sb2))

then I update the formula adding the first predictor

model1 = update(model, as.formula(paste(". ~ . + ", OS)))

the resulting formula will be:

depM ~ (1 |Sb2) + OS

let suppose now I want to update the model to have OS both as a fixed  
effect and in the random term, something like:

depM ~ (1 + OS |Sb2) + OS

I can do something very ugly (please tell me if there is a  more  
elegant way to do it) that looks like:

model2 = update(model1, as.formula(paste(paste(paste(paste(". ~ . + (1  
+ ", OS), "|" ), Sb2), ")")))

the resulting model2 formula will be:

depM ~ (1 |Sb2) + OS + (1 + OS | Sb2)

one first thing I am wondering at this point is whether having
(1 |Sb2) and (1 + OS | Sb2) in the same expression is redundant.
in the output it will obviously tell me that group Sb2 is considered twice:

number of obs: 6514, groups:  Sb2, 23; Sb2, 23

and i am not sure if am doing it correctly...any advice?

So let suppose now I want to add the new predictor VR again both in  
the fixed and in the random part of the formula.
If i just repeat the two steps above:

model3 = update(model2, as.formula(paste(". ~ . + ", VR)))

and then:

model4 = update(model3, as.formula(paste(paste(paste(paste(". ~ . + (1  
+ ", VR), "|" ), Sb2), ")")))

the formula I get is:

depM ~ (1 |Sb2) + OS + (1 + OS | Sb2) + VR + (1 + VR | Sb2)

so, basically I am adding new stuff on the right side of the formula...

My first question at this point is whether the above formula is equivalent to:

depM ~ OS + VR + (1 + OS + VR | Sb2)

if is not equivalent, which one of the two is correct?

obviously in the second case, group Sb2, is considered only once.

If the second version of the formula is the correct one, I don't  
understand how I can update arguments inside the formula rather than  
adding things on his right side...

thus, in the ideal case,  how do I go from something like this:

depM ~ OS + (1 + OS | Sb2)

to something like this:

depM ~ OS + VR + (1 + OS + VR | Sb2)

Thanks a lot for your help,
Best,

Moreno


























-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.




More information about the R-help mailing list