R: 相关系数

ref: R画月亮阴晴圆缺:corrplot绘图相关系数矩阵

ref:R语言学习笔记之相关性矩阵分析及其可视化 -

Correlation【相关系数】, Variance【方差】 and Covariance【协方差】 (Matrices)

求相关系数:

n <- 10

x <- rnorm(n)

y <- rnorm(n)

# method=c("pearson", "kendall", "spearman")

cor(x, y,method = "pearson") #相关系数

cor.test(x,y)

可视化方法一:

#例子:

R: 相关系数_第1张图片
原始数据

library(corrplot)

M = cor(mtcars)

corrplot(M,add=TRUE, type="lower", method="number",order="AOE",diag=FALSE,tl.pos="n", cl.pos="n")

很详细的R包介绍-ref: R语言相关系数可视化之corrplot包 - 知乎

R: 相关系数_第2张图片
M

画图:

R: 相关系数_第3张图片

可视化方法二:

library(PerformanceAnalytics)#加载包

chart.Correlation(mydata, histogram=TRUE, pch=19)

R: 相关系数_第4张图片

可视化方法二:

heatmap()

col<- colorRampPalette(c("blue","white","red"))(20)#调用颜色版自定义颜色

heatmap(x = res, col = col, symm =TRUE)#symm表示是否对称

R: 相关系数_第5张图片

你可能感兴趣的:(R: 相关系数)