簡體   English   中英

系數和相關的成對組合p.value?

[英]pairwise combinations of coefficient and correlation p.value?

我想按列計算矩陣的系數和相關p值的成對組合。

這里我使用兩個函數:

allCoef<- function(Y,X) {  lm(Y~X+0)$coef }
allCorr.p<- function(Y,X) {  cor.test(Y,X)$p.value }

例如,我有一個A矩陣:

A= matrix(sample(1:100,16),4,4)
apply(Y=A,2,allCoef,X=A)

工作正常。

apply(Y=A,2,allCorr.p,X=A)

但是,在cor.test.default(Y, X) : 'x' and 'y' must have the same length顯示Error cor.test.default(Y, X) : 'x' and 'y' must have the same length 有人可以告訴我這里做錯了什么嗎? 我使用相同的矩陣,因此列的長度應該相同。

您可以使用combn函數生成列比較的所有組合,然后使用cor.testA的列組合中應用此矩陣(假設A在您的全局環境中可用):

# All combinations of pairwise comparisons
cols <- t( combn(1:4,2) )
apply( cols , 1 , function(x) cor.test( A[,x[1] ] , A[ , x[2] ] )$p.value )
#[1] 0.9893876 0.9844555 0.5461623 0.7987615 0.7414658 0.1061751

combn函數生成的列的成對組合是:

     [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    1    4
[4,]    2    3
[5,]    2    4
[6,]    3    4

你的apply(Y=A,2,allCorr.p,X=A)沒有按預期工作,因為(無論你不需要使用Y=A )你將整個矩陣作為第二個參數傳遞給你的函數,所以X實際上具有矩陣中所有列的長度。

暫無
暫無

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

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