# How do I perform this complicated ANOVA type analysis in R?

I have the following type of dataset:

        |          |             |            variable_r         |
subject |  gender  |  age_group  |     Cond_1    |     Cond_2    |
--------|----------|-------------|---------------|---------------|
1    |    m     |      1      | r (A) | r (B) | r (A) | r (B) |
2    |    f     |      2      | r (A) | r (B) | r (A) | r (B) |
...
8    |    f     |      2      | r (A) | r (B) | r (A) | r (B) |


So two genders, two age groups, two conditions (Cond_1 and Cond_2) under which the experiment was done and two ways the subjects were prompted (A and B). r is the numerical result from each experiment. So two within-subject variables (prompt A/B and Cond 1/2) and two between-subject variables (age group 1/2 and gender m/f) (right?). I should calculate the statistically significant effects of each variable and their interactions.

How can I do this in R (or Python)? My googling found a lot of information about different types of ANOVA analyses, but I wasn't able to apply that information to my case.

Thanks!

e: the subjects were tested 4 times

• It is a bit unclear what you mean with 'prompted'. Do you mean subjects were tested four times, twice in each condition? I suggest mixed modelling in R would be best, e.g. lmer(response~genderagecondition,random=~1|subject/condition,data) and contrasts to investigate your specific questions of interest. – crazjo Aug 14 '14 at 7:48
• A page of anova specification in R that might be helpful. Your case is probably somewhere in the second half. – conjugateprior Aug 14 '14 at 8:41
• @JolJols I'm sorry I was unclear. Yes, the subjects were tested four times. Thank you for the suggestion, I will look into it. – hsintone Aug 14 '14 at 8:43
• @conjugateprior This looks helpful too, I will read through it. Thank you. – hsintone Aug 14 '14 at 8:47

yes, this is entirely possible in R. Not only that, there are many ways of doing it. One way that I would suggest is with a mixed linear model from the package 'nlme'

You would need to structure your data (dt) so that there were four observations for each participant. Something like this:

Subject Gender  Age Condition   DV
1       1       2   1a          X
1       1       2   1b          X
1       1       2   2a          X
1       1       2   2b          X
2       1       1   1a          X
2       1       1   1b          X
2       1       1   2a          X
2       1       1   2b          X
3       2       2   1a          X
3       2       2   1b          X
3       2       2   2a          X
3       2       2   2b          X


There should be an option within the package to automatically convert your dataset. After that, use something like the following code:

library(nlme)
lme(DV ~ Gender + Age + Condition, data = dt, random = ~ 1 | Subject)


Hope this helps point you in the right direction!

• Should the random argument be "random = ~ 1 | subject"? I got an "object 'subject' not found" error with just "random = 1 | subject". Or could the problem be somewhere else? – hsintone Aug 15 '14 at 7:23
• My apologies for the typo; you are correct. I was missing a "~". That part should read "random = ~ 1 | subject". – Xander Aug 15 '14 at 17:34