[R] lme with nested factor and random effect

Ben Bolker bbolker at gmail.com
Thu Dec 15 20:07:24 CET 2011

Mari Pesek <marifrances <at> gmail.com> writes:

> Hello all,
> I'm having difficulty with setting up a mixed model using lme in the
> nlme package. To summarize my study, I am testing for effects of
> ornamentation on foraging behavior of wolf spiders. I tested spiders
> at two different ages (penultimate vs. mature) and of two different
> phenotypes (one species tested lacks ornamentation throughout life
> [non-ornamented males] while the other acquires ornamentation upon
> maturation [i.e. brush-legged males]). I tested a sample of
> brush-legged and non-ornamented males (as both penultimates and
> matures) in 2009, and an additional sample of brush-legged males in
> 2010 (as both penultimates and matures again) because I had a very
> small sample of brush-legged males in 2009.
> I would like to set up my lme so the fixed effects are "age"
> (penultimate vs mature), "phenotype" (non-ornamented vs brush-legged),
> and "year" (2009 vs 2010) nested within "phenotype" to test for
> differences between the two samples of brush-legged males.
> Additionally I want to include "id" (a unique identification number
> given to each spider tested) as a random factor to account for testing
> each individual twice (once as a penultimate and once as a mature).
> So far I have the following code: lme(behavior ~ age*phenotype,
> random=~1|maturity/id, data)
> But I don't know how to include the code to nest year within phenotype
> while testing for all possible interactions. Any help would be greatly
> appreciated.

   I have some thoughts on this.  I think your best bet is

lme(behavior~age*phenotype*year, random=~age|id, data)

or possibly

lme(behavior~age*phenotype + phenotype:year, random=~age|id, data)

("crossing" for fixed effects is more or less equivalent to
creating an interaction.  You should also make sure that you
have converted 'year' to a factor rather than a numeric variable ...)

  but if you re-post this to the r-sig-mixed models at r-project.org list I will
answer more fully ...

  Ben Bolker

More information about the R-help mailing list