文章目录
示例数据:
MASS
包中的
birthwt
数据集。
首先将数据集中的分类变量因子化,具体参考 这里。
连续变量间相关性
为便于处理,将数据集中的连续变量抽取为一新的数据集:cont.vars<-dplyr::select(birthwt,age,lwt,bwt)
计算Pearson、Spearman、Kendall相关系数:cor(,method="")
计算相关系数矩阵的语法:cor(数据框名,method="相关系数名称")
- Pearson相关系数:一般要求两个变量服从正态分布
- Spearman和Kendall相关系数:对变量分布没有要求
> cor(cont.vars,method="pearson")
age lwt bwt
age 1.00000000 0.1800732 0.09031781
lwt 0.18007315 1.0000000 0.18573328
bwt 0.09031781 0.1857333 1.00000000
> cor(cont.vars,method="spearman")
age lwt bwt
age 1.00000000 0.1860614 0.06123376
lwt 0.18606140 1.0