# R Markdown
#Since the data has lots of additional columns we have work on data frame before applying any algorithm for assosaciton rules.
#Importing data with Read Excel
raw <- read_excel("Project.xlsx")

#Changing description and country columns from chr to factor
raw %>% mutate(Description = as.factor(Description))
## # A tibble: 541,909 x 8
##    InvoiceNo StockCode Description Quantity InvoiceDate         UnitPrice
##    <chr>     <chr>     <fct>          <dbl> <dttm>                  <dbl>
##  1 536365    85123A    WHITE HANG~        6 2010-12-01 08:26:00      2.55
##  2 536365    71053     WHITE META~        6 2010-12-01 08:26:00      3.39
##  3 536365    84406B    CREAM CUPI~        8 2010-12-01 08:26:00      2.75
##  4 536365    84029G    KNITTED UN~        6 2010-12-01 08:26:00      3.39
##  5 536365    84029E    RED WOOLLY~        6 2010-12-01 08:26:00      3.39
##  6 536365    22752     SET 7 BABU~        2 2010-12-01 08:26:00      7.65
##  7 536365    21730     GLASS STAR~        6 2010-12-01 08:26:00      4.25
##  8 536366    22633     HAND WARME~        6 2010-12-01 08:28:00      1.85
##  9 536366    22632     HAND WARME~        6 2010-12-01 08:28:00      1.85
## 10 536367    84879     ASSORTED C~       32 2010-12-01 08:34:00      1.69
## # ... with 541,899 more rows, and 2 more variables: CustomerID <dbl>,
## #   Country <chr>
raw %>% mutate(Country = as.factor(Country))
## # A tibble: 541,909 x 8
##    InvoiceNo StockCode Description Quantity InvoiceDate         UnitPrice
##    <chr>     <chr>     <chr>          <dbl> <dttm>                  <dbl>
##  1 536365    85123A    WHITE HANG~        6 2010-12-01 08:26:00      2.55
##  2 536365    71053     WHITE META~        6 2010-12-01 08:26:00      3.39
##  3 536365    84406B    CREAM CUPI~        8 2010-12-01 08:26:00      2.75
##  4 536365    84029G    KNITTED UN~        6 2010-12-01 08:26:00      3.39
##  5 536365    84029E    RED WOOLLY~        6 2010-12-01 08:26:00      3.39
##  6 536365    22752     SET 7 BABU~        2 2010-12-01 08:26:00      7.65
##  7 536365    21730     GLASS STAR~        6 2010-12-01 08:26:00      4.25
##  8 536366    22633     HAND WARME~        6 2010-12-01 08:28:00      1.85
##  9 536366    22632     HAND WARME~        6 2010-12-01 08:28:00      1.85
## 10 536367    84879     ASSORTED C~       32 2010-12-01 08:34:00      1.69
## # ... with 541,899 more rows, and 2 more variables: CustomerID <dbl>,
## #   Country <fct>
glimpse(raw)
## Observations: 541,909
## Variables: 9
## $ InvoiceNo   <chr> "536365", "536365", "536365", "536365", "536365", "5363...
## $ StockCode   <chr> "85123A", "71053", "84406B", "84029G", "84029E", "22752...
## $ Description <chr> "WHITE HANGING HEART T-LIGHT HOLDER", "WHITE METAL LANT...
## $ Quantity    <dbl> 6, 6, 8, 6, 6, 2, 6, 6, 6, 32, 6, 6, 8, 6, 6, 3, 2, 3, ...
## $ InvoiceDate <dttm> 2010-12-01 08:26:00, 2010-12-01 08:26:00, 2010-12-01 0...
## $ UnitPrice   <dbl> 2.55, 3.39, 2.75, 3.39, 3.39, 7.65, 4.25, 1.85, 1.85, 1...
## $ CustomerID  <dbl> 17850, 17850, 17850, 17850, 17850, 17850, 17850, 17850,...
## $ Country     <chr> "United Kingdom", "United Kingdom", "United Kingdom", "...
## $ Date        <date> 2010-12-01, 2010-12-01, 2010-12-01, 2010-12-01, 2010-1...
#Since the data.frame is single format, to apply and association rule groping must be done.
transactiondata <- ddply(raw,c("InvoiceNo","Date"),
                         function(df1)paste(df1$Description,
                                            collapse = ","))

#After Grouping, 2 variables which will not be usefull removed
#set column InvoiceNo of dataframe transactionData  
transactiondata$InvoiceNo <- NULL
#set column Date of dataframe transactionData
transactiondata$Date<- NULL
#Rename column to items
colnames(transactiondata) <- c("items")
# Now data is ready for applying any association rules
#Saving data frame as xlsx or csv format
write.xlsx(transactiondata, file = "basket.xlsx", sheetName="Sheet1", 
           col.names=TRUE, row.names=FALSE, append=FALSE)
summary(basket)
## transactions as itemMatrix in sparse format with
##  25901 rows (elements/itemsets/transactions) and
##  9029 columns (items) and a density of 0.001592033 
## 
## most frequent items:
## WHITE HANGING HEART T-LIGHT HOLDER           REGENCY CAKESTAND 3 TIER 
##                               1860                               1791 
##            JUMBO BAG RED RETROSPOT                      PARTY BUNTING 
##                               1613                               1356 
##      ASSORTED COLOUR BIRD ORNAMENT                            (Other) 
##                               1263                             364430 
## 
## element (itemset/transaction) length distribution:
## sizes
##    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 
## 1454 5350 1716 1189  934  879  770  704  684  669  602  631  538  524  533  549 
##   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31 
##  523  467  450  483  413  387  317  303  270  238  249  227  211  223  215  169 
##   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47 
##  161  139  145  133  115  115   91  109   96   88   93   89   66   68   63   65 
##   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63 
##   67   60   52   64   43   46   50   46   38   30   40   34   29   30   19   22 
##   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79 
##   25   23   29   25   22   18   20   18   14   17   19   10   13   13   11    9 
##   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95 
##   15   18   14    6    9   15   11   15   11    6    7   10   11    8    5    7 
##   96   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111 
##    4    7    7    4    6    6    4    5    6    1    3    5    7    4    7    4 
##  113  114  116  117  118  119  120  121  122  123  124  125  126  127  129  130 
##    7    1    4    5    4    1    2    5    3    3    1    4    3    2    1    1 
##  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146 
##    3    3    4    2    1    1    1    1    1    2    4    4    1    3    2    4 
##  147  148  150  151  153  154  155  156  157  158  159  160  163  164  167  168 
##    2    2    2    2    2    5    3    1    3    1    3    1    2    1    2    3 
##  169  170  171  172  173  174  175  179  180  181  183  184  186  187  193  196 
##    1    1    1    2    1    3    4    1    2    1    1    1    1    1    1    2 
##  197  201  202  204  208  213  215  226  227  230  232  236  238  241  249  250 
##    2    1    1    2    1    1    2    1    1    1    1    1    1    1    1    2 
##  256  257  260  261  263  265  266  270  272  285  305  314  320  339  348  365 
##    1    1    2    1    1    1    1    1    1    1    1    1    2    1    1    1 
##  375  398  400  411  460  514  530  587 
##    2    1    1    1    1    1    1    1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    1.00    7.00   14.37   19.00  587.00 
## 
## includes extended item information - examples:
##                    labels
## 1 *USB Office Mirror Ball
## 2                       ?
## 3         ? sold as sets?
#According to summary of the data White hanging holder is the most frequent item in the dataset.
install.packages("RColorBrewer")
## Installing package into 'C:/Users/bugra.duman/Documents/R/win-library/3.6'
## (as 'lib' is unspecified)
## package 'RColorBrewer' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\bugra.duman\AppData\Local\Temp\RtmpGU6Ibw\downloaded_packages
library(RColorBrewer)

#Showing a brief summary about frequent items into graph
itemFrequencyPlot(basket,topN=10,type="absolute",col=brewer.pal(8,'Pastel2'), main="Absolute Item Frequency Plot")

#Appliy Apriori to basket.
association.rules <- apriori(basket, parameter = list(supp=0.003, conf=0.09,maxlen=10))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.09    0.1    1 none FALSE            TRUE       5   0.003      1
##  maxlen target   ext
##      10  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 77 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[9029 item(s), 25901 transaction(s)] done [0.23s].
## sorting and recoding items ... [1356 item(s)] done [0.01s].
## creating transaction tree ... done [0.01s].
## checking subsets of size 1 2 3 4 5 6 done [0.07s].
## writing ... [9070 rule(s)] done [0.00s].
## creating S4 object  ... done [0.01s].
summary(association.rules)
## set of 9070 rules
## 
## rule length distribution (lhs + rhs):sizes
##    2    3    4    5    6 
## 3925 3300 1496  325   24 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   2.000   3.000   2.812   3.000   6.000 
## 
## summary of quality measures:
##     support           confidence           lift             count      
##  Min.   :0.003011   Min.   :0.09013   Min.   :  1.381   Min.   : 78.0  
##  1st Qu.:0.003320   1st Qu.:0.22161   1st Qu.:  8.714   1st Qu.: 86.0  
##  Median :0.003784   Median :0.43781   Median : 15.746   Median : 98.0  
##  Mean   :0.004419   Mean   :0.44799   Mean   : 23.501   Mean   :114.5  
##  3rd Qu.:0.004787   3rd Qu.:0.64179   3rd Qu.: 24.444   3rd Qu.:124.0  
##  Max.   :0.022123   Max.   :1.00000   Max.   :193.526   Max.   :573.0  
## 
## mining info:
##    data ntransactions support confidence
##  basket         25901   0.003       0.09
#We can see that most of the rules consist of 2 to 4 items.
#Let's sort the rules by confidence
sorted.rules <- sort(association.rules, by = "confidence", decreasing = FALSE)

inspect(sorted.rules[1:20])
##      lhs                                     rhs                                     support confidence      lift count
## [1]  {SPOTTY BUNTING}                     => {JAM MAKING SET PRINTED}            0.003243118 0.09012876  2.705011    84
## [2]  {LUNCH BAG RED RETROSPOT}            => {ASSORTED COLOUR BIRD ORNAMENT}     0.004324157 0.09032258  1.852292   112
## [3]  {NATURAL SLATE HEART CHALKBOARD}     => {ROSES REGENCY TEACUP AND SAUCER}   0.003358944 0.09043659  2.749294    87
## [4]  {NATURAL SLATE HEART CHALKBOARD}     => {GREEN REGENCY TEACUP AND SAUCER}   0.003358944 0.09043659  3.014669    87
## [5]  {LUNCH BAG SPACEBOY DESIGN}          => {JUMBO BAG VINTAGE DOILY}           0.003088684 0.09049774  4.430968    80
## [6]  {LUNCH BAG SPACEBOY DESIGN}          => {SET OF 4 PANTRY JELLY MOULDS}      0.003088684 0.09049774  2.862005    80
## [7]  {JUMBO BAG RED RETROSPOT}            => {CHARLOTTE BAG PINK POLKADOT}       0.005636848 0.09051457  4.688836   146
## [8]  {PACK OF 72 RETROSPOT CAKE CASES}    => {STRAWBERRY CERAMIC TRINKET BOX}    0.003629204 0.09055877  4.367901    94
## [9]  {PACK OF 72 RETROSPOT CAKE CASES}    => {RED TOADSTOOL LED NIGHT LIGHT}     0.003629204 0.09055877  4.016374    94
## [10] {JUMBO BAG PINK POLKADOT}            => {RED TOADSTOOL LED NIGHT LIGHT}     0.003243118 0.09071274  4.023203    84
## [11] {JUMBO BAG PINK POLKADOT}            => {RECIPE BOX PANTRY YELLOW DESIGN}   0.003243118 0.09071274  2.783828    84
## [12] {JUMBO SHOPPER VINTAGE RED PAISLEY}  => {SET OF 4 PANTRY JELLY MOULDS}      0.003011467 0.09080326  2.871667    78
## [13] {WHITE HANGING HEART T-LIGHT HOLDER} => {LUNCH BAG CARS BLUE}               0.006524845 0.09086022  2.541437   169
## [14] {LUNCH BAG  BLACK SKULL.}            => {ROUND SNACK BOXES SET OF 4 FRUITS} 0.003436161 0.09100204  5.146384    89
## [15] {JAM MAKING SET WITH JARS}           => {JAM JAR WITH GREEN LID}            0.003281727 0.09100642 10.763276    85
## [16] {JAM MAKING SET WITH JARS}           => {SET 2 PANTRY DESIGN TEA TOWELS}    0.003281727 0.09100642  7.805157    85
## [17] {HEART OF WICKER SMALL}              => {ZINC METAL HEART DECORATION}       0.003243118 0.09130435  5.603967    84
## [18] {LUNCH BAG PINK POLKADOT}            => {TOY TIDY PINK POLKADOT}            0.003088684 0.09142857  7.241870    80
## [19] {LUNCH BAG PINK POLKADOT}            => {PINK VINTAGE PAISLEY PICNIC BAG}   0.003088684 0.09142857  8.000309    80
## [20] {PARTY BUNTING}                      => {SET OF 4 PANTRY JELLY MOULDS}      0.004787460 0.09144543  2.891976   124
#Checking rules if significant or not
head(is.significant(association.rules,basket))
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
#Checking the rules if there is any redundant one
any(is.redundant(association.rules))
## [1] TRUE
first20 <- sorted.rules[1:20]


inspect(first20[is.redundant(first20)==FALSE])
##      lhs                                     rhs                                     support confidence      lift count
## [1]  {SPOTTY BUNTING}                     => {JAM MAKING SET PRINTED}            0.003243118 0.09012876  2.705011    84
## [2]  {LUNCH BAG RED RETROSPOT}            => {ASSORTED COLOUR BIRD ORNAMENT}     0.004324157 0.09032258  1.852292   112
## [3]  {NATURAL SLATE HEART CHALKBOARD}     => {ROSES REGENCY TEACUP AND SAUCER}   0.003358944 0.09043659  2.749294    87
## [4]  {NATURAL SLATE HEART CHALKBOARD}     => {GREEN REGENCY TEACUP AND SAUCER}   0.003358944 0.09043659  3.014669    87
## [5]  {LUNCH BAG SPACEBOY DESIGN}          => {JUMBO BAG VINTAGE DOILY}           0.003088684 0.09049774  4.430968    80
## [6]  {LUNCH BAG SPACEBOY DESIGN}          => {SET OF 4 PANTRY JELLY MOULDS}      0.003088684 0.09049774  2.862005    80
## [7]  {JUMBO BAG RED RETROSPOT}            => {CHARLOTTE BAG PINK POLKADOT}       0.005636848 0.09051457  4.688836   146
## [8]  {PACK OF 72 RETROSPOT CAKE CASES}    => {STRAWBERRY CERAMIC TRINKET BOX}    0.003629204 0.09055877  4.367901    94
## [9]  {PACK OF 72 RETROSPOT CAKE CASES}    => {RED TOADSTOOL LED NIGHT LIGHT}     0.003629204 0.09055877  4.016374    94
## [10] {JUMBO BAG PINK POLKADOT}            => {RED TOADSTOOL LED NIGHT LIGHT}     0.003243118 0.09071274  4.023203    84
## [11] {JUMBO BAG PINK POLKADOT}            => {RECIPE BOX PANTRY YELLOW DESIGN}   0.003243118 0.09071274  2.783828    84
## [12] {JUMBO SHOPPER VINTAGE RED PAISLEY}  => {SET OF 4 PANTRY JELLY MOULDS}      0.003011467 0.09080326  2.871667    78
## [13] {WHITE HANGING HEART T-LIGHT HOLDER} => {LUNCH BAG CARS BLUE}               0.006524845 0.09086022  2.541437   169
## [14] {LUNCH BAG  BLACK SKULL.}            => {ROUND SNACK BOXES SET OF 4 FRUITS} 0.003436161 0.09100204  5.146384    89
## [15] {JAM MAKING SET WITH JARS}           => {JAM JAR WITH GREEN LID}            0.003281727 0.09100642 10.763276    85
## [16] {JAM MAKING SET WITH JARS}           => {SET 2 PANTRY DESIGN TEA TOWELS}    0.003281727 0.09100642  7.805157    85
## [17] {HEART OF WICKER SMALL}              => {ZINC METAL HEART DECORATION}       0.003243118 0.09130435  5.603967    84
## [18] {LUNCH BAG PINK POLKADOT}            => {TOY TIDY PINK POLKADOT}            0.003088684 0.09142857  7.241870    80
## [19] {LUNCH BAG PINK POLKADOT}            => {PINK VINTAGE PAISLEY PICNIC BAG}   0.003088684 0.09142857  8.000309    80
## [20] {PARTY BUNTING}                      => {SET OF 4 PANTRY JELLY MOULDS}      0.004787460 0.09144543  2.891976   124
# to find transactions which support each of a set of associations 
# and to return this information as a transaction ID list.
supportingTransactions(sorted.rules, basket)
## tidLists in sparse format with
##  9070 items/itemsets (rows) and
##  25901 transactions (columns)
inspect(supportingTransactions(sorted.rules[1:20], basket))
##    items                                                                
## 1  {SPOTTY BUNTING} => {JAM MAKING SET PRINTED}                         
## 2  {LUNCH BAG RED RETROSPOT} => {ASSORTED COLOUR BIRD ORNAMENT}         
## 3  {NATURAL SLATE HEART CHALKBOARD} => {ROSES REGENCY TEACUP AND SAUCER}
## 4  {NATURAL SLATE HEART CHALKBOARD} => {GREEN REGENCY TEACUP AND SAUCER}
## 5  {LUNCH BAG SPACEBOY DESIGN} => {JUMBO BAG VINTAGE DOILY}             
## 6  {LUNCH BAG SPACEBOY DESIGN} => {SET OF 4 PANTRY JELLY MOULDS}        
## 7  {JUMBO BAG RED RETROSPOT} => {CHARLOTTE BAG PINK POLKADOT}           
## 8  {PACK OF 72 RETROSPOT CAKE CASES} => {STRAWBERRY CERAMIC TRINKET BOX}
## 9  {PACK OF 72 RETROSPOT CAKE CASES} => {RED TOADSTOOL LED NIGHT LIGHT} 
## 10 {JUMBO BAG PINK POLKADOT} => {RED TOADSTOOL LED NIGHT LIGHT}         
## 11 {JUMBO BAG PINK POLKADOT} => {RECIPE BOX PANTRY YELLOW DESIGN}       
## 12 {JUMBO SHOPPER VINTAGE RED PAISLEY} => {SET OF 4 PANTRY JELLY MOULDS}
## 13 {WHITE HANGING HEART T-LIGHT HOLDER} => {LUNCH BAG CARS BLUE}        
## 14 {LUNCH BAG  BLACK SKULL.} => {ROUND SNACK BOXES SET OF 4 FRUITS}     
## 15 {JAM MAKING SET WITH JARS} => {JAM JAR WITH GREEN LID}               
## 16 {JAM MAKING SET WITH JARS} => {SET 2 PANTRY DESIGN TEA TOWELS}       
## 17 {HEART OF WICKER SMALL} => {ZINC METAL HEART DECORATION}             
## 18 {LUNCH BAG PINK POLKADOT} => {TOY TIDY PINK POLKADOT}                
## 19 {LUNCH BAG PINK POLKADOT} => {PINK VINTAGE PAISLEY PICNIC BAG}       
## 20 {PARTY BUNTING} => {SET OF 4 PANTRY JELLY MOULDS}                    
##    transactionIDs                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
## 1  {7804,7924,8063,8162,8200,8214,8426,8546,8596,8624,8703,8829,9110,9272,9305,9340,9433,9767,9774,9836,10105,10152,10286,10399,10401,10441,10445,10453,10516,10559,10648,10724,10795,10895,11068,11107,11151,11360,11409,11571,11581,11910,11932,12015,12285,12603,12651,12858,12865,12986,13044,13090,13191,13213,13241,13264,13393,13568,13604,14104,14313,14321,14408,14505,14647,14943,14967,15147,15194,15456,15645,15747,15777,16105,16373,17274,18557,19023,20197,20213,20539,20633,21996,24835}                                                                                                                                                                                                                                                                                                                                                                                                                                                           
## 2  {422,791,815,977,1009,1300,1721,1819,1827,2750,2864,3077,3282,3440,3465,3538,3578,3590,4431,4588,4710,4721,5054,5139,5691,5717,5811,5884,6022,6322,6364,6885,7058,7102,7205,7322,7507,7628,7955,7989,8108,8198,8383,8628,8732,8789,8943,8972,9056,9180,9726,9909,10089,10123,10367,10586,10660,10721,10910,10957,11076,11487,11580,11716,11894,11920,11970,12030,12285,12374,12633,12825,12826,13177,13278,13388,13919,13955,14118,14408,14439,14549,14943,14962,15034,15194,15319,15384,15435,15798,15865,16118,16133,16135,16625,17062,19055,19133,19153,19337,19832,19975,20025,20591,21054,21180,21598,21602,21648,23637,23980,25034}                                                                                                                                                                                                                                                                                                                       
## 3  {297,484,664,933,1237,1251,1567,1636,1852,2190,2261,2681,2783,2854,2873,2956,3081,4155,4316,4331,4601,4818,5143,5155,5351,5384,5488,5968,6287,6406,6831,7053,7202,7302,7932,8167,9092,9886,9906,10252,10875,11028,11211,11771,12533,12603,12608,12651,13235,13790,13807,13919,13984,13987,14280,14288,14616,14653,14655,14670,14859,15615,16033,16589,16601,16643,16711,17085,17296,17565,17935,18130,18716,18864,19282,19485,20097,20787,20917,21648,21820,21897,21899,21915,21996,22034,22440}                                                                                                                                                                                                                                                                                                                                                                                                                                                                
## 4  {294,297,484,664,933,1536,1567,1636,2121,2190,2261,2638,2715,2783,2873,2956,3081,3308,3848,4155,4316,4331,4601,5143,5155,5351,5384,5563,5826,5968,6287,6406,6634,6831,7053,7077,7302,7773,7932,8167,8562,8580,9092,9379,9431,9906,10258,10308,10383,11005,11028,11206,11211,12438,12533,12603,12608,12651,13235,13880,13919,13984,13987,14288,14313,15027,15615,16033,16601,16643,16711,17085,17296,17565,18130,18864,19282,19453,20097,20539,20787,20917,21648,21897,21899,21915,22034}                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
## 5  {12757,12815,12985,13050,13083,13169,13197,13204,13242,13301,13352,13388,13419,13455,13466,13528,13579,13655,13662,13678,13701,13751,13786,13931,13944,13955,14049,14144,14154,14271,14523,14616,14676,14811,14826,14836,14850,14874,15232,15270,15383,15394,15420,15591,15642,15713,15723,15865,16105,16286,16312,16467,16599,16625,16949,17062,17149,17261,17352,17354,17887,18092,18131,18506,18559,18565,18830,18889,18905,18911,19100,19345,19475,19727,20043,20072,20591,21054,21101,21492}                                                                                                                                                                                                                                                                                                                                                                                                                                                               
## 6  {3685,3962,4211,4265,4329,4433,4445,4699,4731,4903,5001,5011,5086,5230,5280,5307,5442,5482,5489,5515,5557,5569,5572,5832,5928,6189,6302,6370,6418,6620,6665,6757,6898,7019,7102,7414,7593,7929,7945,8141,8527,8672,8719,9305,9440,9517,9963,10272,10419,10572,11195,11281,11400,11646,11806,12049,12269,12638,12651,12773,12854,13164,13242,13419,13701,13786,13808,13931,14193,14641,14811,16022,17380,18889,19332,20132,20917,21054,21996,24979}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
## 7  {412,514,1029,1045,1184,1835,1844,2013,2177,2239,2421,2594,2951,2962,3016,3181,3239,3567,3621,3684,3816,4068,4276,4407,4433,4434,4567,4896,5037,5224,5225,5329,5442,5515,5717,5875,5928,6124,6322,6539,6651,6727,7118,7170,7255,7686,7773,7811,8043,8565,8610,8765,9009,9259,9282,9392,9405,9438,9641,9759,9868,9870,9966,10229,10375,10398,10419,10464,10516,10600,10879,10970,11004,11068,11199,11244,11589,11601,11678,11780,11793,12013,12084,12164,12186,12521,12815,12879,13067,13138,13261,13419,13522,13528,13680,13790,14246,14309,14333,14781,14787,14849,14945,14967,15006,15014,15031,15054,15125,15383,15756,15910,15915,16021,16086,16286,16326,16686,16982,17085,17142,17149,17624,17721,18622,19100,19266,19301,19345,19554,19598,19681,19825,19975,20025,20072,20254,20408,20569,20715,20859,21054,21820,21996,24706,25058}                                                                                                                    
## 8  {331,473,662,956,1028,1078,1232,1486,1617,1827,2495,2556,2856,2862,3077,3141,3247,3376,3486,3611,3743,3800,3856,3957,3989,4016,4304,4407,4433,4462,4570,4578,4784,5118,5165,5280,5740,5898,5960,5987,6452,6564,6613,6713,6799,7034,7074,7287,7363,8115,8124,8300,8443,8580,8740,8948,9015,9047,9098,9192,9547,9638,9733,9898,10118,10190,10243,10258,10323,10659,11023,11364,11433,11780,11830,12450,12603,12630,12826,13560,13985,14438,14812,14967,15064,15384,16365,16601,16678,16690,16904,17055,17808,24173}                                                                                                                                                                                                                                                                                                                                                                                                                                               
## 9  {473,956,1305,1579,1611,1628,1886,2116,2204,2343,2461,2504,2681,3124,3141,3247,3308,3363,3376,3586,3611,3856,3950,3989,4316,4407,4424,4531,4681,4763,4928,5086,5151,5175,5402,5542,5549,5928,6076,6171,6287,6829,7171,7252,7379,7522,7524,7533,7628,8230,8580,8997,9192,9774,10138,10229,10444,11004,12582,12757,12826,13208,13790,13810,13985,14673,14805,14991,15384,15996,16365,16550,16758,16846,16862,17131,17578,19000,19226,19337,19424,19451,19598,19716,20201,20359,20361,20896,20910,20917,21054,21331,21648,21897}                                                                                                                                                                                                                                                                                                                                                                                                                                   
## 10 {896,1871,2186,2188,2189,2453,2503,2550,2570,2573,2594,2859,3586,3684,5006,5547,5549,5826,6076,6232,6623,6624,6631,6634,6829,7027,7028,7032,7118,7171,7228,7252,7522,7628,7849,7851,7855,7859,7860,7865,8230,8310,8314,9192,10229,10314,10600,11004,11068,11194,11350,11621,11625,11971,12186,12533,12582,12625,12858,12985,13547,13810,13828,14288,14653,14859,14972,15291,15559,16033,16505,16760,17217,17555,17752,18130,18159,18716,19466,20097,20539,20676,20917,21054}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
## 11 {297,412,439,484,934,936,1310,1635,1827,1835,1874,2550,2570,3016,3586,3621,4702,4794,5421,5442,5515,5545,5687,5826,6076,6124,6322,6624,7327,7507,7634,7665,8230,8706,8719,9379,9514,9612,9868,9906,10783,11004,11752,11894,12015,12490,12569,12582,12858,13066,13067,13931,14098,14265,14313,14530,14653,14773,14811,14859,14967,15057,15086,15559,16033,16312,16505,17142,17217,17233,17555,18130,18159,18716,18864,19266,19282,20097,20321,20539,20917,21054,21915,21996}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
## 12 {3685,3747,4044,4433,4592,4838,5079,5288,5307,5630,5777,5895,6070,6123,6287,6370,6634,7327,7633,7682,7773,7811,7915,8086,8250,8527,8765,9771,10328,10660,10882,10883,10885,10916,12080,12185,12373,12490,12533,12582,12630,12638,12651,12682,13066,13437,13502,13547,13701,13785,13786,13828,13931,14193,14288,14457,14653,14811,15615,15670,16407,17085,17217,17752,18130,18786,18835,18889,19453,20097,20149,20794,20917,21308,21648,21897,21996,22054}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
## 13 {399,456,484,541,1026,1045,1232,1257,1264,1267,1647,1665,1801,1819,1827,2095,2209,2404,2471,3087,3308,3316,3661,3924,4142,4213,4263,4327,4407,4425,4584,4588,4678,4691,4993,5175,5409,5442,5451,5756,5960,6022,6322,6357,6492,6712,6744,6777,6784,6830,6904,7122,7170,7507,7512,7531,7759,7844,8227,8444,8609,8703,8765,8804,8833,8914,8930,9036,9040,9047,9056,9093,9140,9175,9181,9217,9296,9305,9344,9375,9456,9514,9715,10043,10419,10436,10464,10498,10508,10707,11151,11250,11395,11396,11400,11450,11505,11590,11601,11646,11651,11768,12269,12365,12414,12438,12588,12693,12788,12806,12820,12825,12854,12907,12920,12969,13067,13124,13164,13466,13483,13495,13528,13759,13774,13811,13963,13984,13995,14154,14567,14605,14616,14678,14787,14901,15372,15488,15777,15868,15911,16393,16452,16690,17039,17664,17692,18134,18478,18506,18559,18611,18745,18748,19089,19166,19268,19453,19598,20073,20156,20212,20321,20362,20723,20788,21054,21648,21897}
## 14 {44,606,828,1184,1244,1264,1647,1824,1825,1886,2073,2326,2404,2588,2636,2779,3141,3247,3376,3611,3627,3856,3962,5053,5515,6229,6649,6865,7034,7287,7760,8124,8300,8316,9015,9032,9040,9098,9292,9305,9547,9733,9774,9898,10118,10315,10323,10419,10436,10451,10659,11109,11199,11400,11460,11465,11589,11783,12365,12379,12422,12611,12815,12955,13269,13528,13609,13623,13645,13984,14154,14216,14273,14294,14406,14787,14901,14972,15591,15748,15932,16326,16564,18142,18965,20321,20605,20656,22445}                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 15 {25,71,129,448,458,511,547,1004,1171,1452,1480,1486,1838,1859,2056,2204,2252,2429,2495,2749,3124,3187,3215,3308,3475,3729,3816,3976,4092,4154,4324,4407,4653,4690,4758,4941,5140,5183,5384,5717,5928,6076,6104,6285,6598,6915,7088,7400,7915,7978,8106,8188,8546,8624,9433,9719,9819,10586,11005,11759,11910,12365,12999,13399,13492,13810,13983,14101,14104,14459,15952,16267,16475,17209,17441,18134,18786,19000,19716,20539,21648,21897,21996,24591,25822}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
## 16 {3178,3187,3475,3663,3729,3792,3804,3957,4168,4206,4306,4336,4499,4643,4657,4690,4825,4937,5001,5250,5275,5286,5294,5406,5488,5616,5630,5878,5914,5928,6104,6176,6729,6915,6991,7061,7400,7875,7876,7915,8106,8368,8624,9152,10536,10724,11111,11360,11520,11585,11650,11698,11752,11759,12814,12858,13239,13241,13563,14260,14321,14647,14944,15194,15924,16267,16316,16475,16678,16977,17085,17274,17764,18085,18786,19000,19716,19988,20425,20940,21648,21788,21897,21996,24160}                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
## 17 {176,422,427,484,498,508,608,615,1045,1068,1142,1294,1305,1311,1536,1601,1617,2075,2243,2482,2584,2854,3103,3271,3308,3568,4003,4010,4019,4325,4588,8279,8624,8758,9049,9186,9200,9203,9214,9319,9374,9427,10258,11290,11575,11723,11768,11858,11984,12680,12716,12981,13441,13810,13963,14290,14351,14485,14506,14524,14567,14989,15038,15265,15907,16044,16114,16532,16939,17233,17402,17425,17455,18049,18060,18417,18523,18537,18651,18659,18663,18693,19320,20260}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 18 {1045,2442,2681,2750,2879,3077,3350,3751,4034,4087,4213,4903,5003,5006,5307,5442,5875,6302,6554,7015,7224,7713,8145,8213,8527,8706,8765,9192,9273,9305,9392,9615,9641,9906,9963,9966,10229,10365,10405,10584,10656,10855,11126,11464,11479,11565,11745,11907,12121,12144,12185,12625,12929,12960,13433,13786,13808,13984,14154,14221,14273,14600,14992,15086,15713,16086,16505,16830,17999,18002,18622,18748,18767,18864,20072,20109,20176,21415,21899,21996}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
## 19 {737,1935,2429,3350,4089,4444,4496,5409,5511,6839,6933,7122,7208,7371,7403,7507,7756,7759,7805,7930,8049,8141,8350,8611,8765,8833,9180,9192,9292,9305,9314,9359,9369,9444,9463,9534,9546,9689,9781,9948,10043,10089,10405,10425,10584,10853,10855,10928,11030,11111,11162,11395,11400,11506,11701,11787,11793,11798,11886,12080,12414,12533,12633,12885,12969,13197,13455,13466,13505,13516,13528,13538,13786,13808,14313,14838,15693,17624,19287,19938}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
## 20 {3697,3804,4026,4087,4316,4336,4360,4367,4407,4433,4445,4699,4700,4791,4838,5011,5019,5086,5293,5557,5615,5630,5928,5968,6074,6124,6287,6302,6344,6370,6558,6799,7126,7129,7171,7431,7437,7472,7499,7628,7633,7662,7692,7766,7773,7811,7833,7853,7928,7945,8051,8305,8330,8515,8584,8624,8672,8719,8810,8816,8893,9310,9313,9463,9658,9868,10098,10132,10145,10440,10581,10660,10974,11094,11218,11364,11460,11525,11546,11638,11643,11698,11806,12015,12197,12582,12651,12773,12854,13001,13164,13213,13242,13279,13393,13406,13520,13664,13786,13808,14104,14142,14246,14313,14403,14527,14566,14799,15615,15930,16022,16316,16373,17579,19571,19573,19632,20119,20197,20213,20516,21897,24977,24979}
#VISULATION
pilot <- association.rules[1:1000]
plot(pilot, measure=c("support","lift"), shading="confidence")
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

# To interpret better filtering with confidence
subrules<-association.rules[quality(association.rules)$confidence>0.4]

#Putting rules into interactive plot where we can see the rule itself.
plotly_arules(subrules)
## Warning: 'plotly_arules' is deprecated.
## Use 'plot' instead.
## See help("Deprecated")
## Warning: plot: Too many rules supplied. Only plotting the best 1000 rules using
## measure lift (change parameter max if needed)
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.
top10subrules <- head(subrules, n = 20, by = "confidence")

plot(top10subrules, method = "graph",  engine = "htmlwidget")