您可以为此使用 correlation 包。
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)
| 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 值调整方法: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 (v2.0.0) 于 2021-05-30 创建