1

I am using the apriori algorithm in R to mine for association rules. I can inspect the resulting rules based on confidence, lift, and support, but was hoping to evaluate the conviction values for each rule. Does anyone know how to do this in R?

E.Mcc
  • 21
  • 3

1 Answers1

2

Have a look at ?arules::interestMeasure. For instance, following the Wikipedia example, you can do:

df <- read.table(header=T, text="ID     milk    bread   butter  beer    diapers
1   T   T   F   F   F
2   F   F   T   F   F
3   F   F   F   T   T
4   T   T   T   F   F
5   F   T   F   F   F")
library(arules)
trans <- as(df[, -1], "transactions")
rules <- apriori(trans, list(supp = 0.01, conf = 0.01, minlen = 2))
cbind(as(rules, "data.frame"), conviction=interestMeasure(rules, "conviction", trans))
#                       rules support confidence      lift conviction
# 1       {beer} => {diapers}     0.2  1.0000000 5.0000000         NA
# 2       {diapers} => {beer}     0.2  1.0000000 5.0000000         NA
# 3        {butter} => {milk}     0.2  0.5000000 1.2500000        1.2
# 4        {milk} => {butter}     0.2  0.5000000 1.2500000        1.2
# 5       {butter} => {bread}     0.2  0.5000000 0.8333333        0.8
# 6       {bread} => {butter}     0.2  0.3333333 0.8333333        0.9
# 7         {milk} => {bread}     0.4  1.0000000 1.6666667         NA
# 8         {bread} => {milk}     0.4  0.6666667 1.6666667        1.8
# 9  {milk,butter} => {bread}     0.2  1.0000000 1.6666667         NA
# 10 {bread,butter} => {milk}     0.2  1.0000000 2.5000000         NA
# 11 {milk,bread} => {butter}     0.2  0.5000000 1.2500000        1.2
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Hi! So I think this worked. However, when I ran it on my association rules the resulting table did not include column headings (rules, support, confidence, lift, conviction) and did not include an actual description of the rule. Could this be an issue with how I ran the apriori algorithm? – E.Mcc Oct 17 '16 at 22:32
  • I dunno. Please read [how to provide minimal reproducible examples in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Then edit & improve your original question accordingly and add such an example. This prevents guesswork. And it's best practice anyway - hover over the R tag under your question and read the little note. – lukeA Oct 17 '16 at 22:36
  • 1
    I managed to solve the formatting issue. Thank you, this was exactly what I needed!! – E.Mcc Oct 17 '16 at 22:37