Search code examples
rassociationsapriori

Calculating conviction values for association rules in R?


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?


Solution

  • 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