[R-sig-eco] Two-way contrasts with adonis()

microbiomics microbiomics at mytng.de
Thu Mar 10 17:30:00 CET 2016


Hi Alexandre,

> f    <- length(levels(treat))

For brevity, you could use:

f <- nlevels(treat)





> comb <- t(combn(1:f, 2))
> n    <- nrow(comb)
>
> contr2 <- NULL
> for (x in 1:n) {
>       i <- comb[x, 1]
>       j <- comb[x, 2]
>       tmp <- list(TI[,i] - TI[,j]); names(tmp) <- paste0("TI",i, "_", j)
>       contr2 <- c(contr2, tmp)
> }
> contr2
>
> adonis(sp ~ contr2[1]+contr2[2]+contr2[3]+contr2[4]+contr2[5]+contr2[6],
> method = "euclidean", permutations = 9999)

contr2 is a "list" object. To make it work with adonis, you need to 
transform it into a data frame:

contr2df <- as.data.frame(contr2)

Then you can easily refer to all variables in contr2df at once using the 
"~ ." shorthand formula notation:

adonis(
     sp ~ ., data = contr2df,
     method = "euclidean",
     permutations = 9999
)




Problem: Since contr2 contains all possible pairwise combinations of 
your factor levels, the resulting "contrasts" are not orthogonal anymore:

crossprod(as.matrix(contr2))

With a factor of four levels, there can only be three independent 
contrasts, and adonis will only report results for the first three 
variables in contr2 (contr2df, respectively), ignoring the other three.




Best,
Sven



More information about the R-sig-ecology mailing list