aDist: Aitchison distance

View source: R/aDist.R

aDistR Documentation

Aitchison distance

Description

Computes the Aitchison distance between two compositions, between the corresponding rows of two data sets, between all rows of two data sets, or within the observations of one data set. Parts can be weighted (weighted Aitchison geometry), and missing values, zeros and cell weights (e.g. from a cellwise-robust estimator) can be taken into account via the available pairwise log-ratios.

Usage

aDist(
  x,
  y = NULL,
  type = c("sum", "rows", "cross"),
  weights = NULL,
  na.rm = FALSE,
  cellweights = NULL
)

iprod(x, y)

Arguments

x

a vector (one composition), matrix or data frame with the compositions in the rows.

y

NULL, or a vector, matrix or data frame of compositions to compare with x; see Details for the required dimensions.

type

character, one of "sum" (default), "rows" or "cross"; see Details. Ignored if y is NULL.

weights

optional vector of D positive part weights for the weighted Aitchison distance; see Details. NULL (default) means equal weights of one.

na.rm

logical. If TRUE, missing, zero and negative values are left out and the distance is computed from the remaining log-ratios. If FALSE (default), missing or infinite values give an error and distances involving rows with zero or negative values are NaN.

cellweights

optional non-negative cell weights: a matrix with the dimensions of x, which then also applies to y if y is NULL, or a list with elements x and y giving weight matrices for both data sets (either may be NULL for weights of one).

Details

The Aitchison distance between two compositions x and y with D parts is the Euclidean distance of their clr coordinates,

d_a(x, y)^2 = \sum_{j=1}^D \left(\mathrm{clr}(x)_j - \mathrm{clr}(y)_j\right)^2 = \frac{1}{D}\sum_{j<k}\left(\ln\frac{x_j}{x_k} - \ln\frac{y_j}{y_k}\right)^2.

It accounts for the relative scale of compositional data: it does not change if a composition is multiplied by a positive constant.

Output types. If y is NULL, the distance matrix between all observations of x is returned as a "dist" object and type is ignored. Otherwise type selects the output:

"sum"

(default) the sum of the distances between the corresponding rows of x and y; for two single compositions this is simply their distance.

"rows"

the vector of distances between the corresponding rows of x and y, e.g. to see which observations were imputed badly.

"cross"

the n \times m matrix of distances between every row of x and every row of y; y may have any number of rows, e.g. to find nearest neighbours of new observations in a reference data set.

For "sum" and "rows", x and y must have the same dimensions; for "cross" the same number of columns.

Weighted parts. With weights p = (p_1, \dots, p_D), the parts get different importance through a change of the reference measure of the simplex (Egozcue and Pawlowsky-Glahn, 2016; Hron et al., 2022):

d_p(x, y)^2 = \sum_{j=1}^D p_j \left(u_j - \bar u_p\right)^2, \qquad u = \ln x - \ln y, \quad \bar u_p = \frac{\sum_j p_j u_j}{\sum_j p_j},

i.e. the weighted Euclidean distance of the weighted clr coordinates. The weights are not normalised: p = (1, \dots, 1) gives the ordinary Aitchison distance, and multiplying all weights by c multiplies the distance by \sqrt{c}. Reducing weights never increases the distance (Egozcue and Pawlowsky-Glahn, 2016), so down-weighting a part acts like a soft version of dropping it from the composition. This is the discrete counterpart of fcenLRp for densities.

Missing values, zeros and cell weights. By default all values must be strictly positive and finite: missing or infinite values give an error, and every distance that involves a row with a zero or negative value is NaN (with a warning). With na.rm = TRUE, cells that are missing, zero or negative are left out. With cellweights, each cell gets a weight w \ge 0, for example a weight from a cellwise-robust method that down-weights outlying cells. For a comparison of two rows the parts receive the weights v_j = w^x_j w^y_j (0 for a cell left out), each log-ratio between parts j and k the weight v_j v_k, and the sum of squared log-ratio differences is estimated by the weighted mean over the available log-ratios times the number of all D(D-1)/2 log-ratios, as in Gower's distance for incomplete data (Gower, 1971). Without part weights, and with u = \ln x - \ln y and V = \sum_j v_j, this gives

d^2 = (D-1)\,\frac{V \sum_j v_j (u_j - \bar u_v)^2}{V^2 - \sum_j v_j^2}, \qquad \bar u_v = \frac{1}{V}\sum_j v_j u_j.

With all weights equal to one this is exactly the Aitchison distance; with a available parts and the others left out it is \frac{D-1}{a-1}\sum_{j \in A}(u_j - \bar u_A)^2, the Aitchison distance of the available subcomposition scaled up to D parts. If fewer than two parts carry weight, the distance is NA. Together with part weights p, the log-ratio weights are p_j v_j p_k v_k and the estimate is scaled to the weighted distance d_p; with all cell weights equal to one it is exactly d_p.

Value

For y = NULL, an object of class "dist". Otherwise, depending on type: a single number ("sum"; for two named single compositions or two one-row data frames a 1 x 1 matrix with their names as dimnames), a vector with one distance per row ("rows"), or a matrix with nrow(x) rows and nrow(y) columns ("cross"). Distances are NA where fewer than two parts are available, and NaN for rows with zero or negative values if na.rm = FALSE.

Author(s)

Matthias Templ, Bernhard Meindl

References

Aitchison, J. (1986) The Statistical Analysis of Compositional Data Monographs on Statistics and Applied Probability. Chapman and Hall Ltd., London (UK). 416p.

Aitchison, J. and Barcelo-Vidal, C. and Martin-Fernandez, J.A. and Pawlowsky-Glahn, V. (2000) Logratio analysis and compositional distance. Mathematical Geology, 32, 271-275.

Egozcue, J.J. and Pawlowsky-Glahn, V. (2016) Changing the reference measure in the simplex and its weighting effects. Austrian Journal of Statistics, 45 (4), 25-44. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.17713/ajs.v45i4.126")}

Gower, J.C. (1971) A general coefficient of similarity and some of its properties. Biometrics, 27 (4). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.2307/2528823")}

Hron, K. and Templ, M. and Filzmoser, P. (2010) Imputation of missing values for compositional data using classical and robust methods. Computational Statistics and Data Analysis, 54 (12), 3095-3107. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1016/j.csda.2009.11.023")}

Hron, K., Menafoglio, A., Palarea-Albaladejo, J., Filzmoser, P., Talska, R. and Egozcue, J.J. (2022) Weighting of parts in compositional data analysis: advances and applications. Mathematical Geosciences, 54 (1), 71-93. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/s11004-021-09952-y")}

See Also

cenLR, pivotCoord, ced, aDistDens, cellPcaCoDa

Examples

data(expenditures)
x <- xOrig <- expenditures
## Aitchison distance between two observations:
aDist(x[1, ], x[2, ])
aDist(as.numeric(x[1, ]), as.numeric(x[2, ]))
## distance matrix of x:
aDist(x)

## distance between an original and an imputed data set:
x[1,3] <- x[3,5] <- x[2,4] <- x[5,3] <- x[8,3] <- NA
xImp <- impCoda(x, method="ltsReg")$xImp
aDist(xOrig, xImp)
## ... and per observation: which rows were imputed worst?
round(aDist(xOrig, xImp, type = "rows"), 3)

## distances of three new observations to all observations of a data set
aDist(xOrig[1:3, ] * 1.1, xOrig, type = "cross")[, 1:6]

## weighted Aitchison distance: give the first part (housing) double weight
aDist(xOrig[1, ], xOrig[2, ], weights = c(2, 1, 1, 1, 1))

## incomplete data: use the available log-ratios
aDist(x[1:4, ], na.rm = TRUE)

## down-weight a suspicious cell (weight 0 = leave it out)
w <- matrix(1, nrow(xOrig), ncol(xOrig))
w[1, 2] <- 0
aDist(xOrig[1:2, ], xImp[1:2, ], type = "rows", cellweights = w[1:2, ])
data("expenditures") 
aDist(expenditures)  
x <- expenditures[, 1]
y <- expenditures[, 2]
aDist(x, y)
aDist(expenditures, expenditures)

robCompositions documentation built on Nov. 1, 2026, 1:06 a.m.