-
Notifications
You must be signed in to change notification settings - Fork 63
/
arules_Jack_Astors.Rmd
137 lines (95 loc) · 2.73 KB
/
arules_Jack_Astors.Rmd
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
title: "Association Rules"
output:
pdf_document: default
html_notebook: default
---
```{r}
library(arules)
data("Groceries")
inspect(Groceries[1:20,])
library(arulesViz)
```
# Load the data
```{r}
data("Groceries")
```
# Explore and Visualize
```{r}
str(Groceries)
dim(Groceries)
summary(Groceries)
```
```{r}
inspect(Groceries[1:20,])
```
```{r}
# Which items contain the word fish?
colnames(Groceries)[grep("fish", colnames(Groceries))]
# Which items contain the word wine?
colnames(Groceries)[grep("wine", colnames(Groceries))]
```
```{r}
itemFrequencyPlot(Groceries,
type="relative",
topN=15, # can be changed to the number of interest
horiz=FALSE,
col='steelblue3',
xlab='',
main='Item frequency, relative')
```
# Simple contingency table
Sometimes, just looking at a simple contingency table could shed some light.
```{r}
tbl <- crossTable(Groceries)
tbl[1:13,1:13]
```
How many people buy canned bear and bottled beer at the same time? And other products.
```{r}
tbl['canned beer','bottled beer']
tbl['beef', 'red/blush wine']
# What do people buy most often with their wine?
sort(tbl[,'red/blush wine'], decreasing = TRUE)
```
# Build the association rules
```{r}
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.1, target = "rules"))
summary(rules)
```
```{r}
# List the top 40 rules, ordered by lift
inspect(head(rules, n=40, by = "lift"))
```
```{r}
# Calculate another interestingness measure: conviction
quality(rules) <- cbind(quality(rules),
conviction = interestMeasure(rules, measure="conviction", transactions = Groceries))
```
```{r}
# List the top 40 rules, ordered by conviction
inspect(head(rules, n=40, by = "conviction"))
```
```{r}
subrules <- rules[quality(rules)$confidence > 0.8]
plot(subrules)
plot(subrules, method="matrix", measure="lift")
plot(subrules, method="matrix3D", measure="lift")
plot(subrules, method="graph", measure="lift")
```
## Example
```{r}
inspect(sort(subset(rules,
subset=rhs %in% 'bottled beer' & confidence > .7),
by = 'lift',
decreasing = T))
```
## Example
Both “whole milk” and “yogurt” must be present and rule’s confidence must be higher than .9
```{r}
inspect(subset(rules, subset=items %ain% c("whole milk","yogurt") & confidence >.90))
```
## Example
“Bread” must be present in lhs: any type of “bread” – “white bread”, “brown bread” – both qualify. “Whole milk” must be present in rhs “as is”. confidence of the rule must be higher than .9
```{r}
inspect(subset(rules, subset= lhs %pin% "bread" & rhs %in% "whole milk" & confidence > .9))
```