简体   繁体   中英

pairwise combinations of coefficient and correlation p.value?

I want to compute the pairwise combinations of coefficient and correlation pvalue for a matrix by columns.

Here I use two functions:

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

For example I have a matrix of A:

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

works fine.

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

However, shows Error in cor.test.default(Y, X) : 'x' and 'y' must have the same length . Can somebody please advise what have I done wrong here? I am using the same matrix so the length of columns should be identical.

You can use the combn function to generate all combinations of column comparisons and then apply across this matrix using cor.test on the combinations of columns of A (this assumes A is available in your global environment):

# 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

The pairwise combinations of columns generated by the combn function is:

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

Your apply(Y=A,2,allCorr.p,X=A) did not work as expected because (disregarding that you do not need to use Y=A ) you pass the whole matrix as the second argument to your function, so X actually has the length of all columns in your matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM