在 R 中进行显着性检验的相关矩阵

Correlation matrix with significance testing in R

我想知道 R 中是否已经有一个包可以通过 SPSS 或 Statistica 等显着性测试吐出相关矩阵?目前我必须生成一个相关矩阵(使用 ggplot2 geom_tile)并使用 cor.test 单独进行显着性测试.将两者组合成一个图形会很棒。

这是我使用的真实数据的一个子集。

           X         Var2   CorrValue        pValue
1         AAAA       V1   0.96240707       1.802995e-07
2         BBBB       V1  0.85385864       1.336821e-03
3         CCCC       V1  0.65835637       1.166884e-01
4         DDDD       V1 -0.54059196       3.673812e-01
5         EEEE       V1  0.29001017       1.000000e+00
6         FFFF       V1 -0.92886462       1.329147e-05
7         GGGG       V1  0.44273554       7.732118e-01
8         HHHH       V1 -0.80137821       7.597324e-03
9         IIII       V1  0.93013470       1.193781e-05
10        JJJJ       V1 -0.80781321       6.605418e-03
11        KKKK       V1 -0.42663765       7.948182e-01
12        LLLL       V1  0.80636700       6.771520e-03
13        MMMM       V1  0.85018600       1.543993e-03
14        NNNN       V1  0.90895906       6.210643e-05
15        OOOO       V1  0.88107227       3.669339e-04
16       PPPPP       V1  0.87853038       4.149637e-04
17       QQQQQ       V1  0.80868505       6.563540e-03
18       RRRRR       V1  0.79054501       1.014822e-02
19        AAAA       V2 -0.69444018       7.088595e-02
20        BBBB       V2 -0.75584928       2.402074e-02
21        CCCC       V2 -0.82625132       3.674920e-03
22        DDDD       V2  0.76938820       1.724964e-02
23        EEEE       V2 -0.07953878       1.000000e+00
24        FFFF       V2  0.64012814       1.335781e-01
25        GGGG       V2 -0.13886613       1.000000e+00
26        HHHH       V2  0.67113002       9.731157e-02
27        IIII       V2 -0.71736303       4.923524e-02
28        JJJJ       V2  0.79422494       9.297322e-03
29        KKKK       V2  0.65673252       1.166884e-01
30        LLLL       V2 -0.83719167       2.506800e-03

我用来生成 热图或相关矩阵 的代码如下:

    ggplot(longData, aes(X, Var2))+
  geom_tile(data=longData, aes(fill=CorrValue), color="white")+
  geom_text(aes(fill = longData$CorrValue, label = round(longData$CorrValue, 2)))+
  scale_fill_gradientn(colours=cm.colors(4),
                       limit=c(-1,1),name="Correlation\n(Pearson)")+
  theme(axis.text.x = element_text(size=12,  colour='black'),
        axis.text.y=element_text(colour='black'),
        panel.background=element_rect(colour="black", fill=NA))+
  coord_equal()

如有任何帮助,我们将不胜感激。

编辑:感谢蒂姆,我现在已经设法创建了一个包含相关系数和 p 值的数据框。但是,我现在想在热图上绘制显着性值并将其表示为星星或任何最容易做的事情。目前我只在两个级别之后,即 <.01 和 >.01.

psych输出与相应p值的相关性

> psych::corr.test(mtcars)
Call:psych::corr.test(x = mtcars)
Correlation matrix 
       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43
qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00  0.74 -0.23 -0.21 -0.66
vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74  1.00  0.17  0.21 -0.57
am    0.60 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17  1.00  0.79  0.06
gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  0.21  0.79  1.00  0.27
carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57  0.06  0.27  1.00
Sample Size 
[1] 32
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
      mpg cyl disp   hp drat   wt qsec   vs   am gear carb
mpg  0.00   0 0.00 0.00 0.00 0.00 0.22 0.00 0.01 0.10 0.02
cyl  0.00   0 0.00 0.00 0.00 0.00 0.01 0.00 0.04 0.08 0.04
disp 0.00   0 0.00 0.00 0.00 0.00 0.20 0.00 0.01 0.02 0.30
hp   0.00   0 0.00 0.00 0.17 0.00 0.00 0.00 1.00 1.00 0.00
drat 0.00   0 0.00 0.01 0.00 0.00 1.00 0.19 0.00 0.00 1.00
wt   0.00   0 0.00 0.00 0.00 0.00 1.00 0.02 0.00 0.01 0.20
qsec 0.02   0 0.01 0.00 0.62 0.34 0.00 0.00 1.00 1.00 0.00
vs   0.00   0 0.00 0.00 0.01 0.00 0.00 0.00 1.00 1.00 0.02
am   0.00   0 0.00 0.18 0.00 0.00 0.21 0.36 0.00 0.00 1.00
gear 0.01   0 0.00 0.49 0.00 0.00 0.24 0.26 0.00 0.00 1.00
carb 0.00   0 0.03 0.00 0.62 0.01 0.00 0.00 0.75 0.13 0.00

 To see confidence intervals of the correlations, print with the short=FALSE option

你可以用tidyr to transform the output to long format画出你想要的样子:

X <- psych::corr.test(mtcars)
tidyr::gather(as.data.frame(cbind(V1=rownames(X$p), X$p)), key, value, -V1)

我想我已经设法找到了解决方案。请随时查看代码,如有错误请指正。

#Create a column with the stars

    longformData$stars <- cut(longformData$pValue, breaks=c(-Inf, 0.001, 0.01, 0.05, Inf), 
                           label=c("***", "**", "*", ""))  # Create column of significance labels

最终剧情代码

    ggplot(longformData, aes(X, Var2))+
  geom_tile(data=longformData, aes(fill=CorrValue), color="white")+
  geom_text(aes(label=stars), color="black", size=5,vjust=-1.5)+
  geom_text(aes(fill = longformData$value, label = round(longformData$CorrValue, 2)))+
  scale_fill_gradient(low='red', high='green',
                      limit=c(-1,1),name="Correlation\n(Pearson)")+
  theme(axis.text.x = element_text(size=12,  colour='black'),
        axis.text.y=element_text(colour='black'),
        panel.background=element_rect(colour="black", fill=NA))+
  coord_equal()

附上输出图像。如果有更简单的方法来解决这个问题,我很乐意知道。

谢谢大家的帮助。