R IN ACTION SELF-TUTORIAL-14 相关系数计算与作图corrplot或chart.Correlation方法 2020-06-05

首先看这个实例和相关系数作图结果,再做解释:

install.packages("corrplot") 
testcorr111 <- read_excel("C:/Users/Mr.R/Desktop/testcorr111.xlsx", sheet = "Sheet1")
View(testcorr111)
h<- testcorr111[,2:7] ## 是从第二列开始的数据计算相关系数!
cor(h) 
library(corrplot)
res_cor <- cor(h) +corrplot(corr=res_cor) + corrplot(corr = res_cor,order = "AOE",type="upper",tl.pos = "d") 
corrplot(corr = res_cor,add=TRUE, type="lower", method="number", order="AOE", diag=FALSE, tl.pos="n", cl.pos="n")

得到的图:


image.png

说明
相关系数计算与作图corrplot或chart.Correlation方法:
步骤
需要的工具包,可以用其中之一:


install.packages("corrplot")
library(corrplot)


install.packages("PerformanceAnalytics")
library(PerformanceAnalytics)

1) 首先是个matrix格式的数据testcorr111,如下


image.png

2)读取、处理与作图:
testcorr111 <- read_excel("C:/Users/Mr.R/Desktop/testcorr111.xlsx", sheet = "Sheet1")
View(testcorr111)
h<- testcorr111[,2:7] ## 是从第二列开始的数据进行相关系数的计算!不如报错!
cor(h)


image.png

library(corrplot)
res_cor <- cor(h)

corrplot(corr=res_cor)
corrplot(corr = res_cor,order = "AOE",type="upper",tl.pos = "d")#不带相关系数!

corrplot(corr = res_cor,add=TRUE, type="lower", method="number",order="AOE",diag=FALSE,tl.pos="n", cl.pos="n")# 可以带上相关系数!
会得到以下图:


image.png

image.png

或者用另一个函数:
install.packages("PerformanceAnalytics")
library(PerformanceAnalytics)
chart.Correlation(h, method = "pearson")
image.png

总的代码与结果:
install.packages("corrplot")
install.packages("PerformanceAnalytics")
testcorr111 <- read_excel("C:/Users/Mr.R/Desktop/testcorr111.xlsx", sheet = "Sheet1")
View(testcorr111)
h<- testcorr111[,2:7] ## 是从第二列开始的数据进行相关系数的计算!不如报错!
cor(h)
library(corrplot)
res_cor <- cor(h)
corrplot(corr=res_cor)
corrplot(corr = res_cor,order = "AOE",type="upper",tl.pos = "d")#不带相关系数!

image.png

testcorr111 <- read_excel("C:/Users/Mr.R/Desktop/testcorr111.xlsx", sheet = "Sheet1")
View(testcorr111)
h<- testcorr111[,2:7] ## 是从第二列开始的数据进行相关系数的计算!不如报错!
cor(h)
library(corrplot)
res_cor <- cor(h)
corrplot(corr = res_cor,add=TRUE, type="lower", method="number",order="AOE",diag=FALSE,tl.pos="n", cl.pos="n")# 可以带上相关系数!
image.png

`
testcorr111 <- read_excel("C:/Users/Mr.R/Desktop/testcorr111.xlsx", sheet = "Sheet1")
View(testcorr111)
h<- testcorr111[,2:7] ## 是从第二列开始计算!不如报错!
cor(h)
library(corrplot)
res_cor <- cor(h)
library(PerformanceAnalytics)
chart.Correlation(h, method = "pearson")
image.png

星号的意义:

  • p < 0.05
    

** p < 0.01
*** p < 0.001

你可能感兴趣的:(R IN ACTION SELF-TUTORIAL-14 相关系数计算与作图corrplot或chart.Correlation方法 2020-06-05)