【发布时间】:2019-04-22 02:38:04
【问题描述】:
尊敬的 * 社区:
我正在尝试制作一个 p 值矩阵,该矩阵对应于我通过获得相关值获得的矩阵
我的数据如下(为简单起见只做 5 行,我的真实数据是 3 列,每个数据框有 50 行)。
FG_Smooth <- data.frame(FS_1 = c(0.43, 0.33, 3.47, 5.26, 1.09), FS2 = c(0.01, 0.02, 6.86, 3.27, 0.86), FS_3 = c(0.07, 0.36, 1.91, 5.61, 0.84), row.names = c("Group_3", "Thermo", "Embryophyta", "Flavo", "Cyclo"))
FMG_Smooth <- data.frame(GS_1 = c(1.13, 1.20, 0.52, 2.81, 0.70), GS_2 = c(1.18, 1.7, 0.42, 2.93, 0.78), GS_3 = c(1.17, 1.11, 0.60, 3.10, 0.87), row.names = c("Proline", "Trigonelline", "L-Lysine", "Nioctine", "Caffeate"))
library(Hmisc)
rcorr(t(FG_Smooth), t(FMG_Smooth), type = "pearson")
但我得到这个错误:
rcorr(t(FG_Smooth), t(FMG_Smooth), type = "pearson") 中的错误: 必须有 >4 个观察结果
我每个人只有 3 个生物样本 - 所以我无法使用在多个帖子中多次建议的 rcorr 命令。 rcorr 命令为您提供 1) 相关矩阵; 2) 相关性的 p 值。
所以,为了回避这个问题,我运行了以下内容:正如其他帖子中所建议的那样:
library(stats)
cor(t(FG_Smooth), t(FMG_Smooth), method = "pearson")
这可行,并给出了我所有相关性的矩阵。
我的下一步是找到与相关矩阵中的每个值相关联的 p 值。函数cor.test 只给出了一个整体 p 值,这不是我需要的。
在阅读了多篇帖子后 - 我遇到了这个: rcorr() function for correlations
我按照给定代码的说明进行操作:
tblcols <- expand.grid(1:ncol(FG_Smooth), 1:ncol(FMG_Smooth))
cfunc <- function(var1, var2) {
cor.test(FG_Smooth[,var1], FMG_Smooth[,var2], method="pearson")
}
res <- mapply(function(a,b) {
cfunc(var1 = a, var2 = b)
}, tblcols$Var1, tblcols$Var2)
head(res)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
statistic 1.324125 -0.1022017 2.422883 0.9131595 -0.3509424 1.734178 1.53494
parameter 3 3 3 3 3 3 3
p.value 0.2773076 0.9250449 0.09392613 0.4284906 0.74883 0.1812997 0.2223626
estimate 0.6073388 -0.05890371 0.8135079 0.4663678 -0.1985814 0.7075406 0.663238
null.value 0 0 0 0 0 0 0
alternative "two.sided" "two.sided" "two.sided" "two.sided" "two.sided" "two.sided" "two.sided"
[,8] [,9]
statistic -0.009291327 2.880821
parameter 3 3
p.value 0.99317 0.06348644
estimate -0.005364273 0.8570256
null.value 0 0
alternative "two.sided" "two.sided"
这只会给我 9 个 p 值,而不是对应于使用 cor 命令获得的每个相关值的 p 值矩阵。对于此示例,它将是一个 5x5 的 p 值矩阵,因为 cor 命令会生成一个 5x5 的相关值矩阵。
有区别吗?怎么做?
【问题讨论】:
标签: r correlation p-value