gset: Generalized sets

View source: R/gset.R

gsetR Documentation

Generalized sets

Description

Creation and manipulation of generalized sets.

Usage

gset(support, memberships, charfun, elements, universe, bound,
     assume_numeric_memberships)
as.gset(x)
is.gset(x)
gset_support(x)
gset_core(x, na.rm = FALSE)
gset_peak(x, na.rm = FALSE)
gset_height(x, na.rm = FALSE)
gset_universe(x)
gset_bound(x)

gset_memberships(x, filter = NULL)
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, na.rm = FALSE)
gset_is_subset(x, y, na.rm = FALSE)
gset_is_proper_subset(x, y, na.rm = FALSE)
gset_is_equal(x, y, na.rm = FALSE)
gset_contains_element(x, e)

gset_is_set(x, na.rm = FALSE)
gset_is_multiset(x, na.rm = FALSE)
gset_is_fuzzy_set(x, na.rm = FALSE)
gset_is_set_or_multiset(x, na.rm = FALSE)
gset_is_set_or_fuzzy_set(x, na.rm = FALSE)
gset_is_fuzzy_multiset(x)
gset_is_crisp(x, na.rm = FALSE)
gset_has_missings(x)

gset_cardinality(x, type = c("absolute", "relative"), na.rm = FALSE)
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)

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

## S3 method for class 'gset'
cut(x, level = 1, type = c("alpha", "nu"), strict = FALSE, ...)
## S3 method for class 'gset'
mean(x, ..., na.rm = FALSE)

## S3 method for class 'gset'
## median(x, na.rm = FALSE, ...)     [R >= 3.4.0]
## median(x, na.rm)                  [R < 3.4.0]
## S3 method for class 'gset'
length(x)
## S3 method for class 'gset'
lengths(x, use.names = TRUE)

Arguments

x

For e(), as.gset() and is.gset(): an R object. A (g)set object otherwise. gset_memberships() also accepts tuple objects.

y

A (g)set object.

e

An object of class element.

filter

Optional vector of elements to be filtered.

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.

bound

Integer used to compute the absolute complement for (fuzzy) multisets. If NULL, defaults to the value of sets_options("bound"). If the latter is also NULL, the maximum multiplicity will be used in computations.

assume_numeric_memberships

When applying carfun() to the universe, should numeric memberships (i.e. fuzzy sets or multisets) be assumed (default)? If FALSE, fuzzy multisets will be created.

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"). For "cut": either "alpha" or "nu".

strict

Logical indicating whether the cut level must be exceeded strictly (“greater than”) or not (“greater than or equal”).

height

Double from the unit interval for scaling memberships.

universe

An optional set of elements. If NULL, defaults to the value of sets_options("universe"). If the latter is also NULL, the support will be used in computations.

method

"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.

use.names

logical; should the names of x be used in the result?

...

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 specified 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(s) of a given (tuple of) gset(s), optionally restricted to the elements specified by filter. 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. The lengths method coerces the set to a list before applying the length method on its elements. 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 provides both \alpha- and \nu-cuts. \alpha-cuts “filter” all elements with memberships greater than (or equal to) level—the result, thus, is a crisp (multi)set. \nu-cuts select those elements with a multiplicity exceeding level (only sensible for (fuzzy) multisets).

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.

References

D. Meyer and K. Hornik (2009), Generalized and customizable sets in R, Journal of Statistical Software 31(2), 1–27. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v031.i02")}.

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_memberships(tuple(A, B), c("a","c"))

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)))

## vectorization
list(gset(1, 0.5), gset(2, 2L), gset()) <= gset(1, 2L)

sets documentation built on May 29, 2024, 10:09 a.m.