Cài đặt các thư viện cần thiết

# install.packages(c("e1071", "caret", "mlbench", "dplyr"))
library(e1071)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(mlbench)
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
# Tải dữ liệu
data(PimaIndiansDiabetes)

Bước 1: Khám phá dữ liệu

str(PimaIndiansDiabetes)
## 'data.frame':    768 obs. of  9 variables:
##  $ pregnant: num  6 1 8 1 0 5 3 10 2 8 ...
##  $ glucose : num  148 85 183 89 137 116 78 115 197 125 ...
##  $ pressure: num  72 66 64 66 40 74 50 0 70 96 ...
##  $ triceps : num  35 29 0 23 35 0 32 0 45 0 ...
##  $ insulin : num  0 0 0 94 168 0 88 0 543 0 ...
##  $ mass    : num  33.6 26.6 23.3 28.1 43.1 25.6 31 35.3 30.5 0 ...
##  $ pedigree: num  0.627 0.351 0.672 0.167 2.288 ...
##  $ age     : num  50 31 32 21 33 30 26 29 53 54 ...
##  $ diabetes: Factor w/ 2 levels "neg","pos": 2 1 2 1 2 1 2 1 2 2 ...
summary(PimaIndiansDiabetes)
##     pregnant         glucose         pressure         triceps     
##  Min.   : 0.000   Min.   :  0.0   Min.   :  0.00   Min.   : 0.00  
##  1st Qu.: 1.000   1st Qu.: 99.0   1st Qu.: 62.00   1st Qu.: 0.00  
##  Median : 3.000   Median :117.0   Median : 72.00   Median :23.00  
##  Mean   : 3.845   Mean   :120.9   Mean   : 69.11   Mean   :20.54  
##  3rd Qu.: 6.000   3rd Qu.:140.2   3rd Qu.: 80.00   3rd Qu.:32.00  
##  Max.   :17.000   Max.   :199.0   Max.   :122.00   Max.   :99.00  
##     insulin           mass          pedigree           age        diabetes 
##  Min.   :  0.0   Min.   : 0.00   Min.   :0.0780   Min.   :21.00   neg:500  
##  1st Qu.:  0.0   1st Qu.:27.30   1st Qu.:0.2437   1st Qu.:24.00   pos:268  
##  Median : 30.5   Median :32.00   Median :0.3725   Median :29.00            
##  Mean   : 79.8   Mean   :31.99   Mean   :0.4719   Mean   :33.24            
##  3rd Qu.:127.2   3rd Qu.:36.60   3rd Qu.:0.6262   3rd Qu.:41.00            
##  Max.   :846.0   Max.   :67.10   Max.   :2.4200   Max.   :81.00

Bước 2: Làm sạch dữ liệu

# Kiểm tra giá trị bị thiếu
sum(is.na(PimaIndiansDiabetes))
## [1] 0
# Thay thế giá trị 0 trong các cột (trừ 'pregnant' và 'diabetes') bằng NA
PimaIndiansDiabetes[, 2:8] <- lapply(PimaIndiansDiabetes[, 2:8], function(x) ifelse(x == 0, NA, x))

# Loại bỏ các hàng có giá trị bị thiếu
PimaIndiansDiabetes <- na.omit(PimaIndiansDiabetes)

Bước 3: Chuẩn hóa dữ liệu

preProcessRangeModel <- preProcess(PimaIndiansDiabetes[, 1:8], method = c("center", "scale"))
PimaIndiansDiabetes[, 1:8] <- predict(preProcessRangeModel, PimaIndiansDiabetes[, 1:8])

Bước 4: Chia dữ liệu thành tập huấn luyện và tập kiểm tra

set.seed(123)
trainIndex <- createDataPartition(PimaIndiansDiabetes$diabetes, p = 0.8, list = FALSE)
trainData <- PimaIndiansDiabetes[trainIndex, ]
testData <- PimaIndiansDiabetes[-trainIndex, ]

Bước 5: Huấn luyện mô hình Naive Bayes

model <- naiveBayes(diabetes ~ ., data = trainData)

# Dự đoán và đánh giá mô hình
predictions <- predict(model, testData)
confusionMatrix(predictions, testData$diabetes, positive = "pos")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction neg pos
##        neg  39   9
##        pos  13  17
##                                           
##                Accuracy : 0.7179          
##                  95% CI : (0.6047, 0.8141)
##     No Information Rate : 0.6667          
##     P-Value [Acc > NIR] : 0.2014          
##                                           
##                   Kappa : 0.3889          
##                                           
##  Mcnemar's Test P-Value : 0.5224          
##                                           
##             Sensitivity : 0.6538          
##             Specificity : 0.7500          
##          Pos Pred Value : 0.5667          
##          Neg Pred Value : 0.8125          
##              Prevalence : 0.3333          
##          Detection Rate : 0.2179          
##    Detection Prevalence : 0.3846          
##       Balanced Accuracy : 0.7019          
##                                           
##        'Positive' Class : pos             
##