-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy patharules.Rmd
225 lines (169 loc) · 4.81 KB
/
arules.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: "Association Rules"
output:
pdf_document: default
html_notebook: default
---
```{r}
library(arules)
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}
# First, just plot them all:
# Note that the windows() call makes a new window pop up, which is what we want for interactivity.
windows(); plot(rules, interactive = T)
subrules <- rules[quality(rules)$confidence > 0.3]
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))
```
# Making up some transaction data
For use in class, I wanted to make up some data to describe different situations.
Looking for a rule with high support, high conf, and high lift. E.g.:
- Support of {A} is 70%
- Support of {B} is 50%
- Support of {A,B} is 50%
- Confidence of {A}->{B} is 100%
- Lift is therefore 2.0
Looking for a rule with high support, high conf, and low lift. I.e.:
- Support of {A} is 100%
- Support of {B} is 50%
- Support of {A,B} is 50%
- Confidence of {A}->{B} is 50%
- Lift is therefore 1.0
```{r}
a_list <- list(
c("milk","bread","carrot"),
c("milk","bread"),
c("milk","bread","meat"),
c("milk","diamonds"),
c("milk","bread","meat","diamonds"),
c("milk", "meat"),
c("milk"),
c("bread"),
c("bread", "meat"),
c("milk","bread", "diamonds"),
c("milk","bread", "carrot"),
c("milk","bread"),
c("diamonds", "meat"),
c("diamonds", "meat"),
c("diamonds", "meat"),
c("diamonds", "meat"),
c("diamonds", "meat"),
c("diamonds", "meat"),
c("diamonds", "meat"),
c("diamonds", "meat")
)
## set transaction names
names(a_list) <- paste("Tr",c(1:5), sep = "")
## coerce into transactions
trans1 <- as(a_list, "transactions")
## analyze transactions
summary(trans1)
#image(trans1)
rules <- apriori(trans1, parameter = list(supp = 0.001, conf = 0.001, target = "rules"))
```
```{r}
inspect(sort(subset(rules,
subset=lhs %in% 'bread' ),
by = 'lift',
decreasing = T))
```
Let's look at milk-> bread. Expect high, high, high
```{r}
inspect(sort(subset(rules,
subset=lhs %in% 'milk' & rhs %in% 'bread'),
by = 'lift',
decreasing = T))
```
```{r}
inspect(sort(subset(rules,
subset=lhs %in% 'diamonds' & confidence > .0),
by = 'lift',
decreasing = T))
```
```{r}
inspect(head(rules, n=42, by = "support"))
```
```{r}
inspect(head(rules, n=42, by = "confidence"))
```
```{r}
inspect(head(rules, n=42, by = "lift"))
```