# Two way repeated measures anova: which factors are between and within?

I am not sure whether I should use a within only or mixed between-within design for ANOVA in the following case. I have 6 patients who all received two treatments. During the treatment the dependent variable is recorded three times at fixed intervalls. Obviously Time is a within predictor, but what about Treatment? I am interested in the difference between the treatments so it might be a between predictor, however because each patient receives both treatments there might be a correlation between the DV for each timepoint within the patients. How can I account for this?

Here is an example dataset:

df <- data.frame(Patient = as.factor(rep(1:3, each=6)),
Treatment = as.factor(rep(rep(1:2, each=3), 3)),
Time = as.factor(rep(1:3, 6)),
DV = c(rnorm(3, 3, 1),rnorm(3, 3.5, 1.2),
rnorm(3, 3, 1),rnorm(3, 3.5, 1.2),
rnorm(3, 3, 1),rnorm(3, 3.5, 1.2)))


so the data for the first patient looks like:

> head(df)
Patient Treatment Time       DV
1       1         1    1 2.957971
2       1         1    2 4.232659
3       1         1    3 1.566545
4       1         2    1 3.674350
5       1         2    2 3.222795
6       1         2    3 1.979652


Here is how I used the function ezANOVA:

library(ez)

# within-only
df$wid <- as.factor(paste(df$Patient, df\$Treatment, sep="."))
mod1 <- ezANOVA(data = df, dv = DV, wid = wid, within = .(Time, Treatment))

# mixed withind and between
mod2 <- ezANOVA(data = df, dv = DV, wid = Patient, within = Time, between = Treatment.)

# this doens't work ("One or more cells is missing data.")
mod3 <- ezANOVA(data = df, dv = DV, wid = wid, within = .(Time, Treatment), between = Treatment)
mod3 <- ezANOVA(data = df, dv = DV, wid = Patient, within = .(Time, Treatment), between = Treatment)


Which one is right? Am I right to be worried about this second within correlation for treatments?

Bonus question: how can I access the model residuals with ezANOVA? The returned objects do not contain an aov object, only the tests results:

names(mod1)
 "ANOVA" "Mauchly's Test for Sphericity" "Sphericity Corrections"


Suggestions for other packages are also welcome.

• Treatment is a within factor because the sane individuals are in both treatments. It is no different than the time factor in this way. – dbwilson May 27 '18 at 12:18
• I am unfamiliar with ezANOVA. Using the "aov" function, the code would be: aov(dv ~ (Time * Treatment) + Error(wid/(Time * Treatment)). – dbwilson May 27 '18 at 14:22
• Thanks @dbwilson for your answer, now it seems obvious to me but I might somehow have been confused by "within" factors preventing me from looking at the difference between both treatments, which is of course not the case. Thank you also for the aov formula (I found out that it should be aov(dv ~ (Time * Treatment) + Error(Patient/(Time * Treatment)) because wid is specific to each combination of Patient and Treatment), it is nice to see that it gives the same results as ezANOVA plus I can access the model residuals. – jkd May 27 '18 at 15:34
• Correct. I assumed that Patient was a unique patient ID. I'm glad this helped. – dbwilson May 27 '18 at 17:57