pair.cor

Function to perform pair-wise correlation tests on multiple variables, and adjust p-values for multiple comparisons according to a variety of methods (Benton et al., in press)

Arguments:

x: a matrix containing the variables of interest as the columns

test: correlation test to perform, either "pearson", "kendall", "spearman" using the cor.test function in {stats}

p.adj.method: the method in which to adjust p-values for multiple comparisons, using the p.adjust function in {stats}, and arguments therein

Description:

This function performs pair-wise correlation tests on all possible pairs of columns in the matrix x, and returns a list containing three contingency tables: $stats, the table of correlation statistics, either Pearson's correlation coefficient, Kendall's tau, or Spearman's rho depending on the test; $p.values, the raw p-values; and $p.adjust, the adjusted p-values. Upper and lower diagonals of each table are identical.

The Function:

pair.cor <- function(x, test=c("pearson", "kendall", "spearman"), p.adj.method=c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none")){

mat <- as.matrix(x)

n <- length(mat[1,])

N <- n*(n-1)/2

cor.stats <- matrix(nrow=length(mat[1,]),ncol=length(mat[1,]))

p.value <- matrix(nrow=length(mat[1,]),ncol=length(mat[1,]))

p.adj <- matrix(nrow=length(mat[1,]),ncol=length(mat[1,]))

for( i in 1:length(mat[1,]) ){

v1 <- mat[,i]

cor <- vector()

p <- vector()

padj <- vector()

for( j in 1:length(mat[1,]) ){

v2 <- mat[,j]

COR <- cor.test(v1,v2, method=test)

cor <- append(cor,COR$estimate)

p <- append(p,COR$p.value)

padj <- append(padj,p.adjust(p[j], method = p.adj.method, n=N ))

}

cor.stats[i,] <- cor

p.value[i,] <- p

p.adj[i,] <- padj

colnames(cor.stats)<-colnames(mat)

rownames(cor.stats)<-colnames(mat)

colnames(p.value)<-colnames(mat)

rownames(p.value)<-colnames(mat)

colnames(p.adj)<-colnames(mat)

rownames(p.adj)<-colnames(mat)

}

result <- list(stats=cor.stats, p.values=p.value, p.adjust=p.adj)

return(result)

}

Reference:

Michael J. Benton, Marcello Ruta, Alexander M. Dunhill, Manabu Sakamoto, The first half of tetrapod evolution, sampling proxies, and fossil record quality, Palaeogeography, Palaeoclimatology, Palaeoecology, Available online 26 September 2012, ISSN 0031-0182, 10.1016/j.palaeo.2012.09.005. (http://www.sciencedirect.com/science/article/pii/S0031018212005019)

##### Function by Manabu Sakamoto, 2012: manabu.sakamoto@gmail.com