Solved – R multcomp: contrasts for Tukey

anovapost-hocr

I want to use the glht() function from package multcomp.

In the linfct argument, I provide my linear functions to be tested.
I provide contrasts there, such as (for example)
c("Group1 - Group2 = 0", "Group1 - Group3 = 0")
to compare group no 1 against the other groups.

I want to do Tukey's test in this fashion. I saw I could do
(for example)
mcp(Group='Tukey')
for this, but this does all the pairwise comparisons and I want only
specific contrasts. How can I do it?

Best Answer

I found a possibility myself by using function contrMat(). For example, one can do

M   <- aov(y ~ x - 1, data = ex)             # the model
cM1 <- contrMat(n=table(ex$x), type='Tukey') # the full contrast matrix
cM2 <- cM1[1:2,]                             # 1 vs 2 and 1 vs 3 only
summary(glht(M, linfct = cM2))               # 1 vs 2 and 1 vs 3 only

where ex is

structure(list(y = c(2.85, 3.1, 3.01, 3.04, 2.91, 2.91, 3.52, 3.56, 3.67, 3.3, 3.36, 4.89, 5.12, 4.78, 4.67, 4.96, 5.12), x = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor")), .Names = c("y", "x"), row.names = c(NA, -17L), class = "data.frame")

I found this link helpful.