library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0     ✓ purrr   0.3.3
## ✓ tibble  3.0.0     ✓ dplyr   0.8.5
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl)
library(knitr)
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:dplyr':
## 
##     intersect, setdiff, union
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Attaching package: 'arules'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Loading required package: grid
## Registered S3 method overwritten by 'seriation':
##   method         from 
##   reorder.hclust gclus
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following object is masked from 'package:purrr':
## 
##     compact
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(DT)
library(RColorBrewer)
grocery<-read.csv("grocery.csv")

colnames(grocery)<- "items"
rownames(grocery)<-NULL

write.csv(grocery,file="g.csv",quote = FALSE,row.names = FALSE)

grocery<-read.transactions("g.csv",format="basket",sep=",")

itemFrequencyPlot(grocery, topN=20, type='absolute',col=c(brewer.pal(8,"Set1")))

rules<- apriori(grocery,parameter = list(supp=0.01,conf=0.6))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.6    0.1    1 none FALSE            TRUE       5    0.01      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: 0 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[12 item(s), 20 transaction(s)] done [0.00s].
## sorting and recoding items ... [12 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [72 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules<-sort(rules,by="confidence",decreasing = TRUE)

summary(rules)
## set of 72 rules
## 
## rule length distribution (lhs + rhs):sizes
##  1  2  3  4 
##  1 12 40 19 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   3.000   3.069   4.000   4.000 
## 
## summary of quality measures:
##     support          confidence         lift            count       
##  Min.   :0.05000   Min.   :0.600   Min.   : 1.000   Min.   : 1.000  
##  1st Qu.:0.05000   1st Qu.:0.950   1st Qu.: 1.667   1st Qu.: 1.000  
##  Median :0.07500   Median :1.000   Median : 2.857   Median : 1.500  
##  Mean   :0.09028   Mean   :0.919   Mean   : 3.105   Mean   : 1.806  
##  3rd Qu.:0.10000   3rd Qu.:1.000   3rd Qu.: 3.500   3rd Qu.: 2.000  
##  Max.   :0.60000   Max.   :1.000   Max.   :10.000   Max.   :12.000  
## 
## mining info:
##     data ntransactions support confidence
##  grocery            20    0.01        0.6
inspect(rules[1:20])
##      lhs                   rhs          support confidence lift      count
## [1]  {JAM}              => {MAGGI}      0.10    1           4.000000 2    
## [2]  {JAM}              => {BREAD}      0.10    1           1.666667 2    
## [3]  {COCK}             => {COFFEE}     0.15    1           2.500000 3    
## [4]  {JAM,MILK}         => {MAGGI}      0.05    1           4.000000 1    
## [5]  {MAGGI,MILK}       => {JAM}        0.05    1          10.000000 1    
## [6]  {JAM,MILK}         => {BREAD}      0.05    1           1.666667 1    
## [7]  {JAM,TEA}          => {MAGGI}      0.05    1           4.000000 1    
## [8]  {JAM,MAGGI}        => {BREAD}      0.10    1           1.666667 2    
## [9]  {BREAD,JAM}        => {MAGGI}      0.10    1           4.000000 2    
## [10] {JAM,TEA}          => {BREAD}      0.05    1           1.666667 1    
## [11] {BISCUIT,COCK}     => {CORNFLAKES} 0.10    1           3.333333 2    
## [12] {COCK,CORNFLAKES}  => {BISCUIT}    0.10    1           3.333333 2    
## [13] {BISCUIT,COCK}     => {COFFEE}     0.10    1           2.500000 2    
## [14] {BISCUIT,COFFEE}   => {COCK}       0.10    1           6.666667 2    
## [15] {COCK,CORNFLAKES}  => {COFFEE}     0.10    1           2.500000 2    
## [16] {BREAD,COCK}       => {COFFEE}     0.05    1           2.500000 1    
## [17] {BOURNVITA,COFFEE} => {SUGER}      0.05    1           3.333333 1    
## [18] {BOURNVITA,TEA}    => {BREAD}      0.10    1           1.666667 2    
## [19] {MAGGI,MILK}       => {BREAD}      0.05    1           1.666667 1    
## [20] {BISCUIT,MILK}     => {CORNFLAKES} 0.05    1           3.333333 1
topRules <- rules[1:10]
plot(topRules)
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

plot(topRules, method="graph")