# 齐鲁风采群英会矩阵

For example, below is the correlation matrix for the dataset mtcars (which, as described by the help documentation of R, comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles).1 对于本文，我们只包括 连续的 variables.

dat <- mtcars[, c(1, 3:7)]
round(cor(dat), 2)
##        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

# 齐鲁风采群英会性测试

• \（h_0 \） : \（\ rho = 0 \）
• \（H_1） : \（\ rho \ ne 0 \）

In the context of our example, the correlogram above shows that the variables wt (weight) and hp (horsepower) are positively correlated, while the variables mpg (miles per gallon) and wt (weight) are negatively correlated (both correlations make sense if we think about it). Furthermore, the variables wt and qsec are not correlated (indicated by a white box). Even if the correlation coefficient is -0.17 between the 2 variables, the correlation test has shown that we cannot reject the hypothesis of no correlation. This is the reason the box for these two variable is white.

# 代码

For those interested to draw this correlogram with their own data, here is the code of the function I adapted based on the corrplot() function from the {corrplot} 包裹 (thanks again to all contributors of this package):

The main arguments in the corrplot2() function are the following:

• data：数据集的名称
• method：计算的齐鲁风采群英会方法，“Pearson”（默认），“肯德尔”或“Spearman”之一。如果您的数据集包含 定量连续 具有线性关系的变量，可以保留Pearson方法。如果你有 定性序单 具有部分线性链路的变量或定量变量，Spearman方法更合适
• sig.level：齐鲁风采群英会性测试的重要性水平，默认值为0.05
• order：变量的顺序，“原始”（默认），“AoE”（eigenvectors的角度顺序），“FPC”（第一个主体组件顺序），“Hclust”（分层聚类顺序），“字母”（按字母顺序排列）命令）
• diag：显示对角线上的齐鲁风采群英会系数？默认为 FALSE
• type：显示整个齐鲁风采群英会矩阵或简单的上/下部，“上”（默认），“较低”，“完整”之一
• tl.srt：可变标签的旋转
• （请注意，数据集中缺少的值会自动删除）

You can also play with the arguments of the corrplot2 function and see the results thanks to this r闪亮的应用程序 .

# {lares} package

Thanks to this article, I discovered the {lares} 包裹 which has really nice features regarding plotting correlations. Another advantage of this package is that it can be used to compute correlations with numerical, logical, categorical and date variables.

## 所有可能的齐鲁风采群英会性

Use the corr_cross() function if you want to compute all correlations and return the highest and significant ones in a plot:

# devtools::install_github("laresbernardo/lares")
library(lares)

corr_cross(dat, # name of dataset
max_pvalue = 0.05, # display only significant correlations (at 5% level)
top = 10 # display top 10 couples of variables (by correlation coefficient)
)

## 对所有其他变量对所有其他变量的齐鲁风采群英会性

Use the corr_var() function if you want to focus on the correlation of one variable against all others, and return the highest ones in a plot:

corr_var(dat, # name of dataset
mpg, # name of variable to focus on
top = 5 # display top 5 correlations
) 

# 参考

1. The dataset mtcars is preloaded in R by default, so there is no need to import it into R. Check the article “如何在r中导入excel文件“如果您需要帮助导入您自己的数据集。 ↩︎