1 引言

相关关系(Correlation)研究是在健康领域非常常用的一种研究。

1.1 相关:

事物或信号之间的共变关系或因果关系。统计上一般指两变量间存在线性关系。

1.2 统计方法选择

1.2.1 连续变量资料

  • 皮尔逊积矩相关系数(Pearson product-moment correlation coefficient)适用于符合以下条件的资料:
    • 两变量的度量水平都是连续数值型
    • 且两变量的总体是正态分布或者近似正态分布的情况,或者
    • 样本量应大于30
  • Spearman秩相关(Spearman Rank Correlation):
    • 资料分布不明
    • 变量非正态分布

1.2.2 分类变量资料

  • 有序分类变量的相关分析

    • 有序分类变量的相关性又称为一致性,即行变量等级高的列变量等级也高,如果行变量等级高而列变量等级低,则称为不一致
    • 常用的统计量有:Gamma、Kendall的tau-b、Kendall的tau-c等。
  • 无序分类变量的相关分析

    • 最常用的为卡方检验,用于评价两个无序分类变量的相关性。根据卡方值衍生出来的指标还有列联系数、Phi、Cramer的V、Lambda系数、不确定系数等。

2 统计分析

2.1 假设检验

2.1.1 BaseR

library(tidyverse)
library(magrittr)

mtcars %>%
  mutate(across(where(is.factor), as.numeric)) %$%
  cor.test(mpg, disp)%>% 
  broom::tidy()
## # A tibble: 1 x 8
##   estimate statistic  p.value parameter conf.low conf.high method    alternative
##      <dbl>     <dbl>    <dbl>     <int>    <dbl>     <dbl> <chr>     <chr>      
## 1   -0.848     -8.75 9.38e-10        30   -0.923    -0.708 Pearson'~ two.sided

2.1.2 rstatix

library(rstatix)
# 所有变量两两相关
mtcars %>% 
  cor_test()
## # A tibble: 121 x 8
##    var1  var2    cor statistic        p conf.low conf.high method 
##    <chr> <chr> <dbl>     <dbl>    <dbl>    <dbl>     <dbl> <chr>  
##  1 mpg   mpg    1       Inf    0.         1          1     Pearson
##  2 mpg   cyl   -0.85     -8.92 6.11e-10  -0.926     -0.716 Pearson
##  3 mpg   disp  -0.85     -8.75 9.38e-10  -0.923     -0.708 Pearson
##  4 mpg   hp    -0.78     -6.74 1.79e- 7  -0.885     -0.586 Pearson
##  5 mpg   drat   0.68      5.10 1.78e- 5   0.436      0.832 Pearson
##  6 mpg   wt    -0.87     -9.56 1.29e-10  -0.934     -0.744 Pearson
##  7 mpg   qsec   0.42      2.53 1.71e- 2   0.0820     0.670 Pearson
##  8 mpg   vs     0.66      4.86 3.42e- 5   0.410      0.822 Pearson
##  9 mpg   am     0.6       4.11 2.85e- 4   0.318      0.784 Pearson
## 10 mpg   gear   0.48      3.00 5.40e- 3   0.158      0.710 Pearson
## # ... with 111 more rows
# 指定变量间相关
mtcars %>% cor_test(
  vars = c("mpg", "wt"),
  vars2 = c("disp", "hp", "drat")
 )
## # A tibble: 6 x 8
##   var1  var2    cor statistic        p conf.low conf.high method 
##   <chr> <chr> <dbl>     <dbl>    <dbl>    <dbl>     <dbl> <chr>  
## 1 mpg   disp  -0.85     -8.75 9.38e-10   -0.923    -0.708 Pearson
## 2 mpg   hp    -0.78     -6.74 1.79e- 7   -0.885    -0.586 Pearson
## 3 mpg   drat   0.68      5.10 1.78e- 5    0.436     0.832 Pearson
## 4 wt    disp   0.89     10.6  1.22e-11    0.781     0.944 Pearson
## 5 wt    hp     0.66      4.80 4.15e- 5    0.403     0.819 Pearson
## 6 wt    drat  -0.71     -5.56 4.78e- 6   -0.850    -0.484 Pearson

2.2 相关系数

2.2.1 BaseR

mtcars %>% 
  mutate(across(where(is.factor), as.numeric)) %>%
  cor() %>%
  round(2)
##        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

2.2.2 rstatix

library(rstatix)
# 相关系数矩阵
mtcars %>%
  cor_mat(method = "pearson") 
## # A tibble: 11 x 12
##    rowname   mpg   cyl  disp    hp   drat     wt   qsec     vs     am   gear
##  * <chr>   <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
##  1 mpg      1    -0.85 -0.85 -0.78  0.68  -0.87   0.42   0.66   0.6    0.48 
##  2 cyl     -0.85  1     0.9   0.83 -0.7    0.78  -0.59  -0.81  -0.52  -0.49 
##  3 disp    -0.85  0.9   1     0.79 -0.71   0.89  -0.43  -0.71  -0.59  -0.56 
##  4 hp      -0.78  0.83  0.79  1    -0.45   0.66  -0.71  -0.72  -0.24  -0.13 
##  5 drat     0.68 -0.7  -0.71 -0.45  1     -0.71   0.091  0.44   0.71   0.7  
##  6 wt      -0.87  0.78  0.89  0.66 -0.71   1     -0.17  -0.55  -0.69  -0.580
##  7 qsec     0.42 -0.59 -0.43 -0.71  0.091 -0.17   1      0.74  -0.23  -0.21 
##  8 vs       0.66 -0.81 -0.71 -0.72  0.44  -0.55   0.74   1      0.17   0.21 
##  9 am       0.6  -0.52 -0.59 -0.24  0.71  -0.69  -0.23   0.17   1      0.79 
## 10 gear     0.48 -0.49 -0.56 -0.13  0.7   -0.580 -0.21   0.21   0.79   1    
## 11 carb    -0.55  0.53  0.39  0.75 -0.091  0.43  -0.66  -0.570  0.058  0.27 
## # ... with 1 more variable: carb <dbl>
# 概率矩阵
mtcars %>% 
  cor_pmat()
## # A tibble: 11 x 12
##    rowname      mpg      cyl     disp      hp    drat        wt    qsec
##    <chr>      <dbl>    <dbl>    <dbl>   <dbl>   <dbl>     <dbl>   <dbl>
##  1 mpg     0.       6.11e-10 9.38e-10 1.79e-7 1.78e-5 1.29e- 10 1.71e-2
##  2 cyl     6.11e-10 0.       1.80e-12 3.48e-9 8.24e-6 1.22e-  7 3.66e-4
##  3 disp    9.38e-10 1.80e-12 0.       7.14e-8 5.28e-6 1.22e- 11 1.31e-2
##  4 hp      1.79e- 7 3.48e- 9 7.14e- 8 0.      9.99e-3 4.15e-  5 5.77e-6
##  5 drat    1.78e- 5 8.24e- 6 5.28e- 6 9.99e-3 0.      4.78e-  6 6.20e-1
##  6 wt      1.29e-10 1.22e- 7 1.22e-11 4.15e-5 4.78e-6 2.27e-236 3.39e-1
##  7 qsec    1.71e- 2 3.66e- 4 1.31e- 2 5.77e-6 6.20e-1 3.39e-  1 0.     
##  8 vs      3.42e- 5 1.84e- 8 5.24e- 6 2.94e-6 1.17e-2 9.80e-  4 1.03e-6
##  9 am      2.85e- 4 2.15e- 3 3.66e- 4 1.80e-1 4.73e-6 1.13e-  5 2.06e-1
## 10 gear    5.40e- 3 4.17e- 3 9.64e- 4 4.93e-1 8.36e-6 4.59e-  4 2.43e-1
## 11 carb    1.08e- 3 1.94e- 3 2.53e- 2 7.83e-7 6.21e-1 1.46e-  2 4.54e-5
## # ... with 4 more variables: vs <dbl>, am <dbl>, gear <dbl>, carb <dbl>
# 或
mtcars %>% 
  cor_mat(method = "pearson") %>% 
  cor_get_pval()
## # A tibble: 11 x 12
##    rowname      mpg      cyl     disp      hp    drat        wt    qsec
##    <chr>      <dbl>    <dbl>    <dbl>   <dbl>   <dbl>     <dbl>   <dbl>
##  1 mpg     0.       6.11e-10 9.38e-10 1.79e-7 1.78e-5 1.29e- 10 1.71e-2
##  2 cyl     6.11e-10 0.       1.80e-12 3.48e-9 8.24e-6 1.22e-  7 3.66e-4
##  3 disp    9.38e-10 1.80e-12 0.       7.14e-8 5.28e-6 1.22e- 11 1.31e-2
##  4 hp      1.79e- 7 3.48e- 9 7.14e- 8 0.      9.99e-3 4.15e-  5 5.77e-6
##  5 drat    1.78e- 5 8.24e- 6 5.28e- 6 9.99e-3 0.      4.78e-  6 6.20e-1
##  6 wt      1.29e-10 1.22e- 7 1.22e-11 4.15e-5 4.78e-6 2.27e-236 3.39e-1
##  7 qsec    1.71e- 2 3.66e- 4 1.31e- 2 5.77e-6 6.20e-1 3.39e-  1 0.     
##  8 vs      3.42e- 5 1.84e- 8 5.24e- 6 2.94e-6 1.17e-2 9.80e-  4 1.03e-6
##  9 am      2.85e- 4 2.15e- 3 3.66e- 4 1.80e-1 4.73e-6 1.13e-  5 2.06e-1
## 10 gear    5.40e- 3 4.17e- 3 9.64e- 4 4.93e-1 8.36e-6 4.59e-  4 2.43e-1
## 11 carb    1.08e- 3 1.94e- 3 2.53e- 2 7.83e-7 6.21e-1 1.46e-  2 4.54e-5
## # ... with 4 more variables: vs <dbl>, am <dbl>, gear <dbl>, carb <dbl>

3 可视化

3.1 数据分布可视化

3.1.1 BaseR

pairs(mtcars, )

3.1.2 PerformanceAnalytics

library(PerformanceAnalytics)#加载包
chart.Correlation(mtcars, histogram=TRUE, pch=19)

3.2 相关系数可视化

3.2.1 rstatix

# 基本款
mtcars %>% 
  cor_mat() %>% 
  cor_plot()

## 不同的图示方式
  mtcars %>% 
  cor_mat() %>% 
  cor_plot(method = "pie")

    mtcars %>% 
  cor_mat() %>% 
  cor_plot(method = "ellipse" ) #"circle" (default), "square", "ellipse", "number",                         "pie", "shade" and "color". 

# 三角
mtcars %>% 
  cor_mat() %>%
  pull_lower_triangle() %>% # or pull_upper_triangle()
  cor_plot(insignificant = "blank")

# 加系数值
mtcars %>%
  cor_mat() %>%
  cor_plot(label = TRUE,
           font.label = list(
             size = .5,
             style = "bold",
             color = "gray"
           ))

3.2.2 corrplot

mtcars %>% 
  cor() %>% 
  corrplot::corrplot.mixed(upper = "ellipse")

3.2.3 GGallY

library(GGally)
mtcars %>% 
  ggcorr(label = T, label_alpha = T)

mtcars %>% 
  ggpairs()

3.2.4 ggcorrplot

pmat <- mtcars%>% 
  ggcorrplot::cor_pmat()
mtcars %>% 
  cor() %>% 
  ggcorrplot::ggcorrplot(hc.order = TRUE, type = "lower",
   lab = TRUE, lab_size = 2.5, p.mat = pmat)

3.2.5 heatmap

mtcars %>% 
  cor() %>%
  heatmap()

3.2.6 psych

mtcars %>% 
  cor() %>% 
  psych::spider(y = 1, x = 1:11, data = ., fill = T)

4 参考文献