library("Hmisc")
## Warning: package 'Hmisc' was built under R version 4.2.3
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
data("swiss")
head(swiss, 6)
## Fertility Agriculture Examination Education Catholic
## Courtelary 80.2 17.0 15 12 9.96
## Delemont 83.1 45.1 6 9 84.84
## Franches-Mnt 92.5 39.7 5 5 93.40
## Moutier 85.8 36.5 12 7 33.77
## Neuveville 76.9 43.5 17 15 5.16
## Porrentruy 76.1 35.3 9 7 90.57
## Infant.Mortality
## Courtelary 22.2
## Delemont 22.2
## Franches-Mnt 20.2
## Moutier 20.3
## Neuveville 20.6
## Porrentruy 26.6
res <- cor(swiss)
round(res, 2)
## Fertility Agriculture Examination Education Catholic
## Fertility 1.00 0.35 -0.65 -0.66 0.46
## Agriculture 0.35 1.00 -0.69 -0.64 0.40
## Examination -0.65 -0.69 1.00 0.70 -0.57
## Education -0.66 -0.64 0.70 1.00 -0.15
## Catholic 0.46 0.40 -0.57 -0.15 1.00
## Infant.Mortality 0.42 -0.06 -0.11 -0.10 0.18
## Infant.Mortality
## Fertility 0.42
## Agriculture -0.06
## Examination -0.11
## Education -0.10
## Catholic 0.18
## Infant.Mortality 1.00
res2 <- rcorr(as.matrix(swiss))
res2
## Fertility Agriculture Examination Education Catholic
## Fertility 1.00 0.35 -0.65 -0.66 0.46
## Agriculture 0.35 1.00 -0.69 -0.64 0.40
## Examination -0.65 -0.69 1.00 0.70 -0.57
## Education -0.66 -0.64 0.70 1.00 -0.15
## Catholic 0.46 0.40 -0.57 -0.15 1.00
## Infant.Mortality 0.42 -0.06 -0.11 -0.10 0.18
## Infant.Mortality
## Fertility 0.42
## Agriculture -0.06
## Examination -0.11
## Education -0.10
## Catholic 0.18
## Infant.Mortality 1.00
##
## n= 47
##
##
## P
## Fertility Agriculture Examination Education Catholic
## Fertility 0.0149 0.0000 0.0000 0.0010
## Agriculture 0.0149 0.0000 0.0000 0.0052
## Examination 0.0000 0.0000 0.0000 0.0000
## Education 0.0000 0.0000 0.0000 0.3018
## Catholic 0.0010 0.0052 0.0000 0.3018
## Infant.Mortality 0.0036 0.6845 0.4454 0.5065 0.2380
## Infant.Mortality
## Fertility 0.0036
## Agriculture 0.6845
## Examination 0.4454
## Education 0.5065
## Catholic 0.2380
## Infant.Mortality
res2$r
## Fertility Agriculture Examination Education Catholic
## Fertility 1.0000000 0.35307918 -0.6458827 -0.66378886 0.4636847
## Agriculture 0.3530792 1.00000000 -0.6865422 -0.63952252 0.4010951
## Examination -0.6458827 -0.68654221 1.0000000 0.69841530 -0.5727418
## Education -0.6637889 -0.63952252 0.6984153 1.00000000 -0.1538589
## Catholic 0.4636847 0.40109505 -0.5727418 -0.15385892 1.0000000
## Infant.Mortality 0.4165560 -0.06085861 -0.1140216 -0.09932185 0.1754959
## Infant.Mortality
## Fertility 0.41655603
## Agriculture -0.06085861
## Examination -0.11402160
## Education -0.09932185
## Catholic 0.17549591
## Infant.Mortality 1.00000000
res2$P
## Fertility Agriculture Examination Education
## Fertility NA 1.491720e-02 9.450437e-07 3.658617e-07
## Agriculture 1.491720e-02 NA 9.951515e-08 1.304590e-06
## Examination 9.450437e-07 9.951515e-08 NA 4.811397e-08
## Education 3.658617e-07 1.304590e-06 4.811397e-08 NA
## Catholic 1.028523e-03 5.204434e-03 2.588308e-05 3.018078e-01
## Infant.Mortality 3.585238e-03 6.844724e-01 4.453814e-01 5.065456e-01
## Catholic Infant.Mortality
## Fertility 1.028523e-03 0.003585238
## Agriculture 5.204434e-03 0.684472412
## Examination 2.588308e-05 0.445381445
## Education 3.018078e-01 0.506545555
## Catholic NA 0.238029702
## Infant.Mortality 2.380297e-01 NA
library(Hmisc)
res2<-rcorr(as.matrix(mtcars[,1:7]))
Hàm corrplot() lấy ma trận tương quan làm đối số đầu tiên. Đối số thứ hai (type=“upper”) được sử dụng để chỉ hiển thị tam giác trên của ma trận tương quan .
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.2.3
## corrplot 0.92 loaded
corrplot(res, type = "upper", order = "hclust", tl.col = "black", tl.srt = 45)
Tương quan tích cực được hiển thị bằng màu xanh lam và tương quan tiêu
cực có màu đỏ. Cường độ màu và kích thước của vòng tròn tỷ lệ thuận với
các hệ số tương quan . Ở phía bên phải của biểu đồ tương quan , màu
huyền thoại hiển thị các hệ số tương quan và các màu tương ứng.
library("PerformanceAnalytics")
## Warning: package 'PerformanceAnalytics' was built under R version 4.2.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
chart.Correlation(swiss, histogram=TRUE, pch=19)
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
Trong cốt truyện trên:
Phân phối của từng biến được hiển thị trên đường chéo. Ở dưới cùng của đường chéo: các biểu đồ phân tán hai chiều với một đường phù hợp được hiển thị Trên cùng của đường chéo : giá trị của mối tương quan cộng với mức ý nghĩa như các ngôi sao Mỗi mức ý nghĩa được liên kết với một ký hiệu : giá trị p(0, 0,001, 0,01, 0,05, 0,1, 1) <=> ký hiệu(“”, “”, “”, “.”, ” “)
col<- colorRampPalette(c("blue", "white", "red"))(20)
heatmap(x = res, col = col, symm = TRUE)