R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Association Rules

Apriori Algorithm

Grocery dataset

# install.packages("arulesViz")
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
data("Groceries")
data()
class(Groceries)
## [1] "transactions"
## attr(,"package")
## [1] "arules"
itemFrequencyPlot(Groceries,topN=25)

rules <- apriori(Groceries,parameter=list(supp=0.002,confidence=0.50,minlen=2,maxlen=3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.002      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 19 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
## sorting and recoding items ... [147 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(Groceries, parameter = list(supp = 0.002, confidence =
## 0.5, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.01s].
## writing ... [582 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(sort(rules),n=10))
##      lhs                     rhs                   support confidence     lift count
## [1]  {other vegetables,                                                             
##       yogurt}             => {whole milk}       0.02226741  0.5128806 2.007235   219
## [2]  {tropical fruit,                                                               
##       yogurt}             => {whole milk}       0.01514997  0.5173611 2.024770   149
## [3]  {other vegetables,                                                             
##       whipped/sour cream} => {whole milk}       0.01464159  0.5070423 1.984385   144
## [4]  {root vegetables,                                                              
##       yogurt}             => {whole milk}       0.01453991  0.5629921 2.203354   143
## [5]  {pip fruit,                                                                    
##       other vegetables}   => {whole milk}       0.01352313  0.5175097 2.025351   133
## [6]  {root vegetables,                                                              
##       yogurt}             => {other vegetables} 0.01291307  0.5000000 2.584078   127
## [7]  {root vegetables,                                                              
##       rolls/buns}         => {whole milk}       0.01270971  0.5230126 2.046888   125
## [8]  {other vegetables,                                                             
##       domestic eggs}      => {whole milk}       0.01230300  0.5525114 2.162336   121
## [9]  {tropical fruit,                                                               
##       root vegetables}    => {other vegetables} 0.01230300  0.5845411 3.020999   121
## [10] {root vegetables,                                                              
##       rolls/buns}         => {other vegetables} 0.01220132  0.5020921 2.594890   120
plot(head(sort(rules,by="lift"),n=5),method = "graph",control = list(cex=0.92))

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

plot(head(sort(rules,by="lift"),n=5),method = "grouped")