sessionInfo()
## R version 3.2.2 (2015-08-14)
## Platform: i386-w64-mingw32/i386 (32-bit)
## Running under: Windows 7 (build 7601) Service Pack 1
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
## [1] magrittr_1.5  tools_3.2.2   htmltools_0.3 stringi_1.0-1 rmarkdown_0.7
## [6] knitr_1.10.5  stringr_1.0.0 digest_0.6.8  evaluate_0.7
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## 
## The following objects are masked from 'package:base':
## 
##     %in%, write
library(arulesViz)
## Loading required package: grid
## 
## Attaching package: 'arulesViz'
## 
## The following object is masked from 'package:base':
## 
##     abbreviate
data("Groceries")
Groceries
## transactions in sparse format with
##  9835 transactions (rows) and
##  169 items (columns)
head(Groceries)
## transactions in sparse format with
##  6 transactions (rows) and
##  169 items (columns)
Groceries[1:6]
## transactions in sparse format with
##  6 transactions (rows) and
##  169 items (columns)
inspect(Groceries[1:6])
##   items                     
## 1 {citrus fruit,            
##    semi-finished bread,     
##    margarine,               
##    ready soups}             
## 2 {tropical fruit,          
##    yogurt,                  
##    coffee}                  
## 3 {whole milk}              
## 4 {pip fruit,               
##    yogurt,                  
##    cream cheese ,           
##    meat spreads}            
## 5 {other vegetables,        
##    whole milk,              
##    condensed milk,          
##    long life bakery product}
## 6 {whole milk,              
##    butter,                  
##    yogurt,                  
##    rice,                    
##    abrasive cleaner}
itemFrequencyPlot(Groceries,topN=20,type="absolute")

#apriori
rules.all=apriori(Groceries, 
                  parameter = list(supp = 0.001, conf = 0.8))
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport support minlen maxlen
##         0.8    0.1    1 none FALSE            TRUE   0.001      1     10
##  target   ext
##   rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## apriori - find association rules with the apriori algorithm
## version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
## sorting and recoding items ... [157 item(s)] done [0.00s].
## creating transaction tree ... done [0.01s].
## checking subsets of size 1 2 3 4 5 6 done [0.02s].
## writing ... [410 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules.all
## set of 410 rules
inspect(rules.all[1:5])
##   lhs                 rhs                support confidence      lift
## 1 {liquor,                                                           
##    red/blush wine} => {bottled beer} 0.001931876  0.9047619 11.235269
## 2 {curd,                                                             
##    cereals}        => {whole milk}   0.001016777  0.9090909  3.557863
## 3 {yogurt,                                                           
##    cereals}        => {whole milk}   0.001728521  0.8095238  3.168192
## 4 {butter,                                                           
##    jam}            => {whole milk}   0.001016777  0.8333333  3.261374
## 5 {soups,                                                            
##    bottled beer}   => {whole milk}   0.001118454  0.9166667  3.587512
plot(rules.all[1:5])

plot(rules.all[1:5],method="graph",interactive = F)

plot(rules.all[1:15],method="graph",interactive = T)

subrules2 <- head(sort(rules.all, by="lift"), 10)
plot(subrules2, method="graph")

plot(subrules2, method="grouped")

plot(subrules2, method="matrix")
## Itemsets in Antecedent (LHS)
##  [1] "{liquor,red/blush wine}"                                   
##  [2] "{citrus fruit,other vegetables,soda,fruit/vegetable juice}"
##  [3] "{tropical fruit,other vegetables,whole milk,yogurt,oil}"   
##  [4] "{citrus fruit,grapes,fruit/vegetable juice}"               
##  [5] "{other vegetables,whole milk,yogurt,rice}"                 
##  [6] "{tropical fruit,other vegetables,whole milk,oil}"          
##  [7] "{ham,pip fruit,other vegetables,yogurt}"                   
##  [8] "{beef,citrus fruit,tropical fruit,other vegetables}"       
##  [9] "{root vegetables,butter,cream cheese }"                    
## [10] "{tropical fruit,whole milk,butter,sliced cheese}"          
## Itemsets in Consequent (RHS)
## [1] "{bottled beer}"    "{root vegetables}" "{tropical fruit}" 
## [4] "{yogurt}"

plot(rules.all[1:15], method="matrix3D")
## Itemsets in Antecedent (LHS)
##  [1] "{liquor,red/blush wine}"                    
##  [2] "{curd,cereals}"                             
##  [3] "{yogurt,cereals}"                           
##  [4] "{butter,jam}"                               
##  [5] "{soups,bottled beer}"                       
##  [6] "{napkins,house keeping products}"           
##  [7] "{whipped/sour cream,house keeping products}"
##  [8] "{pastry,sweet spreads}"                     
##  [9] "{turkey,curd}"                              
## [10] "{rice,sugar}"                               
## [11] "{butter,rice}"                              
## [12] "{domestic eggs,rice}"                       
## [13] "{rice,bottled water}"                       
## [14] "{yogurt,rice}"                              
## [15] "{oil,mustard}"                              
## Itemsets in Consequent (RHS)
## [1] "{bottled beer}"     "{whole milk}"       "{other vegetables}"

plot(rules.all[1:15], method="matrix3D",measure="lift")
## Itemsets in Antecedent (LHS)
##  [1] "{liquor,red/blush wine}"                    
##  [2] "{curd,cereals}"                             
##  [3] "{yogurt,cereals}"                           
##  [4] "{butter,jam}"                               
##  [5] "{soups,bottled beer}"                       
##  [6] "{napkins,house keeping products}"           
##  [7] "{whipped/sour cream,house keeping products}"
##  [8] "{pastry,sweet spreads}"                     
##  [9] "{turkey,curd}"                              
## [10] "{rice,sugar}"                               
## [11] "{butter,rice}"                              
## [12] "{domestic eggs,rice}"                       
## [13] "{rice,bottled water}"                       
## [14] "{yogurt,rice}"                              
## [15] "{oil,mustard}"                              
## Itemsets in Consequent (RHS)
## [1] "{bottled beer}"     "{whole milk}"       "{other vegetables}"

plot(rules.all, measure=c("support", "lift"), shading="confidence")

plot(subrules2, method="paracoord")

#Example from
#http://datamining.togaware.com/survivor/Video_Marketing.html

getwd()
## [1] "C:/Users/dell/Desktop/Teaching"
dvd.transactions <- read.transactions("DVD.csv", sep=",")
## Warning in readLines(file, encoding = encoding): incomplete final line
## found on 'DVD.csv'
dvd.transactions
## transactions in sparse format with
##  10 transactions (rows) and
##  11 items (columns)
summary(dvd.transactions)
## transactions as itemMatrix in sparse format with
##  10 rows (elements/itemsets/transactions) and
##  11 columns (items) and a density of 0.2727273 
## 
## most frequent items:
##     Gladiator       Patriot   Sixth Sense    Green Mile Harry Potter1 
##             6             6             6             2             2 
##       (Other) 
##             8 
## 
## element (itemset/transaction) length distribution:
## sizes
## 2 3 4 5 
## 3 5 1 1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.00    2.25    3.00    3.00    3.00    5.00 
## 
## includes extended item information - examples:
##       labels
## 1 Braveheart
## 2  Galdiator
## 3  Gladiator
dvdrules.all=apriori(dvd.transactions, 
                  parameter = list(supp = 0.2, conf = 0.7))
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport support minlen maxlen
##         0.7    0.1    1 none FALSE            TRUE     0.2      1     10
##  target   ext
##   rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## apriori - find association rules with the apriori algorithm
## version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[11 item(s), 10 transaction(s)] done [0.00s].
## sorting and recoding items ... [7 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 done [0.00s].
## writing ... [7 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
dvdrules.all
## set of 7 rules
inspect(dvdrules.all)
##   lhs              rhs           support confidence     lift
## 1 {LOTR1}       => {LOTR2}           0.2          1 5.000000
## 2 {LOTR2}       => {LOTR1}           0.2          1 5.000000
## 3 {Green Mile}  => {Sixth Sense}     0.2          1 1.666667
## 4 {Gladiator}   => {Patriot}         0.6          1 1.666667
## 5 {Patriot}     => {Gladiator}       0.6          1 1.666667
## 6 {Gladiator,                                               
##    Sixth Sense} => {Patriot}         0.4          1 1.666667
## 7 {Patriot,                                                 
##    Sixth Sense} => {Gladiator}       0.4          1 1.666667
plot(dvdrules.all,method="graph",interactive = F)