relabel_class: Relabel class labels according to the reference labels

View source: R/utils.R

relabel_classR Documentation

Relabel class labels according to the reference labels

Description

Relabel class labels according to the reference labels

Usage

relabel_class(class, ref, full_set = union(class, ref), return_map = TRUE)

Arguments

class

A vector of class labels.

ref

A vector of reference labels.

full_set

The full set of labels.

return_map

Whether to return the mapping of the adjusted labels.

Details

In partitions, the exact value of the class label is not of importance. E.g. for two partitions a, a, a, b, b, b, b and b, b, b, a, a, a, a, they are the same partitions although the labels of a and b are switched in the two partitions. Even the partition c, c, c, d, d, d, d is the same as the previous two although it uses a different set of labels. Here relabel_class function relabels class vector according to the labels in ref vector by looking for a mapping m() to maximize sum(m(class) == ref).

Mathematically, this is called linear sum assignment problem and it is solved by solve_LSAP.

Value

A named vector where names correspond to the labels in class and values correspond to ref, which means map = relabel_class(class, ref); map[class] returns the relabelled labels.

The returned object attaches a data frame with three columns:

  • original labels. in class

  • adjusted labels. according to ref

  • reference labels. in ref

If return_map in the relabel_class is set to FALSE, the function simply returns a vector of adjusted class labels.

If the function returns the mapping vector (when return_map = TRUE), the mapping variable is always character, which means, if your class and ref are numeric, you need to convert them back to numeric explicitely. If return_map = FALSE, the returned relabelled vector has the same mode as class.

Examples

class = c(rep("a", 10), rep("b", 3))
ref = c(rep("b", 4), rep("a", 9))
relabel_class(class, ref)
relabel_class(class, ref, return_map = FALSE)
# if class and ref are from completely different sets
class = c(rep("A", 10), rep("B", 3))
relabel_class(class, ref)

# class labels are numeric
class = c(rep(1, 10), rep(2, 3))
ref = c(rep(2, 4), rep(1, 9))
relabel_class(class, ref)
relabel_class(class, ref, return_map = FALSE)

jokergoo/cola documentation built on Feb. 29, 2024, 1:41 a.m.