Integration with the arules package

Introduction

One of the goals in the design of this package is to be able to integrate with the arules package. This means that any one using the arules functionalities can export to and import from fcaR objects, more precisely, FormalContext and ImplicationSet objects.

library(arules)
#> Loading required package: Matrix
#> 
#> Attaching package: 'arules'
#> The following objects are masked from 'package:base':
#> 
#>     abbreviate, write
library(fcaR)

Datasets

For these examples, we are using two binary datasets, Mushroom (from the arules package) and planets (from fcaR).

data("Mushroom", package = "arules")

At the moment, in arules there is no support for fuzzy sets, so we must restrict ourselves to the binary case.

Let us create a FormalContext object for the planets dataset:

fc_planets <- FormalContext$new(planets)

Converting between formal contexts and transactions objects

We begin by converting between the objects which store the datasets.

Importing a transactions object

It suffices to initialize a FormalContext object with the transactions dataset:

fc <- FormalContext$new(Mushroom)
fc
#> Warning: Too many attributes, output will be truncated.
#> FormalContext with 8124 objects and 114 attributes.
#> Attributes' names are: Class=edible, Class=poisonous, CapShape=bell,
#>   CapShape=conical, CapShape=flat, CapShape=knobbed, ...
#> Matrix:
#>   Class=edible Class=poisonous CapShape=bell CapShape=conical CapShape=flat
#> 1            0               1             0                0             0
#> 2            1               0             0                0             0
#> 3            1               0             1                0             0
#> 4            0               1             0                0             0
#> 5            1               0             0                0             0
#> 6            1               0             0                0             0
#>   CapShape=knobbed CapShape=sunken
#> 1                0               0
#> 2                0               0
#> 3                0               0
#> 4                0               0
#> 5                0               0
#> 6                0               0

From this point, we can use all the functionalities in the fcaR package regarding formal contexts, concept lattices and implication sets.

Exporting a FormalContext

The to_transactions() function enables us to export a formal context to a format compatible with the arules package:

fc_planets$to_transactions()
#> transactions in sparse format with
#>  9 transactions (rows) and
#>  7 items (columns)

and use the functionality in that package.

Converting between implication sets and rules objects

Other point of integration between the two packages is the ability to import rules from the arules package, operate on them to compute closures, recommendations or to remove redundancies, or to export an implication set as a rules object.

Importing rules as an implication set

Let us suppose that we have extracted implications from the Mushroom dataset using the apriori() function:

mushroom_rules <- apriori(Mushroom, parameter = list(conf = 1))
#> Apriori
#> 
#> Parameter specification:
#>  confidence minval smax arem  aval originalSupport maxtime support minlen
#>           1    0.1    1 none FALSE            TRUE       5     0.1      1
#>  maxlen target  ext
#>      10  rules TRUE
#> 
#> Algorithmic control:
#>  filter tree heap memopt load sort verbose
#>     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
#> 
#> Absolute minimum support count: 812 
#> 
#> set item appearances ...[0 item(s)] done [0.00s].
#> set transactions ...[114 item(s), 8124 transaction(s)] done [0.01s].
#> sorting and recoding items ... [53 item(s)] done [0.00s].
#> creating transaction tree ... done [0.01s].
#> checking subsets of size 1 2 3 4 5 6 7 8 9 10
#> Warning in apriori(Mushroom, parameter = list(conf = 1)): Mining stopped (maxlen
#> reached). Only patterns up to a length of 10 returned!
#>  done [3.80s].
#> writing ... [1799427 rule(s)] done [0.35s].
#> creating S4 object  ... done [1.23s].

Once we have created the fc object storing the Mushroom dataset, we simply add the implications to it as:

fc$implications$add(mushroom_rules)

And we can use all the functionalities for the ImplicationSet class.

Exporting ImplicationSets to rules format

If we want to export the implications extracted for a binary formal context, we can use:

fc_planets$find_implications()
fc_planets$implications$to_arules(quality = TRUE)
#> set of 10 rules

Final considerations

An example of use may be to extract rules in the arules package by using apriori() or eclat(), then importing everything into fcaR as described above, and use the functionalities to simplify, remove redundancies, compute closures, etc., as needed, and then re-export back to arules.