-
Notifications
You must be signed in to change notification settings - Fork 6
/
README.Rmd
209 lines (139 loc) · 7.85 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# fcaR: Tools for Formal Concept Analysis
<!-- badges: start -->
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![CRAN status](https://www.r-pkg.org/badges/version/fcaR)](https://cran.r-project.org/package=fcaR)
[![codecov](https://codecov.io/gh/neuroimaginador/fcaR/branch/master/graph/badge.svg?token=8ujvQdrzUI)](https://app.codecov.io/gh/neuroimaginador/fcaR)
[![R build status](https://github.com/neuroimaginador/fcaR/workflows/R-CMD-check/badge.svg)](https://github.com/neuroimaginador/fcaR/actions)
[![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/fcaR)](https://cran.r-project.org/package=fcaR)
<!-- badges: end -->
The aim of this package is to provide tools to perform fuzzy formal concept analysis (FCA) from within R.
It provides functions to load and save a Formal Context, extract its concept lattice and implications.
In addition, one can use the implications to compute semantic closures of fuzzy sets and, thus, build recommendation systems.
## Details
The fcaR package provides data structures which allow the user to work seamlessly with formal contexts and sets of implications. More explicitly, three main classes are implemented, using the \code{R6} object-oriented-programming paradigm in R:
- FormalContext encapsulates the definition of a formal context (G, M, I), being G the set of objects, M the set of attributes and I the (fuzzy) relationship matrix, and provides methods to operate on the context using FCA tools.
- ImplicationSet represents a set of implications over a specific formal context.
- ConceptLattice represents the set of concepts and their relationships, including methods to operate on the lattice.
Two additional helper classes are implemented:
- Set is a class solely used for visualization purposes, since it encapsulates in sparse format a (fuzzy) set.
- Concept encapsulates internally both extent and intent of a formal concept as Set.
Since fcaR is an extension of the data model in the arules package, most of the methods and classes implemented interoperates with the main S4 classes in arules (transactions and rules).
## Installation
This package is available at CRAN, so its stable version can be easily installed using:
```
install.packages("fcaR")
```
The development version of this package can be installed with
```
remotes::install_github("neuroimaginador/fcaR", build_vignettes = TRUE)
```
or
```
remotes::install_github("Malaga-FCA-group/fcaR", build_vignettes = TRUE)
```
## Example of Use
Let us start with a fuzzy dataset (stored in a matrix I) as follows:
```{r results="asis", echo = FALSE}
library(fcaR)
objects <- paste0("O", 1:6)
n_objects <- length(objects)
attributes <- paste0("P", 1:6)
n_attributes <- length(attributes)
I <- matrix(data = c(0, 1, 0.5, 0, 0, 0.5,
1, 1, 0.5, 0, 0, 0,
0.5, 1, 0, 0, 1, 0,
0.5, 0, 0, 1, 0.5, 0,
1, 0, 0, 0.5, 0, 0,
0, 0, 1, 0, 0, 0),
nrow = n_objects,
byrow = FALSE)
colnames(I) <- attributes
rownames(I) <- objects
knitr::kable(I, format = "html", booktabs = TRUE)
```
Here, a value $x$ in the intersection of a row and a column indicates that the object of the corresponding row possesses the attribute in the column in a degree of at least $x$ (if $x = 0$, the attribute is absent in the object, and if $x = 1$, the attribute is fully present in the object).
We can build a FormalContext object:
```{r}
fc <- FormalContext$new(I)
print(fc)
```
With a single function, we can compute the set of concepts:
```{r}
# Compute all concepts
fc$find_concepts()
# The first concept
fc$concepts$sub(1)
# And plot the concept lattice
fc$concepts$plot()
```
We can also extract implications from the formal context:
```{r}
# Extract implications
fc$find_implications()
# Which implications have been extracted
fc$implications
```
Some fundamental functionalities on the concept lattice associated to the formal context have been implemented:
- Computing a sublattice.
- Calculating the subconcepts and superconcepts of a given concept.
- Finding the join- and meet- irreducible elements, which allows to reduce the context and find the _standard context_.
Also, one can compute the support of both implications and concepts:
```{r}
fc$implications$support()
fc$concepts$support()
```
In this package, we have implemented a logic to manage implications. This so-called Simplification Logic allows us to simplify the extracted rules by removing redundancies, as well as computing the closure of a given fuzzy attribute set.
```{r}
# Reduce the number of implications using two simple
# rules. The algorithm applies the specified rules
# in batches, if the number of rules is high.
fc$implications$apply_rules(rules = c("composition",
"generalization"))
# Reduced set of implications
fc$implications
```
All these functions work natively with fuzzy and with binary datasets.
For more details on the methods implemented and further examples, see the vignettes in this package.
## Changelog
With respect to the CRAN version, the development version has the following changes.
# fcaR 1.2.2
Enhancements:
* Added more unit tests.
* Minor changes to the plotting of formal contexts.
* Now the `fc$scale()` function admits a new argument `bg` (default: FALSE) which, if set to TRUE, avoids computing the background knowledge of the scales.
# fcaR 1.2.1
Enhancements:
* Other logics have been implemented. Now, we can use `fc$use_logic()` to select one of the `available_logics()`.
* Improved export to LaTeX.
Bugfixes:
* Some rounding errors might induce errors in the computations. These has been fixed.
### fcaR 1.2.0
Bugfixes:
* Fixes required by the new version of Matrix and the new use of HTML Tidy in R 4.2.
### fcaR 1.1.1
Enhancements:
* The user can control the number of decimal digits when exporting to LaTeX or when printing formal contexts, concept lattices and implications. Just use fcaR_options(decimal_places = n), where n is the number of desired decimal digits.
New functionality:
* Now the package uses the _settings_ package to manage several options. Currently, the only option is the number of decimal digits to use when printing or exporting to LaTeX.
Bugfixes:
* Fixed exporting to latex with special characters such as $, _, etc.
## References
Guigues J, Duquenne V (1986). “Familles minimales d'implications informatives résultant d'un tableau de données binaires.” _Mathématiques et Sciences humaines_, *95*, 5-18.
Ganter B, Wille R (1999). _Formal concept analysis : mathematical foundations_. Springer. ISBN 3540627715.
Cordero P, Enciso M, Mora Á, Pérez de Guzman I (2002). “SLFD Logic: Elimination of Data Redundancy in Knowledge Representation.” _Advances in Artificial Intelligence - IBERAMIA 2002_, *2527*, 141-150. doi: 10.1007/3-540-36131-6_15 (URL: https://doi.org/10.1007/3-540-36131-6_15).
Belohlavek R (2002). “Algorithms for fuzzy concept lattices.” In _Proc. Fourth Int. Conf. on Recent Advances in Soft Computing_. Nottingham, United Kingdom, 200-205.
Hahsler M, Grun B, Hornik K (2005). “arules - a computational environment for mining association rules and frequent item sets.” _J Stat Softw_, *14*, 1-25.
Mora A, Cordero P, Enciso M, Fortes I, Aguilera G (2012). “Closure via functional dependence simplification.” _International Journal of Computer Mathematics_, *89*(4), 510-526.
Belohlavek R, Cordero P, Enciso M, Mora Á, Vychodil V (2016). “Automated prover for attribute dependencies in data with grades.” _International Journal of Approximate Reasoning_, *70*, 51-67.