'Multiple comparisons in ANCOVA in R using contrast statements
My ANCOVA was going very well untill I discovered statistical differences and wanted to know which levels of Treatment differ significantly from each other over time.
I have the following lmer model (one of many):
model <- lmer(Value ~ Treatment + time + Treatment*time + (1|Subject)+(1|time), data = df)
anova(model)
Type III Analysis of Variance Table with Satterthwaite's method
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
Treatment 9330.1 3110.04 3 127.23 13.7325 8.152e-08 ***
time 81.8 81.76 1 4.00 0.3610 0.58033
Treatment:time 2461.3 820.43 3 232.00 3.6226 0.01382 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
contrasts(model$Treatment)
B C D
A 0 0 0
B 1 0 0
C 0 1 0
D 0 0 1
In this scheme it compares all factor levels to the reference level (A)
This is the first step, but I would like to compare all levels with each other.
Is there an elegant way to code all these comparisons and be able to see them at once?
What ive tried so far:
contrast1 <- c(1, -1, 0,0) ### I know it was easier to create a matrix here instantly, but I was trying to work with seperate contrasts at first so this was quicker for me
contrast2 <- c(1, 0, -1,0)
contrast3 <- c(1, 0, 0,-1)
contrast4 <- c(0, 1, -1,0)
contrast5 <- c(0, 1, 0,-1)
contrast6 <- c(0, 0, 1,-1)
conmat <- cbind(contrast1,contrast2,contrast3,contrast4,contrast5,contrast6)
cont <- ginv(t(conmat))
contrasts(model$Treatment) <- cont
The problem is that this approach does not work since it drops the last 3 contrasts:
contrasts(model$Treatment)
[,1] [,2] [,3]
A 2.500000e-01 2.500000e-01 2.500000e-01
B -2.500000e-01 -1.387779e-17 -8.326673e-17
C -1.526557e-16 -2.500000e-01 4.163336e-17
D 1.387779e-17 -9.714451e-17 -2.500000e-01
I have also tried changing the reference level, this works but I feel like there should be a faster way to check all my comparisons.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
