install.packages("ggpubr")
library(ggpubr)
## Loading required package: ggplot2
Ngữ pháp câu lệnh có dạng sau
cor(x, y, method = c("pearson", "kendall", "spearman"))
và cor.test(x, y, method=c("pearson", "kendall", "spearman"))
# Nếu data dạng .txt thì dùng lệnh: "my_data <- read.delim(file.choose())"
# Hoặc nếu data dạng .csv thì dùng lệnh "my_data <- read.csv(file.choose())"
df <- read.delim("~/Desktop/ngoc_122423/dat1(leaf).txt")
attach(df)
head(df)
## len wid rat cir pet species
## 1 17.42 6.58 2.65 0.67 2.24 L_cad
## 2 17.77 6.88 2.58 0.65 2.15 L_cad
## 3 20.65 7.73 2.67 0.67 2.15 L_cad
## 4 21.57 7.26 2.97 0.63 2.15 L_cad
## 5 25.07 8.34 3.01 0.61 2.40 L_cad
## 6 21.14 8.11 2.61 0.62 2.34 L_cad
# Chúng ta sẽ thực hiện tính mối tương quan giữa 2 biến "len" và "wid"
library("ggpubr")
ggscatter(df, x = "len", y = "wid",
add = "reg.line", conf.int = TRUE,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Blade Length", ylab = "Blade width")
## `geom_smooth()` using formula 'y ~ x'
Hiệp biến (covariation) có tuyến tính không? Có, nhìn vào biểu đồ trên chúng ta thấy, mối tương quan là tuyến tính. Trong tình huống biểu đồ hiển thị là hình cong, chúng ta đang xử lý mối tương quan phi tuyến tính giữa hai biến.
Dữ liệu từ 2 biến (x, y) có phân phối chuẩn hay không?*
# Shapiro-Wilk normality test for len
shapiro.test(df$len) # => p-value = 4.573e-06
##
## Shapiro-Wilk normality test
##
## data: df$len
## W = 0.93412, p-value = 4.573e-06
# Shapiro-Wilk normality test for wid
shapiro.test(df$wid) # => p-value = 7.321e-07
##
## Shapiro-Wilk normality test
##
## data: df$wid
## W = 0.92205, p-value = 7.321e-07
Từ kết qủa trên cả 2 giá trị P đều < 0.05 -> dữ liệu tuân theo luật phân phối chuẩn.
Trực quan kiểu phân phối dữ liệu bằng biểu đồ
library("ggpubr")
# len
ggqqplot(df$len, ylab = "Blade length")
# wid
ggqqplot(df$wid, ylab = "Blade width")
pear <- cor.test(df$len, df$wid, method = "pearson")
pear
##
## Pearson's product-moment correlation
##
## data: df$len and df$wid
## t = 16.499, df = 136, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.7521380 0.8655937
## sample estimates:
## cor
## 0.8166069
Kết quả trên cho thấy:
ken <- cor.test(df$len, df$wid, method = "kendall")
ken
##
## Kendall's rank correlation tau
##
## data: df$len and df$wid
## z = 8.7583, p-value < 2.2e-16
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.5038386
tau là hệ số tương quan kendall
Hệ số tương quan giữa len và wid là 0.504 và p-value < 2.2e-16
spear <- cor.test(df$len, df$wid, method = "spearman")
## Warning in cor.test.default(df$len, df$wid, method = "spearman"): Cannot compute
## exact p-value with ties
spear
##
## Spearman's rank correlation rho
##
## data: df$len and df$wid
## S = 139243, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.6820864
rho là hệ số tương quan Spearman*
Hệ số tương quan giữa lwen và wid theo spearman test là 0.682, với p-value < 2.2e-16
Hệ số tương quan sẽ có giá trị từ -1 đến 1
Tài liệu tham khảo
http://www.sthda.com/english/wiki/correlation-test-between-two-variables-in-r