簡體   English   中英

相關計算與R中的閾值

[英]correlation computation with threshold in R

我想計算R中的相關性。但是我有很多缺失值。 因此,我想在相關矩陣中承認僅從至少10對值計算的相關性。 如何進行?

編輯:請注意,相關矩陣是從具有相同個體(行)的兩個大矩陣X和Y生成的。

首先我們生成一些示例數據:

R> x = matrix(rnorm(100), ncol=5)
##Fill in some NA's
R> x[3:15,1] = NA
R> x[2:10,3] = NA

接下來,我們遍歷x矩陣進行比較以檢測NA:

##Create a matrix with where the elements are the
##maximum number of possible comparisons 
m = matrix(nrow(x), ncol=ncol(x),nrow=ncol(x)) 
## This comparison can be made more efficient. 
## We only need to do column i with i+1:ncol(x)

## Each list element
for(i in 1:ncol(x)) {
    detect_na = is.na(x[,i]==x)
    c_sums = colSums(detect_na)
    m[i,] = m[i,] - c_sums
}

矩陣m現在包含每個列對的比較數。 現在轉換m矩陣以准備子集:

 m = ifelse(m>10, TRUE, NA)

接下來,我們根據m出所有列對和子集的相關性:

R> matrix(cor(x, use = "complete.obs")[ m], ncol=ncol(m), nrow=nrow(m))
     [,1]    [,2]     [,3]    [,4]    [,5]
[1,]   NA      NA       NA      NA      NA
[2,]   NA  1.0000 -0.14302 0.35902 -0.3466
[3,]   NA -0.1430  1.00000 0.03949  0.6172
[4,]   NA  0.3590  0.03949 1.00000  0.1606
[5,]   NA -0.3466  0.61720 0.16061  1.0000

暫無
暫無

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

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