Procrustes analysis comparing PCA spaces across multiple groups or datasets.
run_procrustes <- function(PCA_list) {
combNames <- combn(names(PCA_list), 2)
combNames <- apply(combNames, 2, function(x) paste0(x[1], "_", x[2]))
procrustes_table <- matrix(NA, ncol = 1, nrow = length(combNames),
dimnames = list(combNames, 'Birds'))
for (j in 1:length(PCA_list)) {
x <- PCA_list[[j]]$PCoA$vectors
for (i in 1:length(PCA_list)) {
if (i > j) {
y <- PCA_list[[i]]$PCoA$vectors
rownames(y) = rownames(x)
prcTest <- ade4::procuste.rtest(as.data.frame(x), as.data.frame(y), nrepet = 999)
cor_coef <- prcTest$obs
p_val <- prcTest$pvalue
signif <- ifelse(p_val < 0.001, "***", ifelse(p_val < 0.01, "**", ifelse(p_val < 0.05, "*", "ns")))
procrustes_table[paste0(names(PCA_list)[j], "_", names(PCA_list)[i]), 1] <-
paste0(round(cor_coef, 3), " ", signif)
}
}
}
return(procrustes_table)
}