簡體   English   中英

我如何只在相關矩陣中包括大於0.6的相關系數?

[英]How would I only include correlation coefficients above .6 in a correlation matrix?

我試圖在R的相關矩陣中僅繪制相關系數大於.6的關系。當前,我正在使用包'corr_test'來運行Pearson Correlation,然后使用'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)

單擊此處 (Google雲端硬盤文件),獲取我一直在使用的相關系數的小型數據集。

謝謝!

最后,這很簡單,只需將絕對值低於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

在此處輸入圖片說明

在繪制之前,請基於絕對值大於或等於0.6的子集。

r <- subset(r, abs(r) >= .6)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM