############################################################################################## #Function to convert lift from data mining reports to Relative Risk and Odds Ratio #Description: calculate RR and OR from lift, support and confidence #Instruction: 1) import the set of rules with lift, support and confidence (e.g. as csv) # 2) run the function #by Khanh Vu, University of Alberta, 2018 ############################################################################################## lift_RR <- function(rules, support, confidence, lift){ k <- as.list(match.call()) c = which(names(rules) %in% c(k$support, k$confidence, k$lift)) support = eval(k$support, rules); confidence = eval(k$confidence, rules); lift = eval(k$lift, rules) prob_E = support / confidence #P(E) prob_O = confidence / lift #P(O) RR = (1 - prob_E)*lift / (1 - prob_E*lift) OR = (RR - confidence) / (1 - confidence) lift_O_negative = (1 - confidence) / (1 - prob_O) #Lift(O~|E) result <- data.frame(rules[-(c)], support, confidence, lift, RR, OR, prob_O, prob_E, lift_O_negative) return(result) } #Example #Create a set of rules with lift, support and confidence rules = c("15 121 127 132 143 -> 4", "15 127 143 -> 4", "15 76 -> 4", "15 139 -> 4", "15 -> 4", "118 119 -> 4") support = c(0.0196, 0.02, 0.0218, 0.0217, 0.0219, 0.0213) cf = c(0.115, 0.115, 0.113, 0.113, 0.112, 0.112) gamma = c(1.295, 1.287, 1.266, 1.266, 1.263, 1.256) Kingfisher = data.frame(rules, support, cf, gamma) #call the function example = lift_RR(rules = Kingfisher, support = support, confidence = cf, lift = gamma) head(example) #save result as .csv write.table(example, "example.csv", sep=",", row.name = F)