如何在R corrplot中返回有效的匹配? [英] How to return significant matches in R corrplot?
本文介绍了如何在R corrplot中返回有效的匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从图1所示的以下结果中返回重要匹配项
I would like to return the significant matches from the following result shown in 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
for (i in 1:(n - 1)) {
for (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)
图. 1个输出
预期产量
cyl: wt hp
disp: wt hp cyl
...
R:3.3.1
操作系统:Debian 8.5
R: 3.3.1
OS: Debian 8.5
推荐答案
此处涉及多次测试的常见警告.
The usual warnings regarding multiple testing apply here.
我会写一个向量化的cor.test(有一个原因不能从stats包中获得,请参见上文).
I would write a vectorized cor.test (there is a reason that is not available from the stats package, see above).
cor.test.all <- function(DF) {
#based on code from stats:::cor.test.default
#see license() for the license
#two-sided test for 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"
这篇关于如何在R corrplot中返回有效的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文