【问题标题】:How to calculate p-value for Kendall Tau correlation coefficients in R?如何计算 R 中 Kendall Tau 相关系数的 p 值?
【发布时间】:2019-12-10 04:30:57
【问题描述】:

我使用以下方法计算了 Kendal 相关系数:

corr_test <- cor.test(values, use = "pairwise", method="kendall")
corr_test

但我需要 p 值。我找不到任何为 Kendall 相关性提供 p 值的包。

如何计算 Kendall tau 相关系数的 p 值?

此任务的目标是生成相关图,其中彩色单元格表示显着的相关系数。我使用 Kendall tau 是因为我的数据中有很多关联,而一个变量是一个因素。

【问题讨论】:

  • cor.test(iris$Sepal.Length, iris$Sepal.Width, method = "kendall") ?
  • 从你的例子:corr_test$p.value
  • 我得到:> corr_test$p.value NULL
  • @teunbrand cor.test 似乎也只取向量,我有一个矩阵

标签: r correlation p-value


【解决方案1】:

您可以简单地遍历数据的列(或行,如果您愿意的话)以在每个列组合上使用cor.test(),如下所示:

# Use some data
mat <- iris[,1:4]

# Index combinations of columns
# Not very efficient, but it'll do for now
idx <- expand.grid(colnames(mat), colnames(mat))

# Loop over indices, calculate p-value
pvals <- apply(idx, 1, function(i){
  x <- mat[,i[[1]]]
  y <- mat[,i[[2]]]
  cor.test(x, y, method = "kendall")$p.value
})
# Combine indices with pvalues, do some sort of multiple testing correction
# Note that we are testing column combinations twice 
# so we're overcorrecting with the FDR here
pvals <- cbind.data.frame(idx, pvals = p.adjust(pvals, "fdr"))

接下来,您必须用常规相关值补充这些值,并将这些值与 p 值结合起来。

# Calculate basic correlation
cors <- cor(mat, method = "kendall")
cors <- reshape2::melt(cors)

# Indices of correlations and pvalues should be the same, thus can be merged
if (identical(cors[,1:2], pvals[,1:2])) {
  df <- cbind.data.frame(pvals, cor = cors[,3])
}

并按以下方式绘制数据:

# Plot a matrix
ggplot(df, aes(Var1, Var2, fill = ifelse(pvals < 0.05, cor, 0))) +
  geom_raster() +
  scale_fill_gradient2(name = "Significant Correlation", limits = c(-1, 1))

另一种选择是使用idx &lt;- t(combn(colnames(mat), 2)),在这种情况下多次测试更正是合适的,但您必须弄清楚如何操纵这些值以再次匹配相关性。

【讨论】:

    猜你喜欢
    • 2011-02-03
    • 2015-05-12
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多