-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmining-and-pruning-rules.Rmd
More file actions
91 lines (75 loc) · 2.42 KB
/
Copy pathmining-and-pruning-rules.Rmd
File metadata and controls
91 lines (75 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
title: "Mining and pruning association rules"
author: "Michael Hahsler"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Mining and pruning association rules}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(arules)
set.seed(1234)
```
Association rule mining can produce more rules than are practical to inspect.
An effective workflow constrains the search, filters and ranks the result, and
then removes rules that add no information.
```{r}
trans <- transactions(list(
T1 = c("bread", "butter", "milk"),
T2 = c("bread", "butter"),
T3 = c("bread", "milk"),
T4 = c("bread", "butter", "jam"),
T5 = c("bread", "butter", "milk"),
T6 = c("butter", "jam"),
T7 = c("bread", "milk", "cereal"),
T8 = c("bread", "butter", "jam")
))
```
## Constrain the search
Support, confidence, and rule length constrain the rule set while Apriori is
searching. The `appearance` argument can also restrict items to the left- or
right-hand side. Here, Apriori generates only rules that predict `butter` or
`milk`.
```{r}
rules <- apriori(
trans,
parameter = list(
support = 0.25, confidence = 0.6,
maxlen = 3
),
appearance = list(
rhs = c("butter", "milk"),
default = "lhs"
)
)
inspect(rules)
```
These constraints produce only `r length(rules)` rules. Constraining the search
also reduces its memory and computation requirements.
## Rank and filter
Filter by criteria appropriate for the task, then rank the remaining rules.
Keeping these criteria in the code makes the selection reproducible.
```{r}
selected <- subset(rules, lift > 1 & confidence >= 0.7)
ranked <- sort(selected, by = "lift", decreasing = TRUE)
inspect(ranked)
```
Many interest measures are available in addition to support, confidence, and
lift. The vignette
[Interest measures](interest-measures.html)
(`vignette("interest-measures", package = "arules")`) introduces the use
of additional interest measures.
## Remove redundant rules
A rule is redundant if a more general rule with the same consequent performs
at least as well according to the selected measure. Removing redundant rules
produces a more concise result.
```{r}
non_redundant <- rules[!is.redundant(rules)]
inspect(sort(non_redundant, by = "lift"))
```
The complementary subset contains the redundant rules that were removed.
```{r}
inspect(rules[is.redundant(rules)])
```