R中的Spearman相关环

R中的Spearman相关环,r,R,之前的一篇文章解释了如何在所有数据对上用R进行卡方循环:。 我想用这段代码为Spearman关联做同样的事情 我已经尝试过改变一些变量,并且我能够使用以下代码计算皮尔逊相关变量: library(plyr) combos <- combn(ncol(fullngodata),2) adply(combos, 2, function(x) { test <- cor.test(fullngodata[, x[1]], fullngodata[, x[2]]) out &l

之前的一篇文章解释了如何在所有数据对上用R进行卡方循环:。 我想用这段代码为Spearman关联做同样的事情

我已经尝试过改变一些变量,并且我能够使用以下代码计算皮尔逊相关变量:

library(plyr)

combos <- combn(ncol(fullngodata),2)

adply(combos, 2, function(x) {
  test <- cor.test(fullngodata[, x[1]], fullngodata[, x[2]])

  out <- data.frame("Row" = colnames(fullngodata)[x[1]]
                , "Column" = colnames(fullngodata[x[2]])
                , "cor" = round(test$statistic,3)
                ,  "df"= test$parameter
                ,  "p.value" = round(test$p.value, 3)
                )
 return(out)

})  

我做错了什么?

问题是当您执行spearman测试时,
cor.test
为参数返回一个值
NULL
。从
?cor.test
:参数:在遵循t分布的情况下,测试统计的自由度

您可以在以下示例中看到这一点:

x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)

str(cor.test(x, y, method = "spearman"))
List of 8
 $ statistic  : Named num 48
  ..- attr(*, "names")= chr "S"
 $ parameter  : NULL    
 $ p.value    : num 0.0968
 $ estimate   : Named num 0.6
  ..- attr(*, "names")= chr "rho"
 $ null.value : Named num 0
  ..- attr(*, "names")= chr "rho"
 $ alternative: chr "two.sided"
 $ method     : chr "Spearman's rank correlation rho"
 $ data.name  : chr "x and y"
 - attr(*, "class")= chr "htest"

尝试
rcor。在
ltm
软件包中测试
功能

mat <- matrix(rnorm(1000), 100, 10, dimnames = list(NULL, LETTERS[1:10]))
rcor.test(mat, method = "spearman")

  A      B      C      D      E      F      G      H      I      J     
A  ***** -0.035  0.072  0.238 -0.097  0.007 -0.010 -0.031  0.039 -0.090
B  0.726  ***** -0.042 -0.166  0.005  0.025  0.007 -0.231  0.005  0.006
C  0.473  0.679  *****  0.046  0.074 -0.020  0.091 -0.183 -0.040 -0.084
D  0.017  0.098  0.647  ***** -0.060 -0.151 -0.175 -0.068  0.039  0.181
E  0.338  0.960  0.466  0.553  *****  0.254  0.055 -0.031  0.072 -0.059
F  0.948  0.805  0.843  0.133  0.011  ***** -0.014 -0.121  0.153  0.048
G  0.923  0.941  0.370  0.081  0.588  0.892  ***** -0.060 -0.050  0.011
H  0.759  0.021  0.069  0.501  0.756  0.230  0.555  ***** -0.053 -0.193
I  0.700  0.963  0.690  0.701  0.476  0.130  0.621  0.597  ***** -0.034
J  0.373  0.955  0.406  0.072  0.561  0.633  0.910  0.055  0.736  *****

upper diagonal part contains correlation coefficient estimates 
lower diagonal part contains corresponding p-values
mat
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)

str(cor.test(x, y, method = "spearman"))
List of 8
 $ statistic  : Named num 48
  ..- attr(*, "names")= chr "S"
 $ parameter  : NULL    
 $ p.value    : num 0.0968
 $ estimate   : Named num 0.6
  ..- attr(*, "names")= chr "rho"
 $ null.value : Named num 0
  ..- attr(*, "names")= chr "rho"
 $ alternative: chr "two.sided"
 $ method     : chr "Spearman's rank correlation rho"
 $ data.name  : chr "x and y"
 - attr(*, "class")= chr "htest"
,  "df"= test$parameter
mat <- matrix(rnorm(1000), 100, 10, dimnames = list(NULL, LETTERS[1:10]))
rcor.test(mat, method = "spearman")

  A      B      C      D      E      F      G      H      I      J     
A  ***** -0.035  0.072  0.238 -0.097  0.007 -0.010 -0.031  0.039 -0.090
B  0.726  ***** -0.042 -0.166  0.005  0.025  0.007 -0.231  0.005  0.006
C  0.473  0.679  *****  0.046  0.074 -0.020  0.091 -0.183 -0.040 -0.084
D  0.017  0.098  0.647  ***** -0.060 -0.151 -0.175 -0.068  0.039  0.181
E  0.338  0.960  0.466  0.553  *****  0.254  0.055 -0.031  0.072 -0.059
F  0.948  0.805  0.843  0.133  0.011  ***** -0.014 -0.121  0.153  0.048
G  0.923  0.941  0.370  0.081  0.588  0.892  ***** -0.060 -0.050  0.011
H  0.759  0.021  0.069  0.501  0.756  0.230  0.555  ***** -0.053 -0.193
I  0.700  0.963  0.690  0.701  0.476  0.130  0.621  0.597  ***** -0.034
J  0.373  0.955  0.406  0.072  0.561  0.633  0.910  0.055  0.736  *****

upper diagonal part contains correlation coefficient estimates 
lower diagonal part contains corresponding p-values