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)

可视化方法一:

#例子:

13325312-a06d8f07b71240b5.png
原始数据

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包 - 知乎

13325312-f644935443b2494e.png
M

画图:

13325312-b18b085fe25ed46a.png

可视化方法二:

library(PerformanceAnalytics)#加载包

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

13325312-4aa115754981fc63.png

可视化方法二:

heatmap()

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

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

13325312-59eab9521bf928aa.png

猜你喜欢

转载自blog.csdn.net/weixin_34413802/article/details/87001428