gpt4 book ai didi

r - R 中的 Spearman 相关环

转载 作者:行者123 更新时间:2023-12-04 11:08:28 25 4
gpt4 key购买 nike

之前的一篇文章解释了如何在 R 中对所有数据对执行卡方循环:Chi Square Analysis using for loop in R .我想使用此代码对 Spearman 相关性执行相同的操作。

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

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 相关性。

我以为我可以通过添加 method="spearman"命令来获取这些数据,但这似乎不起作用。如果我使用代码:

library(plyr)

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

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

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

})

我收到回复:

Error in data.frame(Row = colnames(fullngodata)[x[1]], Column =    
colnames(fullngodata[x[2]]), :
arguments imply differing number of rows: 1, 0
In addition: Warning message:
In cor.test.default(fullngodata[, x[1]], fullngodata[, x[2]], method = "spearman") :
Cannot compute exact p-values with ties

我做错了什么?

最佳答案

试试 ltm 包中的 rcor.test 函数。

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

关于r - R 中的 Spearman 相关环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7767184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com