繁体   English   中英

如何在corrplot()中以数字方式显示置信区间?

[英]How can confidence intervals be numerically visualized in corrplot()?

corrplot() ,是否可以在系数下以数字方式显示置信区间?

library(corrplot)
M <- cor(mtcars)


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(mtcars,0.95)
res2 <- cor.mtest(mtcars,0.99)

我想在下面的图中添加low=res1[[2]]upp=res1[[3]]置信区间作为相关系数以下的数字。

corrplot(M, method="number")

corrplot是一个漂亮的文本text()表。 所以我们可以尝试在其上添加其他文字。

继续你的例子:

corrplot(cor(mtcars), method="number")

我们形成置信区间标签:

conf <- paste0("[", format(res1[[2]], digits=1), ":", format(res1[[3]], digits=1), "]")

并将它们作为文本添加到现有的corrplot

xs <- row(res1[[1]])
ys <- (ncol(res1[[1]])+1) - col(res1[[1]])
text(xs, ys, conf, pos=1, cex=0.5)

注意:似乎y = 1从顶部开始,所以我们需要反转它(这就是为什么ys表达式比xs更复杂。

结果如下:

具有置信水平的corrplot

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM