1. Kết nối dữ liệu
data(mtcars)
dulieu <-mtcars[,c("mpg","disp","hp","drat","wt","qsec")]
attach(dulieu)
head(dulieu)
## mpg disp hp drat wt qsec
## Mazda RX4 21.0 160 110 3.90 2.620 16.46
## Mazda RX4 Wag 21.0 160 110 3.90 2.875 17.02
## Datsun 710 22.8 108 93 3.85 2.320 18.61
## Hornet 4 Drive 21.4 258 110 3.08 3.215 19.44
## Hornet Sportabout 18.7 360 175 3.15 3.440 17.02
## Valiant 18.1 225 105 2.76 3.460 20.22
2. Tính hệ số tương quan 2 biến
2.1 Tính theo phương pháp Pearson
cor(mpg, hp, method = "pearson")
## [1] -0.7761684
cor.test(mpg, hp, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: mpg and hp
## t = -6.7424, df = 30, p-value = 1.788e-07
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.8852686 -0.5860994
## sample estimates:
## cor
## -0.7761684
2.2 Tính theo phương pháp Kendall
cor(mpg, hp, method = "kendall")
## [1] -0.7428125
cor.test(mpg, hp, method = "kendall")
## Warning in cor.test.default(mpg, hp, method = "kendall"): Cannot compute exact
## p-value with ties
##
## Kendall's rank correlation tau
##
## data: mpg and hp
## z = -5.871, p-value = 4.332e-09
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## -0.7428125
2.3 Tính theo phương pháp Spearman
cor(mpg, hp, method = "spearman")
## [1] -0.8946646
cor.test(mpg, hp, method = "spearman")
## Warning in cor.test.default(mpg, hp, method = "spearman"): Cannot compute exact
## p-value with ties
##
## Spearman's rank correlation rho
##
## data: mpg and hp
## S = 10337, p-value = 5.086e-12
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.8946646
3.Tính tương quan cho data
3.1 Tính tương quan GGally
library(GGally)
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following object is masked from 'dulieu':
##
## mpg
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
ggpairs(dulieu)

3.2 Tính tương quan ggcorplot
tuongquan <- round(cor(dulieu), 1)
tuongquan
## mpg disp hp drat wt qsec
## mpg 1.0 -0.8 -0.8 0.7 -0.9 0.4
## disp -0.8 1.0 0.8 -0.7 0.9 -0.4
## hp -0.8 0.8 1.0 -0.4 0.7 -0.7
## drat 0.7 -0.7 -0.4 1.0 -0.7 0.1
## wt -0.9 0.9 0.7 -0.7 1.0 -0.2
## qsec 0.4 -0.4 -0.7 0.1 -0.2 1.0
library(ggcorrplot)
ggcorrplot(tuongquan, hc.order = TRUE,
type = "lower",
lab = TRUE,
lab_size = 3,
method="circle",
colors = c("tomato2", "white", "springgreen3"),
title="Correlogram of mtcars",
ggtheme=theme_bw)

3.3 Tính tương quan corrplot
library(corrplot)
## corrplot 0.84 loaded
corrplot(tuongquan, method="number")

corrplot.mixed(tuongquan,tl.col='black')

3.5 Tương quan theo gói psych
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
pairs.panels(mtcars[,1:6])
