【问题标题】:Correlation matrix with significance testing in RR中具有显着性检验的相关矩阵
【发布时间】:2015-11-20 02:45:15
【问题描述】:

我想知道 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()

任何帮助将不胜感激。

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

【问题讨论】:

  • 您作为示例提供的图与显着性检验无关 - 它与您的问题有何关系?
  • 抱歉,我的问题不清楚。该代码用于生成相关矩阵或热图。我已经编辑了这个问题以明确这一点。我希望能够使用开始(***)或粗体/斜体文本在其中显示显着性测试。我不知道该怎么做。目前我不得不对每对变量使用 cor.test 来测试没有帮助的重要性。同样使用 cor.test 我无法在热图中显示发生显着相关性的位置。
  • corrplot 包中的函数corrplot 可能会有所帮助。
  • @VGu 相关值的热图与您问题中的显着性检验有何关系?这是两件不同的事情..
  • 我正在尝试在相关热图上绘制显着性值。例如,相关值可能为 0.96,但 p 值 >0.01。因此,我想在热图上通过加粗相关值或在具有相关相关值的框中使用一系列星号(*、**、***)来表示这一点。希望现在有意义。再次抱歉造成混乱。

标签: r ggplot2 correlation p-value


【解决方案1】:

我想我已经设法找到了解决方案。如果我错了,请随时检查代码并纠正我。

#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()

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

感谢大家的帮助。

【讨论】:

    【解决方案2】:

    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 将输出转换为long format 以根据需要进行绘制:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2022-01-22
      • 2019-12-07
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      相关资源
      最近更新 更多