[R] Syntax for lme function to model random factors and interactions

Joshua Wiley jwiley.psych at gmail.com
Mon May 21 16:45:10 CEST 2012


Hi,

See inline below

On Mon, May 21, 2012 at 6:03 AM, i_like_macs <dkoya at mac.com> wrote:
> Hello,
>
> I have a question regarding the syntax of the lme function in the nlme
> package. What I'm trying to do is to calculate an estimate of R^2 based on
> the likelihood ratio test. For this calculation, I need to determine the
> maximum log-likelihood of the intercept-only model and the model of interest
> (with the desired factors and interactions).
>
> My model has four independent factors (i.e. A, B, C and D). A and B are
> fixed factors, whilst C and D are random factors.
>
> Presently, I'm trying to code the lme function for the full ANOVA model, but
> am unsure how to code both random factors. Additionally, I'm unsure whether
> I coded the interactions correctly (i.e. I'm unsure whether the interactions
> which contain random factors also need to be specified in the "random"
> argument). I've skimmed through the Pinheiro and Bates book, "Mixed-Effects
> Models in S and S-PLUS", but could not find an answer.

Do they need to be specified for the model to run?  No.  Do they need
to be specified for the model to make sense?  That depends on your
data, theory, and what question you are trying to answer.  I think you
would be better off posting this sort of question to
R-sig-mixedeffects list.  That said, it sounds to me like maybe it
would be easiest to spend some more time learning about mixed effects
models (I know they can be tricky) and then come back to trying to
specify the model in R.  We can help with the code aspect, but do not
typically provide statistical advice on what is or is not an
appropriate model.

>
> My R code for the lme function which includes all the main factors and
> interactions (2- and 3-way) so far is:
>
> lme(Y ~ A +
>                B +
>                C +
>                D +
>                A * B +
>                A * C +
>                A * D +
>                B * C +
>                B * D +
>                C * D +
>                A * B * C +
>                A * B * D +
>                A * C * D +
>                B * C * D,
>                data = myData,
>                random = ~ 1|C,
>                method = "ML")

note that this can be written much more succinctly as:

lme(Y ~ (A + B + C + D)^3, data = myData, random = ~ 1 | C, method = "ML")

which will expand to all three-way interactions and lower terms.  Have
you tried running this model?

Cheers,

Josh

>
> Can someone help me with how to code the random factors and interactions
> which contain these random factors please? I apologise in advance, that I am
> self-taught in statistics, and just started using R. Hence I hope my
> question made sense. Thank you very much.
>
> Daisuke Koya
> Confused PhD student
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Syntax-for-lme-function-to-model-random-factors-and-interactions-tp4630744.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list