[英]How to calculate p-value for Kendall Tau correlation coefficients in R?
您可以简单地遍历数据的列(或行,如果您愿意的话),以在每个列组合上使用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.