library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#Đọc dữ liệu từ file water_potability
data <- read.csv("C:/Users/Dell/OneDrive/Documents/water_potability.csv")
#Xem 10 dòng đầu tiên
head(data,10)
## ph Hardness Solids Chloramines Sulfate Conductivity Organic_carbon
## 1 NA 204.8905 20791.32 7.300212 368.5164 564.3087 10.379783
## 2 3.716080 129.4229 18630.06 6.635246 NA 592.8854 15.180013
## 3 8.099124 224.2363 19909.54 9.275884 NA 418.6062 16.868637
## 4 8.316766 214.3734 22018.42 8.059332 356.8861 363.2665 18.436525
## 5 9.092223 181.1015 17978.99 6.546600 310.1357 398.4108 11.558279
## 6 5.584087 188.3133 28748.69 7.544869 326.6784 280.4679 8.399735
## 7 10.223862 248.0717 28749.72 7.513408 393.6634 283.6516 13.789695
## 8 8.635849 203.3615 13672.09 4.563009 303.3098 474.6076 12.363817
## 9 NA 118.9886 14285.58 7.804174 268.6469 389.3756 12.706049
## 10 11.180284 227.2315 25484.51 9.077200 404.0416 563.8855 17.927806
## Trihalomethanes Turbidity Potability
## 1 86.99097 2.963135 0
## 2 56.32908 4.500656 0
## 3 66.42009 3.055934 0
## 4 100.34167 4.628771 0
## 5 31.99799 4.075075 0
## 6 54.91786 2.559708 0
## 7 84.60356 2.672989 0
## 8 62.79831 4.401425 0
## 9 53.92885 3.595017 0
## 10 71.97660 4.370562 0
#Kiểm tra dữ liệu và cấu trúc ban đầu
str(data)
## 'data.frame': 3276 obs. of 10 variables:
## $ ph : num NA 3.72 8.1 8.32 9.09 ...
## $ Hardness : num 205 129 224 214 181 ...
## $ Solids : num 20791 18630 19910 22018 17979 ...
## $ Chloramines : num 7.3 6.64 9.28 8.06 6.55 ...
## $ Sulfate : num 369 NA NA 357 310 ...
## $ Conductivity : num 564 593 419 363 398 ...
## $ Organic_carbon : num 10.4 15.2 16.9 18.4 11.6 ...
## $ Trihalomethanes: num 87 56.3 66.4 100.3 32 ...
## $ Turbidity : num 2.96 4.5 3.06 4.63 4.08 ...
## $ Potability : int 0 0 0 0 0 0 0 0 0 0 ...
##====================
## XỬ LÍ GIÁ TRỊ THIẾU
##====================
#Kiểm tra giá trị thiếu
colSums(is.na(data))
## ph Hardness Solids Chloramines Sulfate
## 491 0 0 0 781
## Conductivity Organic_carbon Trihalomethanes Turbidity Potability
## 0 0 162 0 0
#Xử lí giá trị thiếu bằng cách tìm trung vị cho từng cột
median_ph <- median(data$ph, na.rm = TRUE)
median_Sulfate <- median(data$Sulfate, na.rm = TRUE)
median_Trihalomethanes <- median(data$Trihalomethanes, na.rm = TRUE)
#Thay các giá trị thiếu NA's bằng trung vị
data$ph[is.na(data$ph)] <- median_ph
data$Sulfate[is.na(data$Sulfate)] <- median_Sulfate
data$Trihalomethanes[is.na(data$Trihalomethanes)] <- median_Trihalomethanes
#Kiếm tra lại NA's sau khi điền
colSums(is.na(data))
## ph Hardness Solids Chloramines Sulfate
## 0 0 0 0 0
## Conductivity Organic_carbon Trihalomethanes Turbidity Potability
## 0 0 0 0 0
##======================
## TRỰC QUAN HÓA DỮ LIỆU
##======================
#Thiết lập layout để vẽ nhiều biểu đồ
par(mfrow=c(1,1))
#Vẽ histogram
#Histogram of pH
hist(data$ph, xlab = "pH", main = "Histogram of pH", col = "pink")

#Histogram of Sulfate
hist(data$Sulfate, xlab = "Sulfate", main = "Histogram of Sulfate", col = "yellow")

#Histogram of Trihalomethanes
hist(data$Trihalomethanes, xlab = "Trihalomethanes", main = "Histogram of Trihalomethanes", col = "orange")

#Histogram of Hardness
hist(data$Hardness, xlab = "Hardness", main = "Histogram of Hardness",col ="orange")

#Histogram of Conductivity
hist(data$Conductivity,xlab = "Conductivity", main = "Histogram of Conductivity",col = "lightblue")

#Histogram of Solids
hist(data$Solids,xlab = "Solids", main = "Histogram of Solids",col = "brown")

#Histogram of Chloramines
hist(data$Chloramines,xlab = "Chloramines",main = "Histogram of Choloramines",col = "purple")

#Histogram of Organic_carbon
hist(data$Organic_carbon,xlab = "Organic_carbon", main = "Histogram of Organic_carbon",col = "green")

#Histogram of Turbidity
hist(data$Turbidity, xlab = "Turbidity",main = "Histogram of Turbidity",col = "blue")

#Vẽ Boxplot - biểu đồ hộp và râu so sánh giá trị theo nhóm (0 = không uống được, 1 = uống được)
#Gán nhãn cho Potability
data$Potability <- factor(data$Potability,
levels = c(0,1),
labels = c("Không uống được","Uống được"))
str(data$Potability)
## Factor w/ 2 levels "Không uống được",..: 1 1 1 1 1 1 1 1 1 1 ...
table(data$Potability)
##
## Không uống được Uống được
## 1998 1278
#Boxplot của pH so với Potability
boxplot(ph ~ Potability, data = data,
main="pH and Potability",
col=c("pink","lightblue"))

#Boxplot của Sulfate so với Potability
boxplot(Sulfate ~ Potability, data = data,
main="Sulfate and Potability",
col=c("pink","lightblue"))

#Boxplot của Trihalomethanes so với Potability
boxplot(Trihalomethanes ~ Potability, data = data,
main="Trihalomethanes and Potability",
col=c("pink","lightblue"))

#Boxplot của Hardness
boxplot(Hardness ~ Potability, data = data,
main = "Hardness and Potability",
col = c("pink","lightblue"))

#Boxplot của độ dẫn điện so với Potability
boxplot(Conductivity ~ Potability, data = data,
main = "Conductivity and Potability",
col = c("pink","lightblue"))

#Boxplot của nồng độ chất rắn so với Potability
boxplot(Solids ~ Potability, data = data,
main="Solids and Potability",
col=c("pink","lightblue"))

#Boxplot của Chloramines so với Potability
boxplot(Chloramines ~ Potability, data = data,
main = "Chloramines and Potability",
col = c("pink","lightblue"))

#Boxplot của Carbon hữu cơ so với Potability
boxplot (Organic_carbon ~ Potability, data = data,
main = "Organic_carbon and Potability",
col = c("pink","lightblue"))

#Boxplot của độ đục so với Potability
boxplot(Turbidity ~ Potability, data = data,
main = "Turbidity and Potability",
col = c("pink","lightblue"))

##================
## THỐNG KÊ MÔ TẢ
##================
#Định nghĩa hàm thống kê
describe_function <- function(x){
c(n=length(x),
mean = mean(x),
sd = sd(x),
min = min(x),
max = max(x),
median = median(x),
Q1 = quantile(x, probs = 0.25),
Q3 = quantile(x, probs = 0.75))
}
#Áp dụng thống kê mô tả theo nhóm
by(data[,1:9], data$Potability,function(df){t(sapply(df, describe_function))})
## data$Potability: Không uống được
## n mean sd min max
## ph 1998 7.077736 1.5455903 0.000000 14.00000
## Hardness 1998 196.733292 31.0575397 98.452931 304.23591
## Solids 1998 21777.490788 8543.0687880 320.942611 61227.19601
## Chloramines 1998 7.092175 1.5010449 1.683993 12.65336
## Sulfate 1998 334.200184 31.9482935 203.444521 460.10707
## Conductivity 1998 426.730454 80.0473166 181.483754 753.34262
## Organic_carbon 1998 14.364335 3.3345535 4.371899 28.30000
## Trihalomethanes 1998 66.320635 15.6427868 0.738000 120.03008
## Turbidity 1998 3.965800 0.7802824 1.450000 6.73900
## median Q1.25% Q3.75%
## ph 7.036752 6.224046 7.940697
## Hardness 197.123423 177.823265 216.120687
## Solids 20809.618280 15663.057382 27006.249013
## Chloramines 7.090334 6.155640 8.066462
## Sulfate 333.073546 319.354715 348.316746
## Conductivity 422.229331 368.498530 480.677198
## Organic_carbon 14.293508 12.101057 16.649485
## Trihalomethanes 66.622485 56.441831 76.518849
## Turbidity 3.948076 3.444062 4.496106
## ------------------------------------------------------------
## data$Potability: Uống được
## n mean sd min max
## ph 1278 7.068655 1.3440133 0.2274991 13.175402
## Hardness 1278 195.800744 35.5470408 47.4320000 323.124000
## Solids 1278 22383.991018 9101.0102083 728.7508296 56488.672410
## Chloramines 1278 7.169338 1.7029877 0.3520000 13.127000
## Sulfate 1278 332.683125 41.8659268 129.0000000 481.030642
## Conductivity 1278 425.383800 82.0484462 201.6197368 695.369528
## Organic_carbon 1278 14.160893 3.2639068 2.2000000 23.604298
## Trihalomethanes 1278 66.543247 15.9719499 8.1758764 124.000000
## Turbidity 1278 3.968328 0.7808418 1.4922066 6.494249
## median Q1.25% Q3.75%
## ph 7.036752 6.351824 7.780068
## Hardness 196.632907 174.330531 218.003420
## Solids 21199.386615 15668.985038 27973.236447
## Chloramines 7.215163 6.094134 8.199261
## Sulfate 333.073546 313.052947 354.807924
## Conductivity 420.712729 360.939023 484.155911
## Organic_carbon 14.162809 12.033897 16.356245
## Trihalomethanes 66.622485 56.911186 77.067457
## Turbidity 3.958576 3.430909 4.509569
#Kiểm tra số lượng mẫu theo nhóm
table(data$Potability)
##
## Không uống được Uống được
## 1998 1278
barplot(table(data$Potability),
main = "Barplot of Potability",
xlab = "Potability",
ylab = "Frequency",
col = c("pink","lightblue"))

##=========================
## TRỰC QUAN HÓA THEO NHÓM
##=========================
#Histogram theo nhóm Potability
par(mfrow = c(3,3), mar = c(2.5,2.5,5.5,0.5)+0.1)
col_group <- c(rgb(1,0,0,0.5), rgb(0,0,1,0.5))
select_data <- data[,1:9]
for (col in names(select_data)){
group0 <- select_data[data$Potability == "Không uống được", col]
group1 <- select_data[data$Potability == "Uống được", col]
max_val <- max(c(group0, group1),na.rm = TRUE)
min_val <- min(c(group0, group1), na.rm = TRUE)
hist(group0,
breaks = 20,
col = col_group[1],
xlim = c(min_val,max_val),
xlab = col,
ylab = "Frequency",
main = paste("Biểu đồ tần suất của", col))
hist(group1,
breaks = 20,
add = TRUE)
legend("topleft",
legend = c("Không uống được", "Uống được"),
fill = col_group,
bty = "n",
bg = "white",
cex = 1.0,
inset = c(0,-0.6),
xpd = TRUE,
box.lty = 0)
}

##====================
## MA TRẬN TƯƠNG QUAN
##====================
#Ma trận tương quan các biến liên tục
par(mfrow=c(1,1))
M <- cor(data[,1:9], use = "complete.obs")
round (M,3)
## ph Hardness Solids Chloramines Sulfate Conductivity
## ph 1.000 0.076 -0.082 -0.032 0.014 0.017
## Hardness 0.076 1.000 -0.047 -0.030 -0.093 -0.024
## Solids -0.082 -0.047 1.000 -0.070 -0.150 0.014
## Chloramines -0.032 -0.030 -0.070 1.000 0.024 -0.020
## Sulfate 0.014 -0.093 -0.150 0.024 1.000 -0.014
## Conductivity 0.017 -0.024 0.014 -0.020 -0.014 1.000
## Organic_carbon 0.040 0.004 0.010 -0.013 0.027 0.021
## Trihalomethanes 0.003 -0.013 -0.009 0.017 -0.026 0.001
## Turbidity -0.036 -0.014 0.020 0.002 -0.010 0.006
## Organic_carbon Trihalomethanes Turbidity
## ph 0.040 0.003 -0.036
## Hardness 0.004 -0.013 -0.014
## Solids 0.010 -0.009 0.020
## Chloramines -0.013 0.017 0.002
## Sulfate 0.027 -0.026 -0.010
## Conductivity 0.021 0.001 0.006
## Organic_carbon 1.000 -0.013 -0.027
## Trihalomethanes -0.013 1.000 -0.021
## Turbidity -0.027 -0.021 1.000
#Lập ma trận
library(corrplot)
## corrplot 0.95 loaded
corrplot(M,
method = "color",
type = "full",
order = "original",
addgrid.col = "lightgrey",
tl.col = "red",
tl.srt = 90,
tl.cex = 1.0,
cl.cex = 1.0,
diag = TRUE)
