Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Combine cor.mtest and p.adjust

Tags:

r

Is there a way to account for p-value multiple comparisons (such as p.adjust) with this cor.mtest function? Code from http://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html

Thank you!

cors<-cor(rel_tnum_data)
cor.mtest <- function(mat, conf.level = 0.95) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)
diag(p.mat) <- 0
diag(lowCI.mat) <- diag(uppCI.mat) <- 1
for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
        tmp <- cor.test(mat[, i], mat[, j], conf.level = conf.level)
        p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        lowCI.mat[i, j] <- lowCI.mat[j, i] <- tmp$conf.int[1]
        uppCI.mat[i, j] <- uppCI.mat[j, i] <- tmp$conf.int[2]
    }
}
return(list(p.mat, lowCI.mat, uppCI.mat))
}
res1 <- cor.mtest(rel_tnum_data, 0.95)
res2 <- cor.mtest(rel_tnum_data, 0.99)
corrplot(cors, p.mat = res1[[1]], sig.level=0.05, insig="blank", cl.align="r", tl.cex=0.6, order="hclust", type="lower", tl.srt=60, cl.ratio=0.1)
like image 794
eliztr Avatar asked Jun 05 '15 00:06

eliztr


1 Answers

You can vectorize your p-value matrix (e.g. res1), which has been returned by cor.mtest function. Afterwards, p.adjust function will help you to get the adjusted p-values.

pAdj <- p.adjust(c(res1[[1]]), method = "BH")

Then create the original matrix with adjusted p-values.

resAdj <- matrix(pAdj, ncol = dim(res1[[1]])[1])

Then corrplot should be ready to create your plot with adjusted p-values.

corrplot(cors, p.mat = resAdj, sig.level=0.05, insig="blank", cl.align="r", tl.cex=0.6, order="hclust", type="lower", tl.srt=60, cl.ratio=0.1)

Note that p.mat = resAdj.

like image 80
Ali Altıntaş Avatar answered Oct 19 '22 21:10

Ali Altıntaş