-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaffinityAnalysis.R
More file actions
111 lines (91 loc) · 3.77 KB
/
Copy pathaffinityAnalysis.R
File metadata and controls
111 lines (91 loc) · 3.77 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# R code to do some affinity analysis on ClickStream data
# import libraries
library(arules, warn.conflicts=FALSE)
library(arulesViz, warn.conflicts=FALSE)
library(Rgraphviz, warn.conflicts=FALSE)
#------------------------------- function space ------------------------------#
###:: Do basic APRIORI analysis ::###
aprioriAnalysis <- function(fname){
# read the transaction file in, then do the rules
trans <- read.transactions(fname, format="basket", sep=",")
supp <- c(0.01, 0.05, 0.10)
conf <- c(0.30, 0.50, 0.70)
for(S in supp){
for(C in conf){
rules <- apriori(trans, parameter=list(support=S, confidence=C, minlen=2))
rules <- sort(rules, by="confidence")
pname <- paste("itemFrequencyApriori", toString(S), toString(C), ".png", sep="")
cat("Saving rules to ",pname)
png(filename=pname, width=800, height=800)
itemFrequencyPlot(trans, support=S, ylim=c(0,1))
dev.off()
}
}
}
###:: Do basic ECLAT analysis ::###
eclatAnalysis <- function(fname){
trans <- read.transactions(fname, format="basket", sep=",")
supp <- c(0.01, 0.05, 0.10)
conf <- c(0.30, 0.50, 0.70)
for(S in supp){
cat(" ==> Using supp=",S,"\n")
rules <- eclat(trans, parameter=list(support=S, minlen=2, maxlen=12))
rules <- sort(rules, by="support")
pname <- paste("itemFrequencyEclat", toString(S), toString(C), ".png", sep="")
plot(rules, method="paracoord", measure="lift",
control=list(main="Parallel Coordinate plot", reorder=T))
dev.off()
}
}
###:: Do FP-growth analysis ::###
fpgrowth <- function(fname, transactions, parameters=list(support=0.60, confidence=0.80)){
# get the frequent itemsets (outsources work to dude's code)
freqItems <- getFrequentItems(fname, transactions, parameters$support*100,
parameters$confidence*100)
# get the rules from the frequent items
rules <- ruleInduction(freqItems, transactions=transactions, confidence=parameters$confidence)
return(rules)
}
###:: get the tree ::###
getFrequentItems <- function(fname, transactions, support, confidence){
ofile = "fpgrowth-out.dat"
# call the fpgrowth function
cat("calling fpgrowth code....\n")
fcall <- paste("fpgrowth -s",
support, " -c", confidence, ' -m1 -v" " ', fname, " ", ofile, sep = "")
cat(fcall,"\n")
system(fcall)
cat("...done\n\n")
# import the data back into R
columnNumber <- max(count.fields(ofile))
itemsets <- read.table(ofile, fill=T, col.names=1:columnNumber, colClasses="factor")
# mark those to be removed and remove the empty cells
toRemove <- apply(itemsets, 1, function(x) length(which(x != "")) > 1)
itemsets <- apply(itemsets, 1, function(x) x[x != ""]);
# format for processing
itemsets <- as(itemsets, "itemMatrix")
itemsets <- recode(itemsets, itemLabels(transactions))
itemsets <- new("itemsets", items=itemsets)
return(itemsets)
}
###:: invert the rules ::###
ruleInversion <- function(rule){
return(new("rules", lhs=rhs(rule), rhs=lhs(rule)))
}
###:: call the rules set based on input file ::###
runFPGrowth <- function(fname){
trans <- read.transactions(fname, format="basket", sep=",")
rules <- fpgrowth(fname, trans, parameters=list(support=0.02, confidence=0.6, measures=F))
rules <- sort(rules, by="support")
#~ x11()
#~ plot(rules, method='graph', control=list(type='itemsets', engine='graphviz', cex=1.6))
#~ Sys.sleep(15)
subrules <- sort(rules, by='support')[4:18]
inspect(subrules)
#~ x11()
#~ plot(subrules, method='graph', control=list(type='itemsets', engine='graphviz', cex=1.6))
#~ Sys.sleep(15)
}
#------------------------------- end functions ------------------------------#
fname <- "TransactionData.csv"
runFPGrowth(fname)