【问题标题】:How would I only include correlation coefficients above .6 in a correlation matrix?我如何只在相关矩阵中包含高于 0.6 的相关系数?
【发布时间】:2018-07-09 15:39:47
【问题描述】:

我试图在 R 中的相关矩阵中仅绘制相关系数高于 0.6 的关系。目前,我正在使用包“corr_test”来运行 Pearson 相关,然后我使用“ggcorrplot”来制作我的矩阵.如何修改我的代码以仅绘制强于 +.6/-.6 的相关系数?

以下是我当前的代码:

##New Pearson Correlation Script##
#Load in the stuff you dont have
install.packages("psych")
install.packages("ggcorrplot")
#Load it up
library("psych")
library("ggcorrplot")
library("ggplot2")
##Load in your datasets, just fill D1 if one matrix, load D2 as well if 2 matrices
D1 =class
D2 =metab
#Run the correlation
test =corr.test(D1, y = D2, use = "complete")
#r is the correlation matrix 
r = test$r
#p is the p-values
p = test$p

#plot it out 
testplot= ggcorrplot(r, method = "circle" ,lab_size= 0.3, tl.cex = 8, outline.col = "black", tl.col = "black")+
  theme(axis.text.y=element_text(size=9),
        axis.text.x = element_text(size=9, angle=90))
print(testplot)

单击Here(Google Drive 文件)查看我一直在使用的相关系数的小数据集。

谢谢!

【问题讨论】:

  • 你的意思是相关系数绝对值大于0.6?
  • data(class) 给出Warning message: In data(class) : data set ‘class’ not founddata(metab) 相同
  • 我想给出我的整个代码,但提供的数据表已经有相关系数。我自己运行了相关性并提取了要绘制的系数
  • 是的,当然是绝对值

标签: r correlation


【解决方案1】:

最后很简单,只需将所有低于0.6的绝对值设置为NA即可。

r2 <- r    # Save a copy in case you need it later
is.na(r) <- abs(r) < 0.6

#plot it out 
testplot= ggcorrplot(r, method = "circle" ,lab_size= 0.3, tl.cex = 8, outline.col = "black", tl.col = "black")+
  theme(axis.text.y=element_text(size=9),
        axis.text.x = element_text(size=9, angle=90))
print(testplot)

r <- r2    # reset
rm(r2)     # tidy up

【讨论】:

    【解决方案2】:

    在绘制之前,基于绝对值大于或等于 0.6 的子集。

    r &lt;- subset(r, abs(r) &gt;= .6)

    【讨论】:

    • 当我尝试添加这个时,我得到这个错误: x[subset & !is.na(subset), vars, drop = drop] 中的错误: (下标) 逻辑下标太长
    • @Haley Odd。你介意分享r 的输出吗?由于我们没有您正在使用的数据集,因此很难重现此错误。或者,就使用@Rui 的建议,也一样好。
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2013-07-20
    • 2023-02-21
    相关资源
    最近更新 更多