--- title: "Mining and pruning association rules" author: "Michael Hahsler" output: rmarkdown::html_vignette: toc: true vignette: > %\VignetteIndexEntry{Mining and pruning association rules} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include=FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") library(arules) set.seed(1234) ``` Association rule mining can produce more rules than are practical to inspect. An effective workflow constrains the search, filters and ranks the result, and then removes rules that add no information. ```{r} trans <- transactions(list( T1 = c("bread", "butter", "milk"), T2 = c("bread", "butter"), T3 = c("bread", "milk"), T4 = c("bread", "butter", "jam"), T5 = c("bread", "butter", "milk"), T6 = c("butter", "jam"), T7 = c("bread", "milk", "cereal"), T8 = c("bread", "butter", "jam") )) ``` ## Constrain the search Support, confidence, and rule length constrain the rule set while Apriori is searching. The `appearance` argument can also restrict items to the left- or right-hand side. Here, Apriori generates only rules that predict `butter` or `milk`. ```{r} rules <- apriori( trans, parameter = list( support = 0.25, confidence = 0.6, maxlen = 3 ), appearance = list( rhs = c("butter", "milk"), default = "lhs" ) ) inspect(rules) ``` These constraints produce only `r length(rules)` rules. Constraining the search also reduces its memory and computation requirements. ## Rank and filter Filter by criteria appropriate for the task, then rank the remaining rules. Keeping these criteria in the code makes the selection reproducible. ```{r} selected <- subset(rules, lift > 1 & confidence >= 0.7) ranked <- sort(selected, by = "lift", decreasing = TRUE) inspect(ranked) ``` Many interest measures are available in addition to support, confidence, and lift. The vignette [Interest measures](interest-measures.html) (`vignette("interest-measures", package = "arules")`) introduces the use of additional interest measures. ## Remove redundant rules A rule is redundant if a more general rule with the same consequent performs at least as well according to the selected measure. Removing redundant rules produces a more concise result. ```{r} non_redundant <- rules[!is.redundant(rules)] inspect(sort(non_redundant, by = "lift")) ``` The complementary subset contains the redundant rules that were removed. ```{r} inspect(rules[is.redundant(rules)]) ``` ## Other vignettes * [Getting started with arules](getting-started.html) * [Preparing transaction data](preparing-transaction-data.html) * [Interest measures](interest-measures.html) * [Item hierarchies](item-hierarchies.html)