繁体   English   中英

如何计算 R 中 Kendall Tau 相关系数的 p 值?

[英]How to calculate p-value for Kendall Tau correlation coefficients in R?

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

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

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

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

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

在此处输入图片说明

您可以简单地遍历数据的列(或行,如果您愿意的话),以在每个列组合上使用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 <- t(combn(colnames(mat), 2)) ,在这种情况下,多次测试更正是合适的,但您必须弄清楚如何操纵这些值以再次与相关性匹配.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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