ham: ham()

View source: R/ham.R

hamR Documentation

ham()

Description

This function computes a matrix distance using the Hamming distance as proximity measure.

Usage

ham(mat_1, mat_2)

Arguments

mat_1

a vector, a matrix or a data.frame of binary values that may contain missing data

mat_2

a vector, a matrix or a data.frame of binary values with the same number of columns as mat_1 that may contain missing data

Details

ham returns the pairwise distances between rows (observations) of a single matrix if mat_1 equals mat_2. Otherwise ham returns the matrix distance between rows of the two matrices mat_1 and mat_2 if this 2 matrices are different in input. Computing the Hamming distance stays possible despite the presence of missing data by applying the following formula. Assuming that A and B are 2 matrices such as ncol(A) = ncol(B). The Hamming distance between the i^{th} row of A and the k^{th} row of B equals:

\mbox{ham}(A_i,B_k) = \frac{∑_j 1_{≤ft\{A_{ij} \neq B_{kj}\right\}}}{∑_j 1}\times≤ft(\frac{∑_j 1}{∑_j 1_{≤ft\{!\mbox{is.na}(A_{ij}) \& !\mbox{is.na}( B_{kj})\right\}}}\right)

where: i = 1,…,\mbox{nrow}(A) and k = 1,…,\mbox{nrow}(B); And the expression located to the right term of the multiplication corresponds to a specific weigh applied in presence of NAs in A_i and/or B_k.

This specificity is not implemented in the cdist function and the Hamming distance can not be computed using the dist function either.

The Hamming distance can not be calculated in only two situations:

  1. If a row of A or B has only missing values (ie for each of the columns of A or B respectively).

  2. The union of the indexes of the missing values in row i of A with the indexes of the missing values in row j of B concerns the indexes of all considered columns.

Example: Assuming that \mbox{ncol}(A) = \mbox{ncol}(B) = 3, if A_i = (1,\mbox{NA},0) and B_j = (\mbox{NA},1,\mbox{NA}), for each column, either the information in row i is missing in A, or the information is missing in B, which induces: \mbox{ham}(A_i,B_k) = \mbox{NA}.

If mat_1 is a vector and mat_2 is a matrix (or data.frame) or vice versa, the length of mat_1 must be equal to the number of columns of mat_2.

Value

A distance matrix

Author(s)

Gregory Guernec

otrecod.pkg@gmail.com

References

Roth R (2006). Introduction to Coding Theory. Cambridge University Press.

Examples

set.seed(3010)
sample_A <- sample(c(0, 1), 12, replace = TRUE)
set.seed(3007)
sample_B <- sample(c(0, 1), 15, replace = TRUE)
A <- matrix(sample_A, ncol = 3)
B <- matrix(sample_B, ncol = 3)

# These 2 matrices have no missing values

# Matrix of pairwise distances with A:
ham(A, A)

# Matrix of distances between the rows of A and the rows of B:
ham(A, B)

# If mat_1 is a vector of binary values:
ham(c(0, 1, 0), B)

# Now by considering A_NA and B_NA two matrices built from A and B respectively,
# where missing values have been manually added:
A_NA <- A
A_NA[3, 1] <- NA
A_NA[2, 2:3] <- rep(NA, 2)

B_NA <- B
B_NA[2, 2] <- NA

ham(A_NA, B_NA)


OTrecod documentation built on Oct. 5, 2022, 5:06 p.m.