2011-02-09 196 views
4

我在R中有一個大的相關矩陣結果 - 現在大約有30個項目相互關聯 - 所以這個數組有大約10,000個單元格。我想找到最大的5個和最小的5個結果。我怎樣才能做到這一點?查找最大的5值小於1,最小的5個值

這裏是一個非常小的部分 - 左上角 - 看起來像:

   PL1   V3   V4   V5 
PL1  1.00000000 0.19905701 -0.02994034 -0.1533846 
V3  0.19905701 1.00000000 0.09036472 0.1306054 
V4  -0.02994034 0.09036472 1.00000000 0.1848030 
V5  -0.15338465 0.13060539 0.18480296 1.0000000 

上表中的值始終爲1 & -1之間,如果有幫助,是相關矩陣的上半在對角線上方是對角線下方的下半部分的副本。

我需要最積極的5少於1和最消極的5包括-1,如果它存在。

在此先感謝。

回答

2

你想找到最大和最小的相關性,可能不僅知道什麼,而且那些值來自哪裏。這很容易。

x<-matrix(runif(25),5,5) 
cor<-cor(x) 
l <- length(cor) 
l1 <- length(cor[cor<1]) 

#the actual high and low correlation indexes 
corHigh <- order(cor)[(l1-4):l1] 
corLow <- order(cor)[1:5] 
#(if you just want to view the correlations cor[corLow] or cor[corHigh] works fine) 

#isolate them in the matrix so you can see where they came from easily 
corHighView <- cor 
corHighView[!1:l %in% corHigh] <- NA 
corLowView <- cor 
corLowView[!1:l %in% corLow] <- NA 

#look at your matrix with your target correlations sticking out like a sore thumb 
corLowView 
corHighView 
1

那種骯髒的:

x<-matrix(runif(25),5,5) 
cor<-cor(x) 
max1<-max(cor) 
max2<-max(cor[cor!=max1]) 
max3<-max(cor[cor!=max1 & cor!=max2]) 
max4<-max(cor[cor!=max1& cor!=max2& cor!=max3]) 
max5<-max(cor[cor!=max1& cor!=max2& cor!=max3& cor!=max4]) 
max6<-max(cor[cor!=max1& cor!=max2& cor!=max3& cor!=max4& cor!=max5]) 
maxes<-c(max2,max3,max4,max5,max6) 
maxes 
matrix(cor %in% maxes,5,5) 
+0

可以把我想的遞歸看起來,但我同意。這有點粗糙。儘管如此,最終的矩陣TRUE/FALSE輸出非常清晰。 – LGTrader 2011-02-10 00:12:45

0

怎麼樣一個漂亮的奶油情節? :)

> m <- matrix(runif(100)*2-1, ncol=10) 
> colnames(m) <- rownames(m) <- paste("V", 1:10, sep="") 
> m 
      V1   V2   V3   V4   V5   V6   V7   V8   V9   V10 
V1 -0.40101571 -0.27049070 0.2414295 -0.1889384 0.6459941 -0.8851884 0.332284597 -0.431312791 0.3828374 0.46398193 
V2 0.38557771 0.37083911 -0.3004923 0.1253908 -0.4405188 -0.5424613 0.869493425 0.023291914 0.9625392 -0.83196773 
V3 0.61923503 -0.27615909 0.1759168 -0.7333568 -0.4256801 -0.6170807 0.438613391 -0.003632086 0.4113488 -0.40590330 
V4 0.72093123 0.68479573 0.5032486 0.3720876 -0.6775834 0.2445693 0.353658359 -0.839104640 -0.8122970 -0.42322187 
V5 -0.08640529 0.04432795 -0.5120129 -0.9327905 -0.5821378 0.4671473 -0.367677007 0.483375219 -0.7849003 0.57686729 
V6 -0.72451704 0.75814550 0.7838393 -0.7650238 0.6742669 0.2260757 0.001645839 0.570753074 0.1944579 0.07917656 
V7 0.64516271 0.51994540 0.9057388 -0.3976167 -0.7403159 -0.2873382 -0.809354444 0.319095368 -0.9766422 -0.71981321 
V8 -0.51509049 0.18727837 -0.1971454 -0.4290346 0.5657622 0.5324266 0.451608266 -0.715594335 -0.2749510 0.38234855 
V9 0.49035803 0.50252397 0.7736783 0.3342899 -0.2732427 0.1128947 0.870315070 -0.291482237 0.5171181 -0.59784449 
V10 -0.51811224 -0.67159723 0.8903813 -0.7562222 -0.9790557 -0.5830560 -0.715136643 0.167987391 -0.0529399 0.44570592 

> library(ggplot2) 
> p <- ggplot(data=melt(m), aes(x=X1, y=X2, color=value)) 
> p + geom_point(size=5, alpha=0.7) + scale_color_gradient2() 

the plot

我不認爲這將是很難看100x100的情節和發現極端值有眼。 :)

+0

情節是相當不錯的。我同意挑選顏色較強的人很困難。我懷疑我必須採用實際陣列中全部爲1的對角線,而不是在你的陣列中,並將它們設置爲0或其他值。我注意到排序與矩陣本身不一樣 - 它的矩陣順序爲V10。有沒有一種選擇可以讓他們保持秩序? – LGTrader 2011-02-10 00:09:19

+0

順便說一句 - 融化是一個不錯的選擇。也許熔解結果可能會被分類以返回最高和最低5? – LGTrader 2011-02-10 00:11:22

5

這裏是要做到這一點(毫無疑問,有一個更簡單的方法),另一種粗暴的方式,但它不是太難的功能來包裝這個:

編輯:縮短了代碼。

# Simulate correlation matrix (taken from Patrick's answer) 
set.seed(1) 
n<-100 
x<-matrix(runif(n^2),n,n) 
cor<-cor(x) 

# Set diagonal and one triangle to to 0: 
diag(cor) <- 0 
cor[upper.tri(cor)] <- 0 

# Get sorted values: 
sort <- sort(cor) 

# Create a dummy matrix and get lowest 5: 
min <- matrix(cor %in% sort[1:5] ,n,n) 
which(min,arr.ind=T) 

# Same for highest 5: 
max <- matrix(cor %in% sort[(n^2-5):(n^2)] ,n,n) 
which(max,arr.ind=T) 

另一個選擇,正如ulidtko說的,是做一個圖。你可以試試我的包,叫qgraph,可用於可視化的相關性矩陣作爲網絡:

library(qgraph) 
qgraph(cor(x),vsize=2,minimum=0.2,filetype="png") 

qgraph output in PNG format

2

有趣的網絡圖薩沙。這裏是真實的數據。似乎我有更強烈的積極而不是消極的相關性。

enter image description here

+0

這在實際數據中很常見,因爲數據通常是關於一般構造的。您可以使用參數`filetype =「png」`或PDF格式的`filetype =「pdf」` – 2011-02-10 00:51:15

0

我採取沒有信用這一點,剛剛發佈的R-幫助名單上的情況下,the link死亡的代碼...感謝季米特里斯。它返回一個包含每個變量的排序的頂部相關的列表p

cor.mat <- cor(matrix(rnorm(100*1000), 1000, 100)) 
p <- 30 # how many top items 
n <- ncol(cor.mat) 
cmat <- col(cor.mat) 
ind <- order(-cmat, cor.mat, decreasing = TRUE) - (n * cmat - n) 
dim(ind) <- dim(cor.mat) 
ind <- ind[seq(2, p + 1), ] 
out <- cbind(ID = c(col(ind)), ID2 = c(ind)) 
as.data.frame(cbind(out,cor = cor.mat[out])) 
相關問題