-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathAssociation_Rule.R
More file actions
57 lines (48 loc) · 1.62 KB
/
Association_Rule.R
File metadata and controls
57 lines (48 loc) · 1.62 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
install.packages('seriation')
install.packages('arules')
install.packages('arulesViz')
# Load the libraries
library(arules)
library(arulesViz)
library(datasets)
# Load the data set
data(Groceries)
# Create an item frequency plot for the top 20 items
itemFrequencyPlot(Groceries,topN=20,type="absolute")
# Get the rules
?apriori
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8))
# Show the top 5 rules, but only 2 digits
options(digits=2)
inspect(rules[1:5])
summary(rules)
#sort rules
rules<-sort(rules, by="confidence", decreasing=TRUE)
inspect(rules[1:5])
# change to have maximum of 3
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8,maxlen=3))
inspect(rules[1:5])
summary(rules)
# Rules pruned
subset.matrix <- is.subset(rules, rules)
subset.matrix[lower.tri(subset.matrix, diag=T)] <- NA
redundant <- colSums(subset.matrix, na.rm=T) >= 1
rules.pruned <- rules[!redundant]
rules<-rules.pruned
summary(rules)
# What are customers likely to buy after buying whole milk?
rules<-apriori(data=Groceries, parameter=list(supp=0.001,conf = 0.08),
appearance = list(default="lhs",rhs="whole milk"),
control = list(verbose=F))
rules<-sort(rules, decreasing=TRUE,by="confidence")
inspect(rules[1:5])
# whole milk and its antecedent
rules<-apriori(data=, parameter=list(supp=0.001,conf = 0.15,minlen=2),
appearance = list(default="rhs",lhs="whole milk"),
control = list(verbose=F))
rules<-sort(rules, decreasing=TRUE,by="confidence")
inspect(rules[1:5])
# Graph
library(arulesViz)
plot(rules)
plot(rules,method="graph",interactive=TRUE,shading=NA)