I have data from an experiment where performance y on two different devices was measured over three successive sessions. The 2x3 design is completely within-subject (see simulation code at the bottom). It is assumed that participants improve with every session due to training.
Two research questions (RQ) are crucial:
- What is the improvement from sessions 1 to 2, and 2 to 3 for both devices
- How do both devices compare at the last session
My problem regards the contrasts to use. Regarding RQ1, I would choose for successive difference (aka repeated) contrasts, as is provided by the MASS library:
library(MASS)
contrasts(D1$Session) <- contr.sdif(3)
That gives me the desired indicators for successive improvement. However, the intercept becomes the overall mean (for the reference level of device), which is not what I want.
How can I create custom contrasts, that combines successive differences with a reference level?
Below is a simulation of the data set. In real, I'm going use mixed-effect models to account for repeated measures, but that is not of concern here.
library(ggplot2)
library(dplyr)
library(plyr)
set.seed(42)
D1 = expand.grid(Subj = as.factor(1:20),
Device = c("A", "B")) %>%
join(expand.grid(Session = as.factor(1:3),
Device = c("A", "B")) %>%
mutate(T = c(1,.75,.5625, 1,.66,.44))) %>%
join(data.frame(Device = c("A", "B"), B = c(100, 120))) %>%
mutate(Y = rnorm(120, B * T, 1))
D1 %>%
ggplot(aes(x = Session, y = Y, fill = Device)) +
geom_boxplot()
D1 %>%
group_by(Device) %>%
summarize(mean(Y))
M1 = lm(Y ~ Session * Device, D1)
summary(M1)