This class implements the data structure and methods for formal contexts.
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.
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.
Public fields
IThe table of the formal context as a matrix.
attributesThe attributes of the formal context.
objectsThe objects of the formal context.
grades_setThe set of degrees (in [0, 1]) the whole set of attributes can take.
expanded_grades_setThe set of degrees (in [0, 1]) each attribute can take.
conceptsThe concept lattice associated to the formal context as a
ConceptLattice.implicationsA set of implications on the formal context as an
ImplicationSet.
Methods
Method new()
Creator for the Formal Context class
Usage
FormalContext$new(I, filename, remove_const = FALSE)Arguments
I(numeric matrix) The table of the formal context.
filename(character) Path of a file to import.
remove_const(logical) If
TRUE, remove constant columns. The default isFALSE.
Details
Columns of I should be named, since they are the names of the attributes of the formal context.
If no I is used, the resulting FormalContext will be empty and not usable unless for loading a previously saved one. In this case, one can provide a filename to import. Only RDS, CSV and CXT files are currently supported.
If the file is not present, the fcarepository.org is looked for coincidences. If so, the corresponding context is loaded.
Method scale()
Scale the context
Details
The types of scaling are implemented in a registry,
so that scalingRegistry$get_entries() returns
all types.
In the dots argument, the user can supply the value for bg (logical), which, if set to TRUE, indicates to compute background knowledge as implications on the scales; if FALSE, no implications will be computed on the scales.
Examples
filename <- system.file("contexts", "aromatic.csv", package = "fcaR")
fc <- FormalContext$new(filename)
fc$scale("nitro", "ordinal", comparison = `>=`, values = 1:3)
fc$scale("OS", "nominal", c("O", "S"))
fc$scale(attributes = "ring", type = "nominal")Method get_scales()
Scales applied to the formal context
Usage
FormalContext$get_scales(attributes = names(private$scales))Returns
The scales that have been applied to the specified attributes
of the formal context. If no attributes are passed,
then all applied scales are returned.
Examples
filename <- system.file("contexts", "aromatic.csv", package = "fcaR")
fc <- FormalContext$new(filename)
fc$scale("nitro", "ordinal", comparison = `>=`, values = 1:3)
fc$scale("OS", "nominal", c("O", "S"))
fc$scale(attributes = "ring", type = "nominal")
fc$get_scales()Method background_knowledge()
Background knowledge of a scaled formal context
Examples
filename <- system.file("contexts", "aromatic.csv", package = "fcaR")
fc <- FormalContext$new(filename)
fc$scale("nitro", "ordinal", comparison = `>=`, values = 1:3)
fc$scale("OS", "nominal", c("O", "S"))
fc$scale(attributes = "ring", type = "nominal")
fc$background_knowledge()Method clarify()
Clarify a formal context
Method reduce()
Reduce a formal context
Method find_concepts()
Use Ganter Algorithm to compute concepts
Method find_implications()
Use modified Ganter algorithm to compute both concepts and implications
Method factorize()
Factorize the formal context using Boolean/Fuzzy Matrix Factorization algorithms.
Method to_transactions()
Convert the formal context to object of class transactions from the arules package
Method save()
Save a FormalContext to RDS or CXT format
Usage
FormalContext$save(filename = tempfile(fileext = ".rds"))Method load()
Load a FormalContext from a file
Method dim()
Dimensions of the formal context
Method print()
Prints the formal context
Method to_latex()
Write the context in LaTeX format
Arguments
table(logical) If
TRUE, surrounds everything between\begin{table}and\end{table}.label(character) The label for the table environment.
caption(character) The caption of the table.
fraction(character) If
none, no fractions are produced. Otherwise, if it isfrac,dfracorsfrac, decimal numbers are represented as fractions with the corresponding LaTeX typesetting.
Method incidence()
Incidence matrix of the formal context
Examples
fc <- FormalContext$new(planets)
fc$incidence()Method subcontext()
Subcontext of the formal context
Arguments
objects(character array) Name of the objects to keep.
attributes(character array) Names of the attributes to keep.
Details
A warning will be issued if any of the names is not present in the list of objects or attributes of the formal context.
If objects or attributes is empty, then it is
assumed to represent the whole set of objects or attributes
of the original formal context.
Returns
Another FormalContext that is a subcontext
of the original one, with only the objects and attributes
selected.
Examples
fc <- FormalContext$new(planets)
fc$subcontext(attributes = c("moon", "no_moon"))Method [()
Subcontext of the formal context
Arguments
objects(character array) Name of the objects to keep.
attributes(character array) Names of the attributes to keep.
Details
A warning will be issued if any of the names is not present in the list of objects or attributes of the formal context.
If objects or attributes is empty, then it is
assumed to represent the whole set of objects or attributes
of the original formal context.
Returns
Another FormalContext that is a subcontext
of the original one, with only the objects and attributes
selected.
Examples
fc <- FormalContext$new(planets)
fc[, c("moon", "no_moon")]Method plot()
Plot the formal context table
Arguments
to_latex(logical) If
TRUE, export the plot as atikzpictureenvironment that can be included in aLaTeXfile....Other parameters to be passed to the
tikzDevicethat renders the lattice inLaTeX, or for the figure caption. SeeDetails.
Details
Particular parameters that control the size of the tikz output are: width, height (both in inches), and pointsize (in points), that should be set to the font size used in the documentclass header in the LaTeX file where the code is to be inserted.
If a caption is provided, the whole tikz picture will be wrapped by a figure environment and the caption set.
Examples
# Build and print the formal context
fc_planets <- FormalContext$new(planets)
print(fc_planets)
#> FormalContext with 9 objects and 7 attributes.
#> small medium large near far moon no_moon
#> Mercury X X X
#> Venus X X X
#> Earth X X X
#> Mars X X X
#> Jupiter X X X
#> Saturn X X X
#> Uranus X X X
#> Neptune X X X
#> Pluto X X X
# Define a set of attributes
S <- Set$new(attributes = fc_planets$attributes)
S$assign(moon = 1, large = 1)
# Compute the closure of S
Sc <- fc_planets$closure(S)
# Is Sc a closed set?
fc_planets$is_closed(Sc)
#> [1] TRUE
# Clarify and reduce the formal context
fc2 <- fc_planets$reduce(TRUE)
# Find implications
fc_planets$find_implications()
# Read a formal context from CSV
filename <- system.file("contexts", "airlines.csv", package = "fcaR")
fc <- FormalContext$new(filename)
# Read a formal context from a CXT file
filename <- system.file("contexts", "lives_in_water.cxt", package = "fcaR")
fc <- FormalContext$new(filename)
## ------------------------------------------------
## Method `FormalContext$scale`
## ------------------------------------------------
filename <- system.file("contexts", "aromatic.csv", package = "fcaR")
fc <- FormalContext$new(filename)
fc$scale("nitro", "ordinal", comparison = `>=`, values = 1:3)
fc$scale("OS", "nominal", c("O", "S"))
fc$scale(attributes = "ring", type = "nominal")
## ------------------------------------------------
## Method `FormalContext$get_scales`
## ------------------------------------------------
filename <- system.file("contexts", "aromatic.csv", package = "fcaR")
fc <- FormalContext$new(filename)
fc$scale("nitro", "ordinal", comparison = `>=`, values = 1:3)
fc$scale("OS", "nominal", c("O", "S"))
fc$scale(attributes = "ring", type = "nominal")
fc$get_scales()
#> $nitro
#> FormalContext with 3 objects and 3 attributes.
#> nitro >= 1 nitro >= 2 nitro >= 3
#> 1 X
#> 2 X X
#> 3 X X X
#>
#> $OS
#> FormalContext with 3 objects and 2 attributes.
#> OS = O OS = S
#>
#> O X
#> S X
#>
#> $ring
#> FormalContext with 2 objects and 2 attributes.
#> ring = hex ring = penta
#> hex X
#> penta X
#>
## ------------------------------------------------
## Method `FormalContext$background_knowledge`
## ------------------------------------------------
filename <- system.file("contexts", "aromatic.csv", package = "fcaR")
fc <- FormalContext$new(filename)
fc$scale("nitro", "ordinal", comparison = `>=`, values = 1:3)
fc$scale("OS", "nominal", c("O", "S"))
fc$scale(attributes = "ring", type = "nominal")
fc$background_knowledge()
#> Implication set with 0 implications.
## ------------------------------------------------
## Method `FormalContext$incidence`
## ------------------------------------------------
fc <- FormalContext$new(planets)
fc$incidence()
#> small medium large near far moon no_moon
#> Mercury 1 0 0 1 0 0 1
#> Venus 1 0 0 1 0 0 1
#> Earth 1 0 0 1 0 1 0
#> Mars 1 0 0 1 0 1 0
#> Jupiter 0 0 1 0 1 1 0
#> Saturn 0 0 1 0 1 1 0
#> Uranus 0 1 0 0 1 1 0
#> Neptune 0 1 0 0 1 1 0
#> Pluto 1 0 0 0 1 1 0
## ------------------------------------------------
## Method `FormalContext$subcontext`
## ------------------------------------------------
fc <- FormalContext$new(planets)
fc$subcontext(attributes = c("moon", "no_moon"))
#> FormalContext with 9 objects and 2 attributes.
#> moon no_moon
#> Mercury X
#> Venus X
#> Earth X
#> Mars X
#> Jupiter X
#> Saturn X
#> Uranus X
#> Neptune X
#> Pluto X
## ------------------------------------------------
## Method `FormalContext$[`
## ------------------------------------------------
fc <- FormalContext$new(planets)
fc[, c("moon", "no_moon")]
#> FormalContext with 9 objects and 2 attributes.
#> moon no_moon
#> Mercury X
#> Venus X
#> Earth X
#> Mars X
#> Jupiter X
#> Saturn X
#> Uranus X
#> Neptune X
#> Pluto X