Data <- read.csv("D:\\DataScience\\Assignments\\AssociationRules\\book.csv")
colnames(Data)
##  [1] "ChildBks"  "YouthBks"  "CookBks"   "DoItYBks"  "RefBks"   
##  [6] "ArtBks"    "GeogBks"   "ItalCook"  "ItalAtlas" "ItalArt"  
## [11] "Florence"
Data$ChildBks <- factor(Data$ChildBks,levels = c("1","0"),labels = c("ChildBks",""))

Data$YouthBks <- factor(Data$YouthBks,levels = c("1","0"),labels = c("YouthBks",""))

Data$CookBks <- factor(Data$CookBks,levels = c("1","0"),labels = c("CookBks",""))

Data$DoItYBks <- factor(Data$DoItYBks,levels = c("1","0"),labels = c("DoItYBks",""))

Data$RefBks <- factor(Data$RefBks,levels = c("1","0"),labels = c("RefBks",""))

Data$ArtBks <- factor(Data$ArtBks,levels = c("1","0"),labels = c("ArtBks",""))

Data$GeogBks <- factor(Data$GeogBks,levels = c("1","0"),labels = c("GeogBks",""))

Data$ItalCook <- factor(Data$ItalCook,levels = c("1","0"),labels = c("ItalCook",""))

Data$ItalAtlas <- factor(Data$ItalAtlas,levels = c("1","0"),labels = c("ItalAtlas",""))

Data$ItalArt <- factor(Data$ItalArt,levels = c("1","0"),labels = c("ItalArt",""))
Data$Florence <- factor(Data$Florence,levels = c("1","0"),labels = c("Florence",""))
library(car)
## Warning: package 'car' was built under R version 3.5.1
## Loading required package: carData
library(carData)
library(arules)
## Warning: package 'arules' was built under R version 3.5.1
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Warning: package 'arulesViz' was built under R version 3.5.1
## Loading required package: grid
library(mvinfluence)
## Warning: package 'mvinfluence' was built under R version 3.5.1
## Loading required package: heplots
## Warning: package 'heplots' was built under R version 3.5.1
Data1 <- as(Data,"transactions")
# Item Frequency plot
itemFrequencyPlot(Data1,topN=25)

Book_apriori <- apriori(Data1, parameter = list(supp=0.005, conf=0.45, minlen=2, maxlen=4))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.45    0.1    1 none FALSE            TRUE       5   0.005      2
##  maxlen target   ext
##       4  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 10 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[22 item(s), 2000 transaction(s)] done [0.00s].
## sorting and recoding items ... [22 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4
## Warning in apriori(Data1, parameter = list(supp = 0.005, conf = 0.45,
## minlen = 2, : Mining stopped (maxlen reached). Only patterns up to a length
## of 4 returned!
##  done [0.01s].
## writing ... [12822 rule(s)] done [0.00s].
## creating S4 object  ... done [0.02s].
Book_apriori
## set of 12822 rules
inspect(head(sort(Book_apriori),n=20))
##      lhs                       rhs          support confidence lift    
## [1]  {ItalArt=}             => {ItalAtlas=} 0.9310  0.9784551  1.016049
## [2]  {ItalAtlas=}           => {ItalArt=}   0.9310  0.9667705  1.016049
## [3]  {ItalCook=}            => {ItalArt=}   0.8755  0.9875917  1.037931
## [4]  {ItalArt=}             => {ItalCook=}  0.8755  0.9201261  1.037931
## [5]  {ItalCook=}            => {ItalAtlas=} 0.8725  0.9842076  1.022022
## [6]  {ItalAtlas=}           => {ItalCook=}  0.8725  0.9060228  1.022022
## [7]  {ItalCook=,ItalArt=}   => {ItalAtlas=} 0.8655  0.9885780  1.026561
## [8]  {ItalCook=,ItalAtlas=} => {ItalArt=}   0.8655  0.9919771  1.042540
## [9]  {ItalAtlas=,ItalArt=}  => {ItalCook=}  0.8655  0.9296455  1.048670
## [10] {Florence=}            => {ItalAtlas=} 0.8610  0.9657880  1.002895
## [11] {ItalAtlas=}           => {Florence=}  0.8610  0.8940810  1.002895
## [12] {Florence=}            => {ItalArt=}   0.8555  0.9596186  1.008532
## [13] {ItalArt=}             => {Florence=}  0.8555  0.8991067  1.008532
## [14] {ItalArt=,Florence=}   => {ItalAtlas=} 0.8375  0.9789597  1.016573
## [15] {ItalAtlas=,Florence=} => {ItalArt=}   0.8375  0.9727062  1.022287
## [16] {ItalAtlas=,ItalArt=}  => {Florence=}  0.8375  0.8995704  1.009053
## [17] {ItalCook=}            => {Florence=}  0.7955  0.8973491  1.006561
## [18] {Florence=}            => {ItalCook=}  0.7955  0.8923163  1.006561
## [19] {ItalCook=,Florence=}  => {ItalArt=}   0.7875  0.9899434  1.040403
## [20] {ItalCook=,ItalArt=}   => {Florence=}  0.7875  0.8994860  1.008958
##      count
## [1]  1862 
## [2]  1862 
## [3]  1751 
## [4]  1751 
## [5]  1745 
## [6]  1745 
## [7]  1731 
## [8]  1731 
## [9]  1731 
## [10] 1722 
## [11] 1722 
## [12] 1711 
## [13] 1711 
## [14] 1675 
## [15] 1675 
## [16] 1675 
## [17] 1591 
## [18] 1591 
## [19] 1575 
## [20] 1575
inspect(tail(sort(Book_apriori),n=20))
##      lhs                    rhs                 support confidence      lift count
## [1]  {YouthBks=YouthBks,                                                          
##       ItalCook=ItalCook,                                                          
##       Florence=Florence} => {ItalAtlas=}          0.005  0.6666667 0.6922811    10
## [2]  {DoItYBks=DoItYBks,                                                          
##       ItalCook=ItalCook,                                                          
##       Florence=Florence} => {RefBks=}             0.005  0.5555556 0.7072636    10
## [3]  {RefBks=,                                                                    
##       ItalCook=ItalCook,                                                          
##       Florence=Florence} => {DoItYBks=DoItYBks}   0.005  0.5000000 1.7730496    10
## [4]  {DoItYBks=,                                                                  
##       ItalCook=ItalCook,                                                          
##       Florence=Florence} => {RefBks=}             0.005  0.5882353 0.7488673    10
## [5]  {RefBks=,                                                                    
##       ItalCook=ItalCook,                                                          
##       Florence=Florence} => {DoItYBks=}           0.005  0.5000000 0.6963788    10
## [6]  {YouthBks=,                                                                  
##       ItalCook=ItalCook,                                                          
##       Florence=Florence} => {ItalArt=}            0.005  0.5000000 0.5254861    10
## [7]  {ItalCook=ItalCook,                                                          
##       ItalArt=,                                                                   
##       Florence=Florence} => {YouthBks=}           0.005  0.6250000 0.8305648    10
## [8]  {RefBks=RefBks,                                                              
##       ArtBks=,                                                                    
##       Florence=Florence} => {GeogBks=GeogBks}     0.005  0.4545455 1.6469038    10
## [9]  {ChildBks=,                                                                  
##       YouthBks=YouthBks,                                                          
##       Florence=Florence} => {ArtBks=ArtBks}       0.005  0.5882353 2.4408103    10
## [10] {ChildBks=,                                                                  
##       YouthBks=YouthBks,                                                          
##       Florence=Florence} => {CookBks=CookBks}     0.005  0.5882353 1.3648151    10
## [11] {YouthBks=YouthBks,                                                          
##       ArtBks=,                                                                    
##       Florence=Florence} => {CookBks=CookBks}     0.005  0.7692308 1.7847582    10
## [12] {ChildBks=,                                                                  
##       YouthBks=YouthBks,                                                          
##       Florence=Florence} => {GeogBks=}            0.005  0.5882353 0.8124797    10
## [13] {YouthBks=YouthBks,                                                          
##       GeogBks=,                                                                   
##       Florence=Florence} => {ChildBks=}           0.005  0.5555556 0.9628346    10
## [14] {CookBks=,                                                                   
##       DoItYBks=DoItYBks,                                                          
##       Florence=Florence} => {GeogBks=GeogBks}     0.005  0.6250000 2.2644928    10
## [15] {CookBks=,                                                                   
##       DoItYBks=DoItYBks,                                                          
##       Florence=Florence} => {ChildBks=ChildBks}   0.005  0.6250000 1.4775414    10
## [16] {DoItYBks=DoItYBks,                                                          
##       ArtBks=,                                                                    
##       Florence=Florence} => {CookBks=CookBks}     0.005  0.7142857 1.6572754    10
## [17] {DoItYBks=DoItYBks,                                                          
##       ArtBks=,                                                                    
##       Florence=Florence} => {ItalCook=}           0.005  0.7142857 0.8057368    10
## [18] {ChildBks=,                                                                  
##       RefBks=RefBks,                                                              
##       ItalCook=ItalCook} => {CookBks=CookBks}     0.005  1.0000000 2.3201856    10
## [19] {ChildBks=,                                                                  
##       ArtBks=ArtBks,                                                              
##       ItalCook=ItalCook} => {YouthBks=YouthBks}   0.005  0.4545455 1.8365473    10
## [20] {ChildBks=,                                                                  
##       ArtBks=ArtBks,                                                              
##       ItalCook=ItalCook} => {GeogBks=}            0.005  0.4545455 0.6278252    10
plot(head(sort(Book_apriori),n=20), method="graph", control=list(cex=0.70))

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

plot(head(sort(Book_apriori),n=10), method="grouped", control=list(cex=0.2))
## Warning: Unknown control parameters: cex
## Available control parameters (with default values):
## main  =  Grouped Matrix for 10 Rules
## k     =  20
## rhs_max   =  10
## lhs_items     =  2
## aggr.fun  =  function (x, ...)  UseMethod("mean")
## col   =  c("#EE0000FF", "#EE0303FF", "#EE0606FF", "#EE0909FF", "#EE0C0CFF", "#EE0F0FFF", "#EE1212FF", "#EE1515FF", "#EE1818FF", "#EE1B1BFF", "#EE1E1EFF", "#EE2222FF", "#EE2525FF", "#EE2828FF", "#EE2B2BFF", "#EE2E2EFF", "#EE3131FF", "#EE3434FF", "#EE3737FF", "#EE3A3AFF", "#EE3D3DFF", "#EE4040FF", "#EE4444FF", "#EE4747FF", "#EE4A4AFF", "#EE4D4DFF", "#EE5050FF", "#EE5353FF", "#EE5656FF", "#EE5959FF", "#EE5C5CFF", "#EE5F5FFF", "#EE6262FF", "#EE6666FF", "#EE6969FF", "#EE6C6CFF", "#EE6F6FFF", "#EE7272FF", "#EE7575FF",  "#EE7878FF", "#EE7B7BFF", "#EE7E7EFF", "#EE8181FF", "#EE8484FF", "#EE8888FF", "#EE8B8BFF", "#EE8E8EFF", "#EE9191FF", "#EE9494FF", "#EE9797FF", "#EE9999FF", "#EE9B9BFF", "#EE9D9DFF", "#EE9F9FFF", "#EEA0A0FF", "#EEA2A2FF", "#EEA4A4FF", "#EEA5A5FF", "#EEA7A7FF", "#EEA9A9FF", "#EEABABFF", "#EEACACFF", "#EEAEAEFF", "#EEB0B0FF", "#EEB1B1FF", "#EEB3B3FF", "#EEB5B5FF", "#EEB7B7FF", "#EEB8B8FF", "#EEBABAFF", "#EEBCBCFF", "#EEBDBDFF", "#EEBFBFFF", "#EEC1C1FF", "#EEC3C3FF", "#EEC4C4FF", "#EEC6C6FF", "#EEC8C8FF",  "#EEC9C9FF", "#EECBCBFF", "#EECDCDFF", "#EECFCFFF", "#EED0D0FF", "#EED2D2FF", "#EED4D4FF", "#EED5D5FF", "#EED7D7FF", "#EED9D9FF", "#EEDBDBFF", "#EEDCDCFF", "#EEDEDEFF", "#EEE0E0FF", "#EEE1E1FF", "#EEE3E3FF", "#EEE5E5FF", "#EEE7E7FF", "#EEE8E8FF", "#EEEAEAFF", "#EEECECFF", "#EEEEEEFF")
## reverse   =  TRUE
## xlab  =  NULL
## ylab  =  NULL
## legend    =  Size: support  Color: lift
## spacing   =  -1
## panel.function    =  function (row, size, shading, spacing)  {     size[size == 0] <- NA     shading[is.na(shading)] <- 1     grid.circle(x = c(1:length(size)), y = row, r = size/2 * (1 - spacing), default.units = "native", gp = gpar(fill = shading, col = shading, alpha = 0.9)) }
## gp_main   =  list(cex = 1.2, fontface = "bold", font = c(bold = 2))
## gp_labels     =  list(cex = 0.8)
## gp_labs   =  list(cex = 1.2, fontface = "bold", font = c(bold = 2))
## gp_lines  =  list(col = "gray", lty = 3)
## newpage   =  TRUE
## max.shading   =  NA
## engine    =  default
## verbose   =  FALSE