關聯規則的視覺化

透過圖形的方式,可藉由視覺化方式,更直觀地顯示出關聯分析的結果。 這需要用到 R 的擴充套件 arulesViz

此處介紹一些此套件相關的簡單應用。


首先安裝與載入 arulesViz 套件

# install.packages("arulesViz", repos="http://cran.us.r-project.org")
library (arulesViz)  
## Warning: 套件 'arulesViz' 是用 R 版本 4.1.3 來建造的
## 載入需要的套件:arules
## Warning: 套件 'arules' 是用 R 版本 4.1.3 來建造的
## 載入需要的套件:Matrix
## Warning: 套件 'Matrix' 是用 R 版本 4.1.3 來建造的
## 
## 載入套件:'arules'
## 下列物件被遮斷自 'package:base':
## 
##     abbreviate, write
library (arules)   # 因為需要用到 Groceries 資料集
data("Groceries") 

接下來將支持度設置為 0.002,信賴度設置為 0.5,並將所得之關聯規則命名為 rules06。

rules06 = apriori (Groceries, parameter = list ( support=0.002, 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.002      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: 19 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[169 item(s), 9835 transaction(s)] done [0.01s].
## sorting and recoding items ... [147 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 done [0.01s].
## writing ... [1098 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules06  # 顯示 rules06 產生的關聯規則項數 
## set of 1098 rules

接下來對 rules06 繪製散佈圖:

plot(rules06)
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

圖中的每個點描述了相對應規則的支持度 (橫軸) 與信賴度 (縱軸),而顏色的深淺則由 lift (提升度) 值的高低來決定。可以透過參數設定,變更橫縱軸與顏色所對應的變量,例如:

plot(rules06, measure=c("support", "lift"), shading ="confidence")
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

由於觀察圖形,無法確認感興趣的規則對應到的是哪些商品,這個問題可以透過設置互動參數來克服 (可互動部分無法以網頁呈現,另行以程式與操作說明)

# plot(rules06, interactive=TRUE)

此外,我們還可以將 shading 參數設置為 “order” 來繪製出一種特殊的散佈圖- “two-key plot”,而關聯規則的顏色深淺代表其中所含有的商品的多少,商品的種類 (order) 越多,點的顏色越深。

plot(rules06, shading="order", control=list(main = "Two-key plot"))
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

接下來我們將圖形類型變更為 “grouped” 來產生 “grouped matrix” 圖形,以 lift 參數來看,關聯性最強 (圓點顏色最深) 的兩組產品是 硬質乳酪 (hard cheese) 及 黃油 (butter) 等產品與 生/酸奶油 (whipped/sour cream),而以 support 參數來看,則是特色起司 (specialty cheese) 及 奶油起司 (cream cheese) 等產品 與 全脂牛奶 (whole milk) 關聯性最強 (圓點尺寸最大)。

plot(rules06, method= "grouped")

至於 method 參數,還可以修改為 “matrix”。“matrix3D”、“paracoord” 等:

plot(rules06[1:50], method="matrix", measure="lift")
## Itemsets in Antecedent (LHS)
##  [1] "{other vegetables,rice}"             
##  [2] "{whole milk,rice}"                   
##  [3] "{herbs,whole milk}"                  
##  [4] "{citrus fruit,herbs}"                
##  [5] "{herbs,yogurt}"                      
##  [6] "{whole milk,specialty cheese}"       
##  [7] "{root vegetables,rice}"              
##  [8] "{herbs,whipped/sour cream}"          
##  [9] "{herbs,other vegetables}"            
## [10] "{tropical fruit,herbs}"              
## [11] "{herbs,rolls/buns}"                  
## [12] "{turkey,whole milk}"                 
## [13] "{root vegetables,processed cheese}"  
## [14] "{whole milk,frozen fish}"            
## [15] "{yogurt,specialty cheese}"           
## [16] "{whole milk,canned vegetables}"      
## [17] "{yogurt,frozen fish}"                
## [18] "{whole milk,frozen dessert}"         
## [19] "{root vegetables,herbs}"             
## [20] "{specialty cheese}"                  
## [21] "{whole milk,roll products }"         
## [22] "{whole milk,dishes}"                 
## [23] "{rice}"                              
## [24] "{cereals}"                           
## [25] "{root vegetables,pasta}"             
## [26] "{tropical fruit,semi-finished bread}"
## [27] "{other vegetables,frozen fish}"      
## [28] "{other vegetables,pot plants}"       
## [29] "{other vegetables,frozen dessert}"   
## [30] "{semi-finished bread,soda}"          
## [31] "{jam}"                               
## [32] "{other vegetables,cake bar}"         
## [33] "{turkey,other vegetables}"           
## [34] "{yogurt,pot plants}"                 
## [35] "{other vegetables,specialty cheese}" 
## [36] "{baking powder}"                     
## [37] "{processed cheese,white bread}"      
## [38] "{other vegetables,pasta}"            
## Itemsets in Consequent (RHS)
## [1] "{whole milk}"       "{other vegetables}" "{yogurt}"          
## [4] "{root vegetables}"

inspect(rules06[1:50])
##      lhs                       rhs                    support confidence    coverage     lift count
## [1]  {cereals}              => {whole milk}       0.003660397  0.6428571 0.005693950 2.515917    36
## [2]  {jam}                  => {whole milk}       0.002948653  0.5471698 0.005388917 2.141431    29
## [3]  {specialty cheese}     => {other vegetables} 0.004270463  0.5000000 0.008540925 2.584078    42
## [4]  {rice}                 => {other vegetables} 0.003965430  0.5200000 0.007625826 2.687441    39
## [5]  {rice}                 => {whole milk}       0.004677173  0.6133333 0.007625826 2.400371    46
## [6]  {baking powder}        => {whole milk}       0.009252669  0.5229885 0.017691917 2.046793    91
## [7]  {yogurt,                                                                                      
##       specialty cheese}     => {whole milk}       0.002033554  0.7142857 0.002846975 2.795464    20
## [8]  {whole milk,                                                                                  
##       specialty cheese}     => {yogurt}           0.002033554  0.5405405 0.003762074 3.874793    20
## [9]  {other vegetables,                                                                            
##       specialty cheese}     => {whole milk}       0.002236909  0.5238095 0.004270463 2.050007    22
## [10] {whole milk,                                                                                  
##       specialty cheese}     => {other vegetables} 0.002236909  0.5945946 0.003762074 3.072957    22
## [11] {turkey,                                                                                      
##       other vegetables}     => {whole milk}       0.002135231  0.5384615 0.003965430 2.107349    21
## [12] {turkey,                                                                                      
##       whole milk}           => {other vegetables} 0.002135231  0.5833333 0.003660397 3.014757    21
## [13] {root vegetables,                                                                             
##       rice}                 => {other vegetables} 0.002236909  0.7096774 0.003152008 3.667723    22
## [14] {other vegetables,                                                                            
##       rice}                 => {root vegetables}  0.002236909  0.5641026 0.003965430 5.175325    22
## [15] {root vegetables,                                                                             
##       rice}                 => {whole milk}       0.002440264  0.7741935 0.003152008 3.029922    24
## [16] {whole milk,                                                                                  
##       rice}                 => {root vegetables}  0.002440264  0.5217391 0.004677173 4.786665    24
## [17] {other vegetables,                                                                            
##       rice}                 => {whole milk}       0.002643620  0.6666667 0.003965430 2.609099    26
## [18] {whole milk,                                                                                  
##       rice}                 => {other vegetables} 0.002643620  0.5652174 0.004677173 2.921131    26
## [19] {other vegetables,                                                                            
##       frozen dessert}       => {whole milk}       0.002033554  0.5555556 0.003660397 2.174249    20
## [20] {whole milk,                                                                                  
##       frozen dessert}       => {other vegetables} 0.002033554  0.5128205 0.003965430 2.650336    20
## [21] {whole milk,                                                                                  
##       canned vegetables}    => {other vegetables} 0.002033554  0.5263158 0.003863752 2.720082    20
## [22] {whole milk,                                                                                  
##       roll products }       => {other vegetables} 0.002338587  0.5000000 0.004677173 2.584078    23
## [23] {yogurt,                                                                                      
##       frozen fish}          => {whole milk}       0.002236909  0.6875000 0.003253686 2.690634    22
## [24] {other vegetables,                                                                            
##       frozen fish}          => {whole milk}       0.002745297  0.5869565 0.004677173 2.297142    27
## [25] {whole milk,                                                                                  
##       frozen fish}          => {other vegetables} 0.002745297  0.5510204 0.004982206 2.847759    27
## [26] {other vegetables,                                                                            
##       cake bar}             => {whole milk}       0.002033554  0.5405405 0.003762074 2.115486    20
## [27] {whole milk,                                                                                  
##       dishes}               => {other vegetables} 0.002643620  0.5000000 0.005287239 2.584078    26
## [28] {yogurt,                                                                                      
##       pot plants}           => {whole milk}       0.002033554  0.5263158 0.003863752 2.059815    20
## [29] {other vegetables,                                                                            
##       pot plants}           => {whole milk}       0.002440264  0.5581395 0.004372140 2.184362    24
## [30] {root vegetables,                                                                             
##       pasta}                => {whole milk}       0.002338587  0.6052632 0.003863752 2.368788    23
## [31] {other vegetables,                                                                            
##       pasta}                => {whole milk}       0.002135231  0.5000000 0.004270463 1.956825    21
## [32] {herbs,                                                                                       
##       whipped/sour cream}   => {other vegetables} 0.002033554  0.6451613 0.003152008 3.334294    20
## [33] {citrus fruit,                                                                                
##       herbs}                => {other vegetables} 0.002135231  0.7241379 0.002948653 3.742457    21
## [34] {tropical fruit,                                                                              
##       herbs}                => {whole milk}       0.002338587  0.8214286 0.002846975 3.214783    23
## [35] {herbs,                                                                                       
##       yogurt}               => {root vegetables}  0.002033554  0.5714286 0.003558719 5.242537    20
## [36] {root vegetables,                                                                             
##       herbs}                => {other vegetables} 0.003863752  0.5507246 0.007015760 2.846231    38
## [37] {herbs,                                                                                       
##       other vegetables}     => {root vegetables}  0.003863752  0.5000000 0.007727504 4.587220    38
## [38] {root vegetables,                                                                             
##       herbs}                => {whole milk}       0.004168785  0.5942029 0.007015760 2.325502    41
## [39] {herbs,                                                                                       
##       whole milk}           => {root vegetables}  0.004168785  0.5394737 0.007727504 4.949369    41
## [40] {herbs,                                                                                       
##       yogurt}               => {other vegetables} 0.002033554  0.5714286 0.003558719 2.953232    20
## [41] {herbs,                                                                                       
##       yogurt}               => {whole milk}       0.002135231  0.6000000 0.003558719 2.348189    21
## [42] {herbs,                                                                                       
##       rolls/buns}           => {whole milk}       0.002440264  0.8000000 0.003050330 3.130919    24
## [43] {herbs,                                                                                       
##       other vegetables}     => {whole milk}       0.004067107  0.5263158 0.007727504 2.059815    40
## [44] {herbs,                                                                                       
##       whole milk}           => {other vegetables} 0.004067107  0.5263158 0.007727504 2.720082    40
## [45] {processed cheese,                                                                            
##       white bread}          => {whole milk}       0.002135231  0.5121951 0.004168785 2.004552    21
## [46] {root vegetables,                                                                             
##       processed cheese}     => {other vegetables} 0.002135231  0.6774194 0.003152008 3.501009    21
## [47] {root vegetables,                                                                             
##       processed cheese}     => {whole milk}       0.002033554  0.6451613 0.003152008 2.524935    20
## [48] {tropical fruit,                                                                              
##       semi-finished bread}  => {other vegetables} 0.002236909  0.5238095 0.004270463 2.707129    22
## [49] {tropical fruit,                                                                              
##       semi-finished bread}  => {whole milk}       0.002135231  0.5000000 0.004270463 1.956825    21
## [50] {semi-finished bread,                                                                         
##       soda}                 => {whole milk}       0.002236909  0.5500000 0.004067107 2.152507    22
plot(rules06[1:50], method="matrix3D", measure="lift")
## Warning in plot.rules(rules06[1:50], method = "matrix3D", measure = "lift"):
## method 'matrix3D' is deprecated use method 'matrix' with engine '3d'
## Itemsets in Antecedent (LHS)
##  [1] "{other vegetables,rice}"             
##  [2] "{whole milk,rice}"                   
##  [3] "{herbs,whole milk}"                  
##  [4] "{citrus fruit,herbs}"                
##  [5] "{herbs,yogurt}"                      
##  [6] "{whole milk,specialty cheese}"       
##  [7] "{root vegetables,rice}"              
##  [8] "{herbs,whipped/sour cream}"          
##  [9] "{herbs,other vegetables}"            
## [10] "{tropical fruit,herbs}"              
## [11] "{herbs,rolls/buns}"                  
## [12] "{turkey,whole milk}"                 
## [13] "{root vegetables,processed cheese}"  
## [14] "{whole milk,frozen fish}"            
## [15] "{yogurt,specialty cheese}"           
## [16] "{whole milk,canned vegetables}"      
## [17] "{yogurt,frozen fish}"                
## [18] "{whole milk,frozen dessert}"         
## [19] "{root vegetables,herbs}"             
## [20] "{specialty cheese}"                  
## [21] "{whole milk,roll products }"         
## [22] "{whole milk,dishes}"                 
## [23] "{rice}"                              
## [24] "{cereals}"                           
## [25] "{root vegetables,pasta}"             
## [26] "{tropical fruit,semi-finished bread}"
## [27] "{other vegetables,frozen fish}"      
## [28] "{other vegetables,pot plants}"       
## [29] "{other vegetables,frozen dessert}"   
## [30] "{semi-finished bread,soda}"          
## [31] "{jam}"                               
## [32] "{other vegetables,cake bar}"         
## [33] "{turkey,other vegetables}"           
## [34] "{yogurt,pot plants}"                 
## [35] "{other vegetables,specialty cheese}" 
## [36] "{baking powder}"                     
## [37] "{processed cheese,white bread}"      
## [38] "{other vegetables,pasta}"            
## Itemsets in Consequent (RHS)
## [1] "{whole milk}"       "{other vegetables}" "{yogurt}"          
## [4] "{root vegetables}"

plot(rules06[1:50], method="paracoord")