r - 打印同表中的关系和相关性的显着性

标签 r tidyverse tidyr data-manipulation tidy

我正在尝试在一张表中打印出关系的重要性。非常感谢。

 library("Hmisc")
    my_data <- mtcars[, c(1,3,4,5,6,7)]
    my.cor <- cor(my_data); my.cor; my.cor
    my.cor.p <- rcorr(as.matrix(my_data)); my.cor.p
    

预期答案:其他列应该类似于 disp 列,其中包括 my.cor 值和 my.cor.p

mpg  disp     hp  drat    wt  qsec
mpg   1.00    -0.85 -0.78  0.68 -0.87  0.42
disp -0.85***  1.00  0.79 -0.71  0.89 -0.43
hp   -0.78***  0.79  1.00 -0.45  0.66 -0.71
drat  0.68*** -0.71 -0.45  1.00 -0.71  0.09
wt   -0.87***  0.89  0.66 -0.71  1.00 -0.17
qsec  0.42*  -0.43 -0.71  0.09 -0.17  1.00

*** p <0.01, ** p<0.05, * p<0.1

最佳答案

您可以使用相关性包来实现此目的。

correlation()函数计算指定变量之间的相关性,并返回一个数据框,其中包含每对变量的相关性、置信区间和测试值:

library(correlation)
my_data <- mtcars[, c(1,3,4,5,6,7)]

(corr <- correlation(my_data))
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1 | Parameter2 |     r |         95% CI | t(30) |         p
#> --------------------------------------------------------------------
#> mpg        |       disp | -0.85 | [-0.92, -0.71] | -8.75 | < .001***
#> mpg        |         hp | -0.78 | [-0.89, -0.59] | -6.74 | < .001***
#> mpg        |       drat |  0.68 | [ 0.44,  0.83] |  5.10 | < .001***
#> mpg        |         wt | -0.87 | [-0.93, -0.74] | -9.56 | < .001***
#> mpg        |       qsec |  0.42 | [ 0.08,  0.67] |  2.53 | 0.053    
#> disp       |         hp |  0.79 | [ 0.61,  0.89] |  7.08 | < .001***
#> disp       |       drat | -0.71 | [-0.85, -0.48] | -5.53 | < .001***
#> disp       |         wt |  0.89 | [ 0.78,  0.94] | 10.58 | < .001***
#> disp       |       qsec | -0.43 | [-0.68, -0.10] | -2.64 | 0.053    
#> hp         |       drat | -0.45 | [-0.69, -0.12] | -2.75 | < .05*   
#> hp         |         wt |  0.66 | [ 0.40,  0.82] |  4.80 | < .001***
#> hp         |       qsec | -0.71 | [-0.85, -0.48] | -5.49 | < .001***
#> drat       |         wt | -0.71 | [-0.85, -0.48] | -5.56 | < .001***
#> drat       |       qsec |  0.09 | [-0.27,  0.43] |  0.50 | 0.678    
#> wt         |       qsec | -0.17 | [-0.49,  0.19] | -0.97 | 0.678    
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 32

如果将此关联对象传递给summary() ,它将返回一个仅包含相关性和 p 值星的紧凑矩阵。

(corrmat <- summary(corr))
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter |     qsec |       wt |     drat |       hp |     disp
#> ----------------------------------------------------------------
#> mpg       |     0.42 | -0.87*** |  0.68*** | -0.78*** | -0.85***
#> disp      |    -0.43 |  0.89*** | -0.71*** |  0.79*** |         
#> hp        | -0.71*** |  0.66*** |   -0.45* |          |         
#> drat      |     0.09 | -0.71*** |          |          |         
#> wt        |    -0.17 |          |          |          |         
#> 
#> p-value adjustment method: Holm (1979)

如果您在 Markdown 中编写此脚本,您可以获得一个带有 display() 的 markdown 格式的表格。功能。

# For rendering in markdown:
display(corrmat)
<表类=“s-表”> <标题> 参数 qsec wt 德拉特 hp 显示 <正文> mpg 0.42 -0.87*** 0.68*** -0.78*** -0.85*** 显示 -0.43 0.89*** -0.71*** 0.79*** hp -0.71*** 0.66*** -0.45* 德拉特 0.09 -0.71*** wt -0.17

相关矩阵( PIL 逊法)

p值调整方法:Holm (1979)

就我个人而言,我建议优先考虑置信区间而不是 p 值或显着性星号,因为它们提供的信息要丰富得多。您可以使用 apaTables 包获取包含相关性和置信区间的表格。

apaTables::apa.cor.table(my_data)
#> 
#> 
#> Means, standard deviations, and correlations with confidence intervals
#>  
#> 
#>   Variable M      SD     1            2            3            4            5
#>   1. mpg   20.09  6.03                                                      
#>                                                                             
#>   2. disp  230.72 123.94 -.85**                                             
#>                          [-.92, -.71]                                       
#>                                                                             
#>   3. hp    146.69 68.56  -.78**       .79**                                 
#>                          [-.89, -.59] [.61, .89]                            
#>                                                                             
#>   4. drat  3.60   0.53   .68**        -.71**       -.45**                   
#>                          [.44, .83]   [-.85, -.48] [-.69, -.12]             
#>                                                                             
#>   5. wt    3.22   0.98   -.87**       .89**        .66**        -.71**      
#>                          [-.93, -.74] [.78, .94]   [.40, .82]   [-.85, -.48]
#>                                                                             
#>   6. qsec  17.85  1.79   .42*         -.43*        -.71**       .09          -.17
#>                          [.08, .67]   [-.68, -.10] [-.85, -.48] [-.27, .43]  [-.49, .19]

#> Note. M and SD are used to represent mean and standard deviation, respectively.
#> Values in square brackets indicate the 95% confidence interval.
#> The confidence interval is a plausible range of population correlations 
#> that could have caused the sample correlation (Cumming, 2014).
#>  * indicates p < .05. ** indicates p < .01.
#> 

reprex package 于 2021 年 5 月 30 日创建(v2.0.0)

关于r - 打印同表中的关系和相关性的显着性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67760881/

相关文章:

r - 将输出保存到 Markdown 中的文本文件

r - 将数据帧保存到R中的光盘以进行存储的最佳格式是什么?

r - data.table 相当于 tidyr::complete with group_by with on and by 语法

r - "Undoing"使用 stringr::str_split 创建的嵌套 tibble

r - 在多列上使用 tidyr 的 pivot_wider 的问题

r - R 中的 CFA : The variance-covariance matrix of the estimated parameters (vcov) does not appear to be positive definite

r - 编织为 PDF 时限制 Rstudio (3.1.1) 中函数的输出

r - 数据框中的列无法使用 R 中的 read_xls 函数正确读取

r - 从 R 中的变量列表中创建一个变量?

r - 如何使用 purrr 嵌套数据对特定行进行计算