###Gọi các gói phần mềm

##đọc dữ liệu

dat <- read.transactions("C:\\Users\\lehoa\\Desktop\\a Phong\\28.6 machine learning\\AR-thongkinh.csv", format = "basket", sep = ",")

#Mô tả dữ liệu

dim(dat)
## [1] 109 148
itemLabels(dat)
##   [1] "Agiao"                      "Bachbiendau"               
##   [3] "Bachduong"                  "Bachgioitu"                
##   [5] "Bachlinh"                   "Bachqua"                   
##   [7] "Bachthuoc"                  "Bachtruat"                 
##   [9] "Bakich"                     "Banha"                     
##  [11] "Baokhuong"                  "Biendau"                   
##  [13] "Boconganh"                  "Bocotchi"                  
##  [15] "Bohoang"                    "Boimau"                    
##  [17] "Camthao"                    "Cankhuong"                 
##  [19] "CanThankhuyhu"              "Canuatkhitrehuyetu"        
##  [21] "Canuattyhu"                 "Caoluongkhuong"            
##  [23] "Catcan"                     "Catcanh"                   
##  [25] "Caudang"                    "Cocnha"                    
##  [27] "Cuuhuongtrung"              "Cuutatung"                 
##  [29] "Chichthao"                  "Chitu"                     
##  [31] "Chithuc"                    "Chixac"                    
##  [33] "Daigiathach"                "Daitao"                    
##  [35] "Daitaoqua"                  "Damduonghoac"              
##  [37] "Danbi"                      "Dansam"                    
##  [39] "Dangsam"                    "Daonhan"                   
##  [41] "Dialong"                    "Dienhosach"                
##  [43] "Donbi"                      "Dotrong"                   
##  [45] "Duonghuhanngung"            "Duongquy"                  
##  [47] "Hadiep"                     "Haiphieutieu"              
##  [49] "Hakhothao"                  "Hanngunghuyetu"            
##  [51] "Hanthapngungtre"            "Hauphac"                   
##  [53] "Hoangba"                    "Hoangcam"                  
##  [55] "Hoangky"                    "Hoanglien"                 
##  [57] "Holoba"                     "Honghoa"                   
##  [59] "Huhan"                      "Huhanhuyetu"               
##  [61] "Huongphu"                   "HuyethukhitreHatieuhuhan"  
##  [63] "Ichmau"                     "Ichmauthao"                
##  [65] "Kehuyetdang"                "Kenoikim"                  
##  [67] "KTHUHNHU"                   "KhihuyetluonghuHatieuhuhan"
##  [69] "Khitrehuyetu"               "Kholuyentu"                
##  [71] "Khonthao"                   "Khuonghoang"               
##  [73] "Lechihach"                  "Lientu"                    
##  [75] "Linhchi"                    "Locgiac"                   
##  [77] "Luukyno"                    "Machmon"                   
##  [79] "Mahoang"                    "Maule"                     
##  [81] "Mocqua"                     "Mocthong"                  
##  [83] "Mochuong"                   "Motduoc"                   
##  [85] "Ngaidiep"                   "Ngatruat"                  
##  [87] "Ngocong"                    "Ngothudu"                  
##  [89] "Ngulinhchi"                 "Nguutat"                   
##  [91] "Nhamda"                     "Nhansam"                   
##  [93] "Nhansm"                     "Nhucqu"                    
##  [95] "Nhucque"                    "Nhucthungdung"             
##  [97] "Nhuhuong"                   "Oduoc"                     
##  [99] "Omai"                       "Phapbanha"                 
## [101] "Phutieumach"                "Phutu"                     
## [103] "Quathach"                   "Quechi"                    
## [105] "Quygiap"                    "Saiho"                     
## [107] "Sanhan"                     "Sinhdia"                   
## [109] "Sinhkhuong"                 "Sonchi"                    
## [111] "Sonduoc"                    "Sonthu"                    
## [113] "Sonthudu"                   "Sotuco"                    
## [115] "Tamlang"                    "Tamthat"                   
## [117] "Tangkysinh"                 "Taonhan"                   
## [119] "Tetan"                      "Tieuhoihuong"              
## [121] "Tieuhoihuongcai"            "Tieumach"                  
## [123] "Tuboixi"                    "Tucdoan"                   
## [125] "Tuthachanh"                 "Tyhuhanthap"               
## [127] "Tyqualac"                   "Thankhuc"                  
## [129] "Thanhbi"                    "Thienhoaphan"              
## [131] "Thomiettrung"               "Thongbach"                 
## [133] "Thongthao"                  "Thophuclinh"               
## [135] "Thotytu"                    "Thucdia"                   
## [137] "thumn"                      "Thuongtruat"               
## [139] "Trachlan"                   "Trachta"                   
## [141] "Tramhuong"                  "Tranbi"                    
## [143] "Uatkim"                     "Unhiethoket"               
## [145] "Xichthuoc"                  "Xuyenkhung"                
## [147] "Xuyenluyentu"               "Ydi"
summary(dat)
## transactions as itemMatrix in sparse format with
##  109 rows (elements/itemsets/transactions) and
##  148 columns (items) and a density of 0.08225886 
## 
## most frequent items:
##       Duongquy     Xuyenkhung     Dienhosach      Bachthuoc Hanngunghuyetu 
##             91             81             64             60             47 
##        (Other) 
##            984 
## 
## element (itemset/transaction) length distribution:
## sizes
##  2  3  5  6  7  8  9 10 11 12 13 14 15 16 17 19 
##  1  1  1  1  3  4  3  4 22 16 21 14  7  6  4  1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.00   11.00   12.00   12.17   14.00   19.00 
## 
## includes extended item information - examples:
##        labels
## 1       Agiao
## 2 Bachbiendau
## 3   Bachduong
display.brewer.all()

arules::itemFrequencyPlot(dat, topN = 20, col = brewer.pal(8, 'Pastel2'), main = 'Relative Item Frequency Plot', type = "relative", ylab = "Item Frequency (Relative)")

##Kiểm tra bộ dữ liệu phù hợp cho AR

sparsity <- 1 - (sum(as(dat, "ngCMatrix")) / (nrow(dat) * ncol(dat)))

cat("Sparsity của dữ liệu là:", round(sparsity * 100, 2), "%\n")
## Sparsity của dữ liệu là: 91.77 %

#Xác định ngưỡng support

rules <- apriori(dat, parameter = list(support = 0.01, confidence = 0.5))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.01      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 1 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[148 item(s), 109 transaction(s)] done [0.00s].
## sorting and recoding items ... [89 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 10
## Warning in apriori(dat, parameter = list(support = 0.01, confidence = 0.5)):
## Mining stopped (maxlen reached). Only patterns up to a length of 10 returned!
##  done [0.03s].
## writing ... [427294 rule(s)] done [0.08s].
## creating S4 object  ... done [0.10s].
rel_sup_rules <- quality(rules)$support

summary(rel_sup_rules)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.01835 0.01835 0.01835 0.02381 0.01835 0.83486
hist(rel_sup_rules, breaks = 50, col = "skyblue", main = "Histogram of Rule Support", xlab = "Support")

#Xây dựng quy luật liên kết chung

rules <- apriori(dat, parameter = list(supp=0.03, conf=0.5, maxlen=5, target= "rules"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.03      1
##  maxlen target  ext
##       5  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 3 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[148 item(s), 109 transaction(s)] done [0.00s].
## sorting and recoding items ... [59 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5
## Warning in apriori(dat, parameter = list(supp = 0.03, conf = 0.5, maxlen = 5, :
## Mining stopped (maxlen reached). Only patterns up to a length of 5 returned!
##  done [0.00s].
## writing ... [22125 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(sort(rules, by = 'confidence')[1:20])
##      lhs            rhs              support    confidence coverage   lift    
## [1]  {Catcanh}   => {Xuyenkhung}     0.03669725 1          0.03669725 1.345679
## [2]  {Huhan}     => {Duongquy}       0.03669725 1          0.03669725 1.197802
## [3]  {Trachta}   => {Bachlinh}       0.05504587 1          0.05504587 9.083333
## [4]  {Trachta}   => {Bachtruat}      0.05504587 1          0.05504587 7.785714
## [5]  {Trachta}   => {Bachthuoc}      0.05504587 1          0.05504587 1.816667
## [6]  {Trachta}   => {Xuyenkhung}     0.05504587 1          0.05504587 1.345679
## [7]  {Trachta}   => {Duongquy}       0.05504587 1          0.05504587 1.197802
## [8]  {Nhansam}   => {Donbi}          0.04587156 1          0.04587156 6.055556
## [9]  {Tranbi}    => {Xuyenkhung}     0.04587156 1          0.04587156 1.345679
## [10] {Tranbi}    => {Duongquy}       0.04587156 1          0.04587156 1.197802
## [11] {Uatkim}    => {Huongphu}       0.04587156 1          0.04587156 2.595238
## [12] {Uatkim}    => {Duongquy}       0.04587156 1          0.04587156 1.197802
## [13] {Baokhuong} => {Dienhosach}     0.05504587 1          0.05504587 1.703125
## [14] {Baokhuong} => {Xuyenkhung}     0.05504587 1          0.05504587 1.345679
## [15] {Baokhuong} => {Duongquy}       0.05504587 1          0.05504587 1.197802
## [16] {Thanhbi}   => {Duongquy}       0.05504587 1          0.05504587 1.197802
## [17] {Nhuhuong}  => {Motduoc}        0.05504587 1          0.05504587 4.192308
## [18] {Nhuhuong}  => {Xuyenkhung}     0.05504587 1          0.05504587 1.345679
## [19] {Nhuhuong}  => {Duongquy}       0.05504587 1          0.05504587 1.197802
## [20] {Ngatruat}  => {Hanngunghuyetu} 0.06422018 1          0.06422018 2.319149
##      count
## [1]  4    
## [2]  4    
## [3]  6    
## [4]  6    
## [5]  6    
## [6]  6    
## [7]  6    
## [8]  5    
## [9]  5    
## [10] 5    
## [11] 5    
## [12] 5    
## [13] 6    
## [14] 6    
## [15] 6    
## [16] 6    
## [17] 6    
## [18] 6    
## [19] 6    
## [20] 7
subrule <- head(rules , n = 5, by = "confidence")
plot(subrule, method = "graph", engine = "htmlwidget")

#Chọn luật mạnh

library(arules)

n <- length(dat)

K <- 5

folds <- sample(rep(1:K, length.out = n))

results <- data.frame(Fold = 1:K, NumRules = NA, Accuracy = NA)
for (k in 1:K) {
  
  test_idx <- which(folds == k)
  
  train_data <- dat[-test_idx]
  
  test_data <- dat[test_idx]
  
  rules <- apriori(train_data, parameter = list(supp = 0.03, conf = 0.5, target = "rules"))
  
  rhs_labels <- labels(rhs(rules))
  
  rhs_items <- gsub("[\\{\\}]", "", rhs_labels)
  
  rhs_support <- itemFrequency(train_data)[rhs_items]
  
  added_value <- quality(rules)$confidence - rhs_support
  
  quality(rules)$addedValue <- added_value
  
  good_rules <- subset(rules,
                       
                       subset = support >= 0.08 &
                         
                         confidence >= 0.95 &
                         
                         lift >= 5 &
                         
                         addedValue >= 0.85)
  if (length(good_rules) > 0) {
    
    matched <- is.subset(lhs(good_rules), test_data)
    
    rule_covered <- rowSums(matched) > 0
    
    acc <- mean(rule_covered)
    
  } else {
    
    acc <- NA
    
  }
  
  results$NumRules[k] <- length(good_rules)
  
  results$Accuracy[k] <- acc
  
}
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.03      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[135 item(s), 87 transaction(s)] done [0.00s].
## sorting and recoding items ... [64 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 10
## Warning in apriori(train_data, parameter = list(supp = 0.03, conf = 0.5, :
## Mining stopped (maxlen reached). Only patterns up to a length of 10 returned!
##  done [0.00s].
## writing ... [76841 rule(s)] done [0.01s].
## creating S4 object  ... done [0.02s].
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.03      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[134 item(s), 87 transaction(s)] done [0.00s].
## sorting and recoding items ... [71 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 10
## Warning in apriori(train_data, parameter = list(supp = 0.03, conf = 0.5, :
## Mining stopped (maxlen reached). Only patterns up to a length of 10 returned!
##  done [0.00s].
## writing ... [60302 rule(s)] done [0.00s].
## creating S4 object  ... done [0.01s].
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.03      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[131 item(s), 87 transaction(s)] done [0.00s].
## sorting and recoding items ... [66 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 10
## Warning in apriori(train_data, parameter = list(supp = 0.03, conf = 0.5, :
## Mining stopped (maxlen reached). Only patterns up to a length of 10 returned!
##  done [0.00s].
## writing ... [83370 rule(s)] done [0.01s].
## creating S4 object  ... done [0.02s].
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.03      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[143 item(s), 87 transaction(s)] done [0.00s].
## sorting and recoding items ... [66 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 10
## Warning in apriori(train_data, parameter = list(supp = 0.03, conf = 0.5, :
## Mining stopped (maxlen reached). Only patterns up to a length of 10 returned!
##  done [0.00s].
## writing ... [61587 rule(s)] done [0.01s].
## creating S4 object  ... done [0.01s].
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.03      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[133 item(s), 88 transaction(s)] done [0.00s].
## sorting and recoding items ... [65 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 10
## Warning in apriori(train_data, parameter = list(supp = 0.03, conf = 0.5, :
## Mining stopped (maxlen reached). Only patterns up to a length of 10 returned!
##  done [0.00s].
## writing ... [78421 rule(s)] done [0.01s].
## creating S4 object  ... done [0.01s].
print(results)
##   Fold NumRules  Accuracy
## 1    1        4 1.0000000
## 2    2       92 1.0000000
## 3    3      791 0.1517067
## 4    4      125 1.0000000
## 5    5       55 1.0000000
cat("Trung bình số luật:", mean(results$NumRules, na.rm = TRUE), "\n")
## Trung bình số luật: 213.4
cat("Độ chính xác trung bình:", round(mean(results$Accuracy, na.rm = TRUE) * 100, 2), "%\n")
## Độ chính xác trung bình: 83.03 %
inspect(sort(good_rules, by = 'confidence'))
##      lhs             rhs          support confidence   coverage     lift count addedValue
## [1]  {Banha,                                                                             
##       Donbi}      => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [2]  {Banha,                                                                             
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [3]  {Agiao,                                                                             
##       Donbi}      => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [4]  {Banha,                                                                             
##       Donbi,                                                                             
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [5]  {Banha,                                                                             
##       Donbi,                                                                             
##       Ngothudu}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [6]  {Banha,                                                                             
##       Donbi,                                                                             
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [7]  {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [8]  {Banha,                                                                             
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [9]  {Banha,                                                                             
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [10] {Banha,                                                                             
##       Duongquy,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [11] {Banha,                                                                             
##       Ngothudu,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [12] {Banha,                                                                             
##       Duongquy,                                                                          
##       Ngothudu}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [13] {Agiao,                                                                             
##       Donbi,                                                                             
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [14] {Agiao,                                                                             
##       Donbi,                                                                             
##       Ngothudu}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [15] {Agiao,                                                                             
##       Donbi,                                                                             
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [16] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [17] {Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.10227273          1 0.10227273 7.333333     9  0.8636364
## [18] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [19] {Banha,                                                                             
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [20] {Banha,                                                                             
##       Donbi,                                                                             
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [21] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [22] {Banha,                                                                             
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [23] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [24] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [25] {Banha,                                                                             
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [26] {Banha,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [27] {Banha,                                                                             
##       Duongquy,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [28] {Banha,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [29] {Agiao,                                                                             
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [30] {Agiao,                                                                             
##       Donbi,                                                                             
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [31] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [32] {Agiao,                                                                             
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [33] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu}   => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [34] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [35] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [36] {Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.10227273          1 0.10227273 7.333333     9  0.8636364
## [37] {Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.10227273          1 0.10227273 7.333333     9  0.8636364
## [38] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [39] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [40] {Banha,                                                                             
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [41] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [42] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [43] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [44] {Banha,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [45] {Agiao,                                                                             
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [46] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [47] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [48] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [49] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [50] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi}     => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [51] {Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.10227273          1 0.10227273 7.333333     9  0.8636364
## [52] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [53] {Banha,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [54] {Agiao,                                                                             
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364
## [55] {Bachthuoc,                                                                         
##       Donbi,                                                                             
##       Duongquy,                                                                          
##       Ngothudu,                                                                          
##       Quechi,                                                                            
##       Xuyenkhung} => {Machmon} 0.09090909          1 0.09090909 7.333333     8  0.8636364

#Xây dựng quy luật liên kết từng phần

hanngunghuyetu_rules_lhs <- apriori(dat, parameter = list(supp=0.01, conf=0.7, maxlen=10, minlen=2),appearance = list(default="rhs",lhs="Hanngunghuyetu"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.7    0.1    1 none FALSE            TRUE       5    0.01      2
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 1 
## 
## set item appearances ...[1 item(s)] done [0.00s].
## set transactions ...[148 item(s), 109 transaction(s)] done [0.00s].
## sorting and recoding items ... [89 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [2 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(sort(hanngunghuyetu_rules_lhs, by = 'confidence')[1:min(20, length(hanngunghuyetu_rules_lhs))])
##     lhs                 rhs          support   confidence coverage  lift    
## [1] {Hanngunghuyetu} => {Duongquy}   0.3761468 0.8723404  0.4311927 1.044891
## [2] {Hanngunghuyetu} => {Xuyenkhung} 0.3486239 0.8085106  0.4311927 1.087996
##     count
## [1] 41   
## [2] 38
subrules <- head(hanngunghuyetu_rules_lhs , n = 5, by = "confidence")

plot(subrules, shading = "confidence", method = "graph", engine = "htmlwidget")
khitrehuyetu_rules_lhs <- apriori(dat, parameter = list(supp=0.01, conf=0.1, maxlen=10, minlen=2),appearance = list(default="rhs",lhs="Khitrehuyetu"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.1    0.1    1 none FALSE            TRUE       5    0.01      2
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 1 
## 
## set item appearances ...[1 item(s)] done [0.00s].
## set transactions ...[148 item(s), 109 transaction(s)] done [0.00s].
## sorting and recoding items ... [89 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [34 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
subrules1 <- head(khitrehuyetu_rules_lhs, n = 7, by = "confidence")

inspect(sort(khitrehuyetu_rules_lhs , by = 'confidence'))
##      lhs               rhs            support    confidence coverage  lift     
## [1]  {Khitrehuyetu} => {Duongquy}     0.16513761 0.90       0.1834862 1.0780220
## [2]  {Khitrehuyetu} => {Xuyenkhung}   0.13761468 0.75       0.1834862 1.0092593
## [3]  {Khitrehuyetu} => {Dienhosach}   0.11926606 0.65       0.1834862 1.1070313
## [4]  {Khitrehuyetu} => {Huongphu}     0.11009174 0.60       0.1834862 1.5571429
## [5]  {Khitrehuyetu} => {Chixac}       0.09174312 0.50       0.1834862 4.1923077
## [6]  {Khitrehuyetu} => {Saiho}        0.08256881 0.45       0.1834862 2.8852941
## [7]  {Khitrehuyetu} => {Daonhan}      0.08256881 0.45       0.1834862 2.7250000
## [8]  {Khitrehuyetu} => {Xichthuoc}    0.08256881 0.45       0.1834862 1.3256757
## [9]  {Khitrehuyetu} => {Honghoa}      0.06422018 0.35       0.1834862 2.5433333
## [10] {Khitrehuyetu} => {Oduoc}        0.06422018 0.35       0.1834862 2.0078947
## [11] {Khitrehuyetu} => {Motduoc}      0.06422018 0.35       0.1834862 1.4673077
## [12] {Khitrehuyetu} => {Nguutat}      0.05504587 0.30       0.1834862 1.4863636
## [13] {Khitrehuyetu} => {Camthao}      0.05504587 0.30       0.1834862 0.8384615
## [14] {Khitrehuyetu} => {Bachthuoc}    0.05504587 0.30       0.1834862 0.5450000
## [15] {Khitrehuyetu} => {Chichthao}    0.04587156 0.25       0.1834862 1.0480769
## [16] {Khitrehuyetu} => {Ngulinhchi}   0.04587156 0.25       0.1834862 0.6987179
## [17] {Khitrehuyetu} => {Uatkim}       0.03669725 0.20       0.1834862 4.3600000
## [18] {Khitrehuyetu} => {Kehuyetdang}  0.03669725 0.20       0.1834862 2.1800000
## [19] {Khitrehuyetu} => {Dansam}       0.03669725 0.20       0.1834862 1.5571429
## [20] {Khitrehuyetu} => {Tieuhoihuong} 0.03669725 0.20       0.1834862 0.7517241
## [21] {Khitrehuyetu} => {Bohoang}      0.03669725 0.20       0.1834862 0.5190476
## [22] {Khitrehuyetu} => {Catcanh}      0.02752294 0.15       0.1834862 4.0875000
## [23] {Khitrehuyetu} => {Thanhbi}      0.02752294 0.15       0.1834862 2.7250000
## [24] {Khitrehuyetu} => {Bachlinh}     0.02752294 0.15       0.1834862 1.3625000
## [25] {Khitrehuyetu} => {Sinhdia}      0.01834862 0.10       0.1834862 3.6333333
## [26] {Khitrehuyetu} => {Trachta}      0.01834862 0.10       0.1834862 1.8166667
## [27] {Khitrehuyetu} => {Tranbi}       0.01834862 0.10       0.1834862 2.1800000
## [28] {Khitrehuyetu} => {Nhuhuong}     0.01834862 0.10       0.1834862 1.8166667
## [29] {Khitrehuyetu} => {Xuyenluyentu} 0.01834862 0.10       0.1834862 1.5571429
## [30] {Khitrehuyetu} => {Mochuong}     0.01834862 0.10       0.1834862 1.2111111
## [31] {Khitrehuyetu} => {Thucdia}      0.01834862 0.10       0.1834862 0.9083333
## [32] {Khitrehuyetu} => {Bachtruat}    0.01834862 0.10       0.1834862 0.7785714
## [33] {Khitrehuyetu} => {Donbi}        0.01834862 0.10       0.1834862 0.6055556
## [34] {Khitrehuyetu} => {Nhucque}      0.01834862 0.10       0.1834862 0.2945946
##      count
## [1]  18   
## [2]  15   
## [3]  13   
## [4]  12   
## [5]  10   
## [6]   9   
## [7]   9   
## [8]   9   
## [9]   7   
## [10]  7   
## [11]  7   
## [12]  6   
## [13]  6   
## [14]  6   
## [15]  5   
## [16]  5   
## [17]  4   
## [18]  4   
## [19]  4   
## [20]  4   
## [21]  4   
## [22]  3   
## [23]  3   
## [24]  3   
## [25]  2   
## [26]  2   
## [27]  2   
## [28]  2   
## [29]  2   
## [30]  2   
## [31]  2   
## [32]  2   
## [33]  2   
## [34]  2
plot(subrules1, shading = "confidence", method = "graph", engine = "htmlwidget")