I am currently trying to analyse data from an experiment of mine and I have done some searching for instructions on the usage of the lme() function for R, since I am looking to analyse my data with a linear mixed effects approach. However,
- In case lme() does work out, I am not sure how to fit a model to my data.
- In case there are better options than lme(), I would be glad about any help regarding the proper use of these functions.
Subjects in my experiment were randomly assigned to one of two contexts (between subjects factor B) and within this context all of the subjects completed a task which was constructed as a combination of two within subjects factors (W1 and W2). Additionally, subjects have filled a personality questionnaire, thus each subjects has a value for this, which I need to include in my analysis (Pers). The outcome variable is a continuous one (reaction time, RT). The data look like this:
B Pers subject W1 W2 RT
A 23 1 m x 187
A 23 1 m y 333
A 23 1 n x 112
A 23 1 n y 110
A 5 2 m x 313
... ... ... ... ... ...
B 11 19 m x 911
B 11 19 m y 248
B 11 19 n x 411
B 11 19 n y 212
B -9 20 m x 666
... ... ... ... ... ...
I am interested in an analysis giving me information about the interactions of all four factor (B, Pers, W1 and W2), thus four-way interaction.
When including only B, it seems to me that I understand what to do when using lme(). My code looks like this (nb: I constructed contrasts between the different conditions before the actual lme() code using the contrasts() function):
> baselinemodel = lme(RT ~ 1, random = ~1|subject/W1/W2, data = df, method = "ML")
> W1model = update(baseline, .~. +W1)
> W2model = update(W1model, .~. +W2)
> Bmodel = update(W2model, .~. +B)
> W1W2mod = update(Bmodel, .~. +W1:W2)
> W1Bmod = update(W1W2mod, .~. +W1:B)
> W2Bmod = update(W1Bmod, .~. +W2:B)
> fullmodel = update(W2bmodel, .~. +W1:W2:B)
I.e. I set up a basline model including only the intercept and specifying W1 and W2 as nested within subjects (hence defining them as within subjects factors) and then in a sequential manner I include more predictors and their interactions in order to finally be able to compare these models using the anova() function:
> anova(baseline, W1model, W2model, Bmodel, W1W2mod, W1Bmod, W2Bmod, fullmodel)
Naturally I also take a look at my predefined contrasts using
> summary(fullmodel)
I find this approach very intuitive, as long as I do not try to include the continuous predictor Pers in addition to B. So this is where I am stuck. I would be deeply grateful if anyone could
explain to me how to include Pers in addition to B, W1 and W2 (i.e. how to include a continuous predictor varying between subjects, and its interactions with the other factors into my analysis)
and in case lme() is not the optimal function for my objectives how to do a comparable analysis with another function (thus broadening my still quite narrow scope of R)
I have, naturally done some searching before starting to post this question, but unfortunately others, who seemed to have related issues weren't too successful in getting answers (https://stats.stackexchange.com/questions/97669/analysis-of-a-mixed-design-with-categorical-and-continuous-variables)
Thank you in advance