library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Loading required package: grid
library(tidyverse)
## ── Attaching packages ─────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.0.0     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.1
## ✔ tidyr   0.8.1     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::expand() masks Matrix::expand()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::recode() masks arules::recode()
library(knitr)
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
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 object is masked from 'package:lubridate':
## 
##     here
## 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(dplyr)

tr <- suppressWarnings(read.transactions('market_basket_transactions.csv',
                        format = 'basket', sep=','))

association.rules <- apriori(tr, parameter = list(supp=0.001, conf=0.8,maxlen=10))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.8    0.1    1 none FALSE            TRUE       5   0.001      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: 131 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[51773 item(s), 131210 transaction(s)] done [0.52s].
## sorting and recoding items ... [1874 item(s)] done [0.02s].
## creating transaction tree ... done [0.07s].
## checking subsets of size 1 2 3 4 done [0.06s].
## writing ... [259 rule(s)] done [0.01s].
## creating S4 object  ... done [0.04s].
association.rules <- sort(association.rules, by = c('confidence','lift'))

shorter.association.rules <- apriori(tr, parameter = list(supp=0.001, conf=0.8,maxlen=4))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.8    0.1    1 none FALSE            TRUE       5   0.001      1
##  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: 131 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[51773 item(s), 131210 transaction(s)] done [0.54s].
## sorting and recoding items ... [1874 item(s)] done [0.03s].
## creating transaction tree ... done [0.07s].
## checking subsets of size 1 2 3 4
## Warning in apriori(tr, parameter = list(supp = 0.001, conf = 0.8, maxlen
## = 4)): Mining stopped (maxlen reached). Only patterns up to a length of 4
## returned!
##  done [0.06s].
## writing ... [259 rule(s)] done [0.01s].
## creating S4 object  ... done [0.05s].
subset.rules <- which(colSums(is.subset(association.rules, association.rules)) > 1)

subset.association.rules. <- association.rules[-subset.rules]

subRules<-association.rules[quality(association.rules)$confidence>0.4]

plot(subRules, jitter = 0)

plot(subRules,method="two-key plot")
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

plotly_arules(subRules, jitter = 0)
## Warning: 'plotly_arules' is deprecated.
## Use 'plot' instead.
## See help("Deprecated")
top10subRules <- head(subRules, n = 20, by = c("confidence","lift"))
plot(top10subRules, method = "graph",  engine = "htmlwidget")
subrules2 <- head(subRules, n=20, by = "lift")
plot (subrules2, method = "paracoord")