简体   繁体   English

系数和相关的成对组合p.value?

[英]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. 我想按列计算矩阵的系数和相关p值的成对组合。

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矩阵:

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 . 但是,在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 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): 您可以使用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

The pairwise combinations of columns generated by the combn function is: combn函数生成的列的成对组合是:

     [,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. 你的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