# How to do two way repeated Measures ANOVA using aov() in R?

I am attempting a 2-way ANOVA with repeated measures using the aov() function in R. I am trying to compare average heights ("X1" and "X2") of algae by treatment ("CODE") and site over time ("MONTH"). The data I entered into R is already averaged. Therefore each row = one observation per treatment, per code, per month (1-60). I have created a column called "ID" to identify each observation (1-60).

ID MONTH SITE CODE  X1       X2
1   OCT  BPT   C+  3.526667 3.440000
2   OCT  BPT   C-  3.296667 3.540000
3   OCT  BPT   U+  2.146667 1.000000
4   OCT  BPT   U-  3.146667 3.016667
5   OCT  BPT   P   2.827778 2.122222
6   OCT  FLC   C+  3.620000 1.990000

However, when running this code:

"x1.aov<-aov(X1 ~ MONTH * SITE * CODE + Error(ID/(SITE * CODE)), data=HNME1)"

...

I am not receiving any p-values in my ANOVA summary. I have read other forums telling me to make the ID values into factors.

"HNME1$ID <- factor(HNME1$ID)"

I have tried this and received the error: "Error () model is singular." However, I do not have any gaps or missing data values. I am not sure what else could be going wrong. Just messing around - I tried a one-way ANOVA with "MONTH" where "ID" was and this produced p-values...

Any suggestions/ help would be much appreciated! Thank you.

It is because you use averages for each condition. R thinks each unique combination of month, site and code has N=1 so it has not clue about the variance or the N. So, you should go back to the values that you had before you averaged and give those to R.

for example:

ID MONTH SITE CODE  X1       X2
1   OCT  BPT   C+  5.526667 5.440000
2   OCT  BPT   C+  1.526667 6.440000
3   OCT  BPT   C+  2.526667 2.440000
4   NOV  BPT   C+  6.526667 3.440000
5   NOV  BPT   C+  4.526667 2.440000
6   NOV  BPT   C+  0.526667 3.440000

...etc. Looks like it will be a very long table :)

• Thank you Dan! Is there any way to not do this? I already averaged each by Code in Excel to enter into R. Can I change the ID number to tell R that ? Or does it need each individual observation to be able to calculate variance depending on N? – Tanya Nicole Ramseyer Feb 27 '17 at 16:27
• Also --- do you know what I need to include in the Error () formula? I was reading about between and within-subjects variables and I definitely confused myself trying to relate the example to my own experimental design. I believe Site and Code are my between-subject variables and Month is within-subjects. Any ideas? Thanks! – Tanya Nicole Ramseyer Feb 27 '17 at 16:31
• Your ID's are not just basically rownumbers right? I mean, each alga ID occurs several times? This is what would make it repeated measures, comparing alga 1 with other measures of alga 1 during another month. So it may be X1 ~ MONTH * SITE * CODE + Error(ID/MONTH). But, I could be misunderstanding your experimental design. Have a look at r-bloggers.com/two-way-anova-with-repeated-measures if you havent seen it yet. – DanB Feb 28 '17 at 1:02
• Dan, thank you! Yes, each alga ID occurs several times (over 4 months). I will take a look at that website. I believe "month" would be my within-subjects variable because I am testing the same subjects every month (for 4 consecutive months). I did not move my experiment every month, I think that is what makes this repeated measures. Code and site would be between-month subjects because I am testing different subjects at each level. IV's: Site (Three sites: BPT, FLC, SHR) Code/treatment (Factorial: 2x2 = C-, C+, U-, U+) Month (Four months: Oct, Nov, Dec, Jan) DV: Average Dictyota height – Tanya Nicole Ramseyer Feb 28 '17 at 1:55
• Hi Dan, I am still having issues determining which combination of variables /effects go into the Error() function. I have tried many different combinations and have read different things online/in my R book. I believe the repeated measure (time/month in my case) would go into the error function, but I have read that your grouping variable goes into the error function. I just want to make sure I am doing this correctly for a publication. Your example : (ID/MONTH) worked, but I have seen other examples that used Site or some other grouping variable before the RM variable (ex: (SITE/MONTH)). – Tanya Nicole Ramseyer Mar 22 '17 at 20:55