繁体   English   中英

在没有 rcorr() 库的情况下查找 R 中两个非方阵之间皮尔逊相关的 p 值

[英]Finding the p-value of the pearson correlation between two non square matrices in R without rcorr() library

我有两个非方阵,我需要找到彼此之间的皮尔逊相关值和相关的 p 值。 我知道rcorr()库,但是这个库的 RAM 内存使用量很大 - 我有两个巨大的矩阵,我无法使用rcorr()

使用cor()库,我能够获得相关值。 使用这个相关数组,可以制作一个函数来获取 p 值,而无需rcorr() ??

附: 我也试图寻找到Python来做到这一点,但我是唯一能够发现,不接受非方阵如scipy.stat pearsonrlinegress库。

 data1 <- matrix(runif(30),ncol=5)
 data2 <- matrix(runif(24),ncol=4)
 correlation <-cor(data1,data2,method='pearson')
 correlation
            [,1]       [,2]        [,3]         [,4]
[1,] -0.63451452  0.8311530 -0.18859842  0.004892728
[2,]  0.66676636 -0.8633116  0.02666929  0.010362925
[3,] -0.03299319 -0.4435478  0.06281622 -0.502668829
[4,] -0.79032734  0.7334099 -0.13531482  0.087650016
[5,] -0.02617180  0.5419900  0.67293404  0.112950907

大多数测试假设零假设输入来自独立的正态分布数据

然后他们测试t = r * sqrt(n-2) / sqrt(1-r^2)作为具有n-2自由度的t统计量

所以你可以尝试这样的事情(除了 base stats没有额外的包)

p <- function(t, d) { 1 - 2 * abs(pt(t, d) - 1/2) }
t <- function(r, n) { r * sqrt(n-2) / sqrt(1-r^2) }
rownum <- 6
set.seed(1)
data1 <- matrix(rnorm(5*rownum), nrow=rownum)
data2 <- matrix(rnorm(4*rownum), nrow=rownum)
correlation <- cor(data1, data2, method='pearson')
correlation
p(d(correlation, rownum), rownum-2)

给出相关性

            [,1]        [,2]       [,3]        [,4]
[1,] -0.52736212 -0.22015909  0.4017038 -0.09294361
[2,]  0.02056352  0.04304460  0.3434117  0.24733758
[3,]  0.40489453  0.73092841 -0.2950121 -0.83761011
[4,]  0.28672335 -0.07727180 -0.3430130 -0.02175433
[5,]  0.52745346  0.09179105 -0.7022999 -0.10932760

p

    [,1]       [,2]      [,3]       [,4]
[1,] 0.2822894 0.67509693 0.4298549 0.86098603
[2,] 0.9691591 0.93547298 0.5051319 0.63655918
[3,] 0.4258473 0.09885895 0.5703196 0.03741457
[4,] 0.5817008 0.88432299 0.5056595 0.96737366
[5,] 0.2821905 0.86270012 0.1197462 0.83666197

其中一个小于 0.05,这并不奇怪,因为您有 20 个值。

将这些矩阵右下角的数字与以下结果进行比较

> cor.test(data1[,5], data2[,4])

        Pearson's product-moment correlation

data:  data1[, 5] and data2[, 4]
t = -0.21997, df = 4, p-value = 0.8367
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.8458408  0.7706066
sample estimates:
       cor 
-0.1093276 

所以这种方法似乎对相关性和p值产生相同的答案

暂无
暂无

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

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