gset {sets}R Documentation

Generalized sets

Description

Creation and manipulation of generalized sets.

Usage

gset(support, memberships, charfun, elements, universe)
as.gset(x)
is.gset(x)
gset_support(x)
gset_core(x)
gset_peak(x)
gset_height(x)
gset_universe(x)

gset_memberships(x)
gset_transform_memberships(x, FUN, ...)
gset_concentrate(x)
gset_dilate(x)
gset_normalize(x, height = 1)
gset_defuzzify(x, method = c("meanofmax", "smallestofmax", "largestofmax", "centroid"))

gset_is_empty(x)
gset_is_subset(x, y)
gset_is_proper_subset(x, y)
gset_is_equal(x, y)
gset_contains_element(x, e)

gset_is_set(x)
gset_is_multiset(x)
gset_is_fuzzy_set(x)
gset_is_set_or_multiset(x)
gset_is_set_or_fuzzy_set(x)
gset_is_fuzzy_multiset(x)
gset_is_crisp(x)

gset_cardinality(x, type = c("absolute", "relative"))
gset_union(...)
gset_sum(...)
gset_difference(...)
gset_product(...)
gset_mean(x, y, type = c("arithmetic", "geometric", "harmonic"))
gset_intersection(...)
gset_symdiff(...)
gset_complement(x, y)
gset_power(x)
gset_cartesian(...)
gset_combn(x, m)

gset_similarity(x, y, method = "Jaccard")

e(x, memberships = 1L)
is_element(e)

## S3 method for class 'gset':
cut(x, level = 1, ...)
## S3 method for class 'gset':
mean(x, ...)
## S3 method for class 'gset':
median(x, na.rm = FALSE)
## S3 method for class 'gset':
length(x)

Arguments

x For e(), as.gset() and is.gset(): an R object. A (g)set object otherwise.
y A (g)set object.
e An object of class element.
m Number of elements to choose.
support A set of elements giving the support of the gset (elements with non-zero memberships). Must be a subset of the universe, if specified.
memberships For an (“ordinary”) set: 1L (or simply missing). For a fuzzy set: a value between 0 and 1. For a multiset: a positive integer. For a fuzzy multiset: a list of multisets with elements from the unit interval (or a list of vectors interpreted as such). Otherwise, the argument will be transformed using as.gset.
elements A set (or list) of e objects which are object/memberships-pairs.
charfun A function taking an object and returning the membership.
FUN A function, to be applied to a membership vector.
type For gset_cardinality(): cardinality type (either "absolute" or "relative"). For gset_mean(): mean type ("arithmetic", "geometric", or "harmonic").
height Double from the unit interval for scaling memberships.
universe An optional set of elements. If NULL, d efaults to the value of sets_options("universe"). If the latter is also NULL, the support will be used in computations.
method For gset_similarity(): Currently, only "Jaccard" is implemented (cardinality of the intersection, divided by the cardinality of the union). For gset_defuzzify(): "centroid" computes the arithmetic mean of the set elements, using the membership values as weights. "smallestofmax" / "meanofmax" / "largestofmax": returns the minimum/mean/maximum of all set elements with maximal membership degree.
level The minimum membership level.
na.rm logical indicating whether NA values should be removed.
... For gset_foo(): (g)set objects. For the mean and sort methods: additional parameters internally passed to mean and order, respectively. For gset_transform_memberships: further arguments passed to FUN. For cut: currently not used.

Details

These functions represent basic infrastructure for handling generalized sets of general (R) objects.

A generalized set (or gset) is set of pairs (e, f), where e is some set element and f is the characteristic (or membership) function. For (“ordinary”) sets f maps to {0, 1}, for fuzzy sets into the unit interval, for multisets into the natural numbers, and for fuzzy multisets f maps to the set of multisets over the unit interval.

The gset_is_foo() predicates are vectorized. In addition to the methods defined, one can use the following operators: | for the union, & for the intersection, + for the sum, - for the difference, %D% for the symmetric difference, * and ^n for the (n-fold) cartesian product, 2^ for the power set, %e% for the element-of predicate, < and <= for the (proper) subset predicate, > and >= for the (proper) superset predicate, and == and != for (in)equality. The Summary methods do also work if defined for the set elements. The mean and median methods try to convert the object to a numeric vector before calling the default methods. set_combn returns the gset of all subsets of specfied length.

gset_support, gset_core, and gset_peak return the set of elements with memberships greater than zero, equal to one, and equal to the maximum membership, respectively. gset_memberships returns the membership vector. gset_height returns only the largest membership degree. gset_cardinality computes either the absolute or the relative cardinality, i.e. the memberships sum, or the absolute cardinality divided by the number of elements, respectively. The length method for gsets gives the (absolute) cardinality. gset_transform_memberships applies function FOO to the membership vector of the supplied gset and returns the transformed gset. The transformed memberships are guaranteed to be in the unit interval. gset_concentrate and gset_dilate are convenience functions, using the square and the square root, respectively. gset_normalize divides the memberships by their maximum and scales with height. gset_product (gset_mean) of some gsets compute the gset with the corresponding memberships multiplied (averaged).

The cut method “filters” all elements with membership not less then level — the result, thus, is a crisp (multi)set. gset_similarity computes the simple Jaccard similarity between two generalized sets A and B, i.e., the cardinality of the intersection divided by the cardinality of the union of A and B.

Because set elements are unordered, it is not allowed to use positional indexing. However, it is possible to do indexing using element labels or simply the elements themselves (useful, e.g., for subassignment). In addition, it is possible to iterate over all elements using for and lapply/sapply.

gset_contains_element is vectorized in e, that is, if e is an atomic vector or list, the is-element operation is performed element-wise, and a logical vector returned. Note that, however, objects of class tuple are taken as atomic objects to correctly handle sets of tuples.

See Also

set for “ordinary” sets, gset_outer, and tuple for tuples (“vectors”).

Examples

## multisets
(A <- gset(letters[1:5], memberships = c(3, 2, 1, 1, 1)))
(B <- gset(c("a", "c", "e", "f"), memberships = c(2, 2, 1, 2)))
rep(B, 2)

gset_union(A, B)
gset_intersection(A, B)
gset_complement(A, B)

gset_is_multiset(A)
gset_sum(A, B)
gset_difference(A, B)

## fuzzy sets
(A <- gset(letters[1:5], memberships = c(1, 0.3, 0.8, 0.6, 0.2)))
(B <- gset(c("a", "c", "e", "f"), memberships = c(0.7, 1, 0.4, 0.9)))
cut(B, 0.5)
A * B
A <- gset(3L, memberships = 0.5, universe = 1:5)
!A

## fuzzy multisets
(A <- gset(c("a", "b", "d"),
         memberships = list(c(0.3, 1, 0.5), c(0.9, 0.1), gset(c(0.4, 0.7), c(1, 2)))))
(B <- gset(c("a", "c", "d", "e"),
         memberships = list(c(0.6, 0.7), c(1, 0.3), c(0.4, 0.5), 0.9)))
gset_union(A, B)
gset_intersection(A, B)
gset_complement(A, B)

## other operations
mean(gset(1:3, c(0.1,0.5,0.9)))
median(gset(1:3, c(0.1,0.5,0.9)))

[Package sets version 0.6 Index]