
Last chance! 50% off unlimited learning
Sale ends in
The rules
class represents a set of rules.
Objects are the result of calling the function apriori
.
Objects can also be created by calls of the form
new("rules", ...)
or by using the constructor function
rules(lhs, rhs, itemLabels, quality = data.frame())
.
lhs
and rhs
need to be a list
describing the items (using labels or item ids) and itemLabels
needs to be a vector of all possible item
labels (character) or a transactions object to copy the item coding (see itemCoding
for details).
signature(from = "rules", to = "data.frame")
;
represents the set of rules as a data.frame
signature(x = "rules")
;
returns a collection of the itemsets which generated the rules, one
itemset for each rule. Note that the collection can be a multiset and
contain duplicated
elements. Use unique
to remove duplicates and obtain a
proper set. Technically this method produces the same as the
result of method items()
,
but wrapped into an
'>itemsets
object with support information.
signature(object = "rules")
;
returns the whole item information data frame including item
labels
signature(object = "rules")
;
returns the item labels used to encode the rules
signature(x = "rules")
;
returns for each rule the union of the items in the
lhs and rhs (i.e., the itemsets
which generated the rule) as an
'>itemMatrix
signature(object = "rules")
;
returns the item labels as a character vector.
The index for each label is the column index of the item in the
binary matrix.
signature(object = "rules")
;
returns labels for the rules ("lhs => rhs") as a
character
vector. The representation can be customized using
the additional parameter ruleSep
and parameters for label
defined in '>itemMatrix
signature(x = "rules")
;
returns the '>itemMatrix
representing the left-hand-side of the rules (antecedents)
signature(x = "rules")
;
replaces the '>itemMatrix
representing the left-hand-side of the rules (antecedents)
signature(x = "rules")
; number of all possible items in the
binary matrix representation of the object.
signature(x = "rules")
;
returns the '>itemMatrix
representing the right-hand-side of the rules (consequents)
signature(x = "rules")
;
replaces the '>itemMatrix
representing the right-hand-side of the rules (consequents)
signature(object = "rules")
Rules are usually created by calling an association rule mining algorithm like apriori
.
Rules store the LHS and the RHS separately as objects of class itemMatrix
.
To create rules manually, the itemMatrix for the LHS and the RHS
of the rules can be created using itemCoding
.
Note the two matrices
need to have the itemLabels (i.e., columns of the sparse matrix) in the same order.
An example is in the Example section below.
Mined rule sets typically contain several interest measures accessible with the quality
method.
Additional measures can be calculated via interestMeasure
.
associations-class
,
[-methods
,
apriori
,
c
,
duplicated
,
inspect
,
itemCoding
length
,
match
,
sets
,
size
,
subset
,
# NOT RUN {
data("Adult")
## Mine rules
rules <- apriori(Adult, parameter = list(support = 0.3))
rules
## Select a subset of rules using partial matching on the items
## in the right-hand-side and a quality measure
rules.sub <- subset(rules, subset = rhs %pin% "sex" & lift > 1.3)
## Display the top 3 support rules
inspect(head(rules.sub, n = 3, by = "support"))
## Display the first 3 rules
inspect(rules.sub[1:3])
## Get labels for the first 3 rules
labels(rules.sub[1:3])
labels(rules.sub[1:3], itemSep = " + ", setStart = "", setEnd="",
ruleSep = " ---> ")
## Manually create rules using the item coding in Adult and calculate some interest measures
twoRules <- rules(
lhs = list(
c("age=Young", "relationship=Unmarried"),
c("age=Old")
),
rhs = list(
c("income=small"),
c("income=large")
),
itemLabels = Adult
)
quality(twoRules) <- interestMeasure(twoRules,
measure = c("support", "confidence", "lift"), transactions = Adult)
inspect(twoRules)
# }
Run the code above in your browser using DataLab