# successive difference (aka repeated) contrasts with reference group

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:

1. What is the improvement from sessions 1 to 2, and 2 to 3 for both devices
2. 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)  • Do you want to test the difference between the devices at level 3? How is this related to the intercept? – Sven Hohenstein Jul 26 '15 at 19:36 • Indeed, this is what I want. And for that purpose, level 3 should become the reference level, like in treatment coding – user2457406 Jul 26 '15 at 19:46 ## 1 Answer You can test Device B vs. Device A at Session 3 without changing the intercept. You have to create an individual contrast matrix that matches your research questions. First, we combine Session and Device into a new factor cond. D1 <- transform(D1, cond = interaction(Session, Device)) levels(D1$cond)
#  "1.A" "2.A" "3.A" "1.B" "2.B" "3.B"


Now, we specify the contrasts corresponding to the research questions.

mat1 <- matrix(c(-1/2, 1/2, 0, -1/2, 1/2, 0,
0, -1/2, 1/2, 0, -1/2, 1/2,
0, 0, -1, 0, 0, 1),
nrow = 3, byrow = TRUE)
colnames(mat1) <- levels(D1\$cond)
rownames(mat1) <- c("Session 2 vs. 1",
"Session 3 vs. 2",
"Device B vs. A | Session 3")
mat1
#                             1.A  2.A  3.A  1.B  2.B 3.B
# Session 2 vs. 1            -0.5  0.5  0.0 -0.5  0.5 0.0
# Session 3 vs. 2             0.0 -0.5  0.5  0.0 -0.5 0.5
# Device B vs. A | Session 3  0.0  0.0 -1.0  0.0  0.0 1.0


Rows one and two correspond to the tests of Session 2 vs. 1 and Session 3 vs. 1. The third row corresponds to the test of Device B vs. A at Session 3. Note that this is a -1 vs. 1 contrast at Session 3. The values at the other sessions are 0.

We have to transform this matrix into a contrast matrix for R. This can be don with ginv from the MASSpackage.

library(MASS)
Cmat <- ginv(mat1)
dimnames(Cmat) <- rev(dimnames(mat1))
#     Session 2 vs. 1 Session 3 vs. 2 Device B vs. A | Session 3
# 1.A      -0.6666667      -0.3333333               6.191464e-17
# 2.A       0.3333333      -0.3333333               8.808285e-17
# 3.A       0.3333333       0.6666667              -5.000000e-01
# 1.B      -0.6666667      -0.3333333               3.574644e-17
# 2.B       0.3333333      -0.3333333               9.578229e-18
# 3.B       0.3333333       0.6666667               5.000000e-01


This contrast matrix Cmat can be used for the regression.

summary(lm(Y ~ cond, D1, contrasts = list(cond = Cmat)))
cbind(1, Cmat)


The result:

Call:
lm(formula = Y ~ cond, data = D1, contrasts = list(cond = Cmat))

Residuals:
Min       1Q   Median       3Q      Max
-12.4879  -2.2356  -0.1177   2.0643  12.6545

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)                     80.5713     0.5631 143.075   <2e-16 ***
condSession 2 vs. 1            -33.0104     1.3794 -23.931   <2e-16 ***
condSession 3 vs. 2            -22.4074     1.3794 -16.244   <2e-16 ***
condDevice B vs. A | Session 3  -3.2739     1.9508  -1.678    0.096 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 6.169 on 116 degrees of freedom
Multiple R-squared:  0.9338,    Adjusted R-squared:  0.9321
F-statistic: 545.5 on 3 and 116 DF,  p-value: < 2.2e-16


As you can see the value of Device B is smaller that the one of Device A at Session 3. The difference is not significant.

Note that the intercept still corresponds to the average of all conditions.