[R] updating arguments of formulae
Meyners, Michael, LAUSANNE, AppliedMathematics
Michael.Meyners at rdls.nestle.com
Thu Dec 10 15:04:15 CET 2009
Moreno,
I leave the discussion on the mixed models to others (you might consider
the SIG group on mixed models as well for this), but try a few hints to
make your code more accessible:
* The "." in updating a formula is substituted with the respective old
formula (depending on the side), but is not mandatory. You could give
the new formula explicitly, i.e. consider something like
model1 = update(model, . ~ (1 |Sb2) + OS)
if you loose control about your models. See ?update.formula
* I don't see the need for using your construct with
as.formula(paste()), this makes things unnecessarily complicated. See my
above example, which should work as well on your data (and see ?update)
* There is also the "-" operator available in update.formula to remove
terms (because it uses formula, see ?formula). As to your question on
how to move from
> depM ~ OS + (1 + OS | Sb2)
> to
> depM ~ OS + VR + (1 + OS + VR | Sb2)
try something like
update(model1, .~. - (1 + OS|Sb2) + VR + (1 + OS + VR | Sb2))
while it goes without saying that in this case, it would be easier to
drop the "." and use something like
update(model1, .~ OS + VR + (1 + OS + VR | Sb2))
directly.
* paste accepts more than just two arguments to be pasted: Try somthing
like
model2 = update(model1, as.formula(paste(". ~ . + (1 + ", "OS", "|" ,
"Sb2", ")"))
instead of your construct with several nested calls to paste, and see
?paste. (Note that I added quotes to "OS" and "Sb2", it didn't work for
me otherwise as I have no object OS, not sure what happens if you have
such an object on our search path, but I would suspect you encounter
problems as well.)
If you work yourself through these and thereby simplify your code, you
are more likely to get responses to your questions on which model to use
(which is actually independent from the use of update). As far as I see
it, it doesn't make sense to use a formula like in your model4, but the
mixed model experts might tell me wrong (and I got a bit lost in your
code as well). Please also try to provide commented, minimal,
self-contained, reproducible code for further enquiries (use e.g. one of
the examples on ?lmer to create appropriate examples for your
questions).
HTH, Michael
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Moreno Ignazio Coco
> Sent: Donnerstag, 10. Dezember 2009 13:35
> To: R-help at r-project.org
> Subject: [R] updating arguments of formulae
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list