statistics - How to return significant matches in R corrplot? -
i return significant matches following result figure fig. 1
library("corrplot") m <- cor(mtcars) # http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram cor.mtest <- function(mat, ...) { mat <- as.matrix(mat) n <- ncol(mat) p.mat<- matrix(na, n, n) diag(p.mat) <- 0 (i in 1:(n - 1)) { (j in (i + 1):n) { tmp <- cor.test(mat[, i], mat[, j], ...) p.mat[i, j] <- p.mat[j, i] <- tmp$p.value } } colnames(p.mat) <- rownames(p.mat) <- colnames(mat) p.mat } n <- length(mtcars) -1 p.mat <- cor.mtest(mtcars) head(p.mat[, 1:n]) ids <- seq(1,n) corrplot(m, type="upper", order="hclust", tl.pos=c("td"), method="circle", tl.cex = 0.5, tl.col = 'black', diag = false, p.mat = p.mat, sig.level = 0.0000005)
fig. 1 output
expected output
cyl: wt hp disp: wt hp cyl ...
r: 3.3.1
os: debian 8.5
the usual warnings regarding multiple testing apply here.
i write vectorized cor.test (there reason not available stats package, see above).
cor.test.all <- function(df) { #based on code stats:::cor.test.default #see license() license #two-sided test pearson correlation #without adjustment of p-values #no na treatment r <- cor(df) df <- nrow(df) - 2l t <- sqrt(df) * r/sqrt(1 - r^2) 2 * pmin(pt(t, df), pt(t, df, lower.tail = false)) } cor.p <- cor.test.all(mtcars) diag(cor.p) <- na res <- which(cor.p < 0.0000005, arr.ind = true) split(colnames(cor.p)[res[,2]], rownames(cor.p)[res[,1]]) #$am #[1] "gear" # #$cyl #[1] "mpg" "disp" "hp" "wt" "vs" # #$disp #[1] "mpg" "cyl" "hp" "wt" # #$gear #[1] "am" # #$hp #[1] "mpg" "cyl" "disp" # #$mpg #[1] "cyl" "disp" "hp" "wt" # #$vs #[1] "cyl" # #$wt #[1] "mpg" "cyl" "disp"
Comments
Post a Comment