knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE
)

Overview

R package for getting inspected rules as data.frame.

How to use

Just inspectDF() instead of arules::inspect() after arules::apriori().

require(dplyr)
require(arules)
require(inspectDF)

data(Groceries)
params <- list(confidence=0.001, support=0.001, maxlen=7, minlen=2)
glo.apriori <- apriori(Groceries, parameter = params, control = list(verbose=FALSE))
print(glo.apriori)

glo.inspectDF  <- inspectDF(glo.apriori)
glo.inspectDF %>% str(vec.len = 3)

InspectDF also provides a plot.igraph wrapper utility.

set.seed(0)
glo.inspectDF %>% 
  arrange(support, confidence) %>%
  head(60) %>% 
  plotRuleGraph()

Detail

Installation

You can install the inspectDF package from GitHub.

install.packages("devtools") # if you have not installed "devtools" package
devtools::install_github("katokohaku/inspectDF")

The source code for inspectDF package is available at

Motivation

Usually, we arules::inspect() to enumerate rules after arules::apriori().

Of cource, we could get data.frame object as side effect of cat() on the last line in inspect(). However, it can't be done quietly (Always show all on consol).

!!Don't run following codes with many rules!!

data(Groceries)
params <- list(confidence=0.001, support=0.001, maxlen=7, minlen=2)
glo.apriori <- apriori(Groceries, parameter = params)
print(glo.apriori)
glo.inspect  <- glo.apriori %>% head(10) %>% inspect()
glo.inspect %>% str

It is noisy when especially inspect() a lot of rules or calling in other function. Therefore, this must be done invisibly.

Use case

Arules package privides several utilities such as sort(), subset() and etc. But if rules were provided as data.frame, we can explore them as tidy data. InspectDF has a good affinity with tidy schemes, such as dplyr::arrange() or dplyr::filter() because this returns only data.frame.

For example, rules with specific item(s) can be extracted using stringr::str_detect()

require(stringr)
rules.lhs  <- glo.inspectDF %>% 
  filter(str_detect(LHS, pattern = "yogurt|sausage")) %>%
  arrange(confidence, lift) %>%
  filter(n.lhs > 1) %>% 
  head(5)

rules.lhs

By default, rule strings are split by separater ",". But, items sometimes contain separater characters e.g. IUPAC of DHA. In such case, user can change rule-separater freely sep = string.

glo.apriori %>% 
  inspectDF(sep = "###") %>% 
  filter(n.lhs >3) %>% 
  select(2:3) %>% 
  head()

Similar to original plot.rules with igraph in arules package, each rule size represents support value. This size can be adjusted by adujust.support.size in plot functions.

require(stringr)
rules.lhs  <- glo.inspectDF %>% 
  filter(str_detect(RHS, pattern = "yogurt")) %>%
  arrange(confidence, lift) %>%
  filter(n.lhs > 1) %>% 
  head(15)

s=5
set.seed(s)
rules.lhs %>% plotRuleGraph(label = "default")
set.seed(0)
rules.lhs %>% plotRuleGraph(label = "adjusted rule size", 
                            adujust.support.size = 4000)
require(stringr)
rules.lhs  <- glo.inspectDF %>% 
  filter(str_detect(RHS, pattern = "yogurt")) %>%
  arrange(confidence, lift) %>%
  filter(n.lhs > 1) %>% 
  head(15)

rules.lhs

edges.lhs <- rules.lhs %>% inspectDF::toEdges()
edges.lhs


katokohaku/inspectDF documentation built on May 23, 2019, 8:51 a.m.