daisy | R Documentation |

Compute all the pairwise dissimilarities (distances) between observations
in the data set. The original variables may be of mixed types. In
that case, or whenever `metric = "gower"`

is set, a
generalization of Gower's formula is used, see ‘Details’
below.

```
daisy(x, metric = c("euclidean", "manhattan", "gower"),
stand = FALSE, type = list(), weights = rep.int(1, p),
warnBin = warnType, warnAsym = warnType, warnConst = warnType,
warnType = TRUE)
```

`x` |
numeric matrix or data frame, of dimension |

`metric` |
character string specifying the metric to be used.
The currently available options are “Gower's distance” is chosen by metric |

`stand` |
logical flag: if TRUE, then the measurements in If not all columns of |

`type` |
list for specifying some (or all) of the types of the
variables (columns) in `"asymm"` **A**symmetric binary variable, aka`"A"` in result`Types` , see`dissimilarity.object` .`"symm"` **S**ymmetric binary variable, aka`"S"` .`"factor"` **N**ominal – the default for`factor` variables, aka`"N"` . When the factor has 2 levels, this is equivalent to`type = "S"` for a (symmetric) binary variable.`"ordered"` **O**rdinal – the default for`ordered` (factor) variables, aka`"O"` , see`dissimilarity.object` .`"logratio"` ratio scaled numeric variables that are to be logarithmically transformed ( `log10` ) and then treated as numeric (`"I"` ): must be*positive*numeric variable.`"ordratio"` “ra **T**io”-like variable to be treated as`ordered` (using the factor codes`unclass(as.ordered(x[,j]))` ), aka`"T"` .`"numeric"` /`"integer"` **I**nterval scaled – the**default**for all numeric (incl`integer` ) columns of`x` , aka`"I"` in result`Types` , see`dissimilarity.object` .
Each component is a (character or numeric) vector, containing either
the names or the numbers of the corresponding columns of Variables not mentioned in |

`weights` |
an optional numeric vector of length |

`warnBin, warnAsym, warnConst` |
logicals indicating if the corresponding type checking warnings should be signalled (when found). |

`warnType` |
logical indicating if |

The original version of `daisy`

is fully described in chapter 1
of Kaufman and Rousseeuw (1990).
Compared to `dist`

whose input must be numeric
variables, the main feature of `daisy`

is its ability to handle
other variable types as well (e.g. nominal, ordinal, (a)symmetric
binary) even when different types occur in the same data set.

The handling of nominal, ordinal, and (a)symmetric binary data is
achieved by using the general dissimilarity coefficient of Gower
(1971). If `x`

contains any columns of these
data-types, both arguments `metric`

and `stand`

will be
ignored and Gower's coefficient will be used as the metric. This can
also be activated for purely numeric data by `metric = "gower"`

.
With that, each variable (column) is first standardized by dividing
each entry by the range of the corresponding variable, after
subtracting the minimum value; consequently the rescaled variable has
range `[0,1]`

, exactly.

Note that setting the type to `symm`

(symmetric binary) gives the
same dissimilarities as using *nominal* (which is chosen for
non-ordered factors) only when no missing values are present, and more
efficiently.

Note that `daisy`

signals a warning when 2-valued numerical
variables do not have an explicit `type`

specified, because the
reference authors recommend to consider using `"asymm"`

; the
warning may be silenced by `warnBin = FALSE`

.

In the `daisy`

algorithm, missing values in a row of x are not
included in the dissimilarities involving that row. There are two
main cases,

If all variables are interval scaled (and

`metric`

is*not*`"gower"`

), the metric is "euclidean", and`n_g`

is the number of columns in which neither row i and j have NAs, then the dissimilarity d(i,j) returned is`\sqrt{p/n_g}`

(`p=`

ncol(x)) times the Euclidean distance between the two vectors of length`n_g`

shortened to exclude NAs. The rule is similar for the "manhattan" metric, except that the coefficient is`p/n_g`

. If`n_g = 0`

, the dissimilarity is NA.When some variables have a type other than interval scaled, or if

`metric = "gower"`

is specified, the dissimilarity between two rows is the weighted mean of the contributions of each variable. Specifically,`d_{ij} = d(i,j) = \frac{\sum_{k=1}^p w_k \delta_{ij}^{(k)} d_{ij}^{(k)}}{ \sum_{k=1}^p w_k \delta_{ij}^{(k)}}.`

In other words,

`d_{ij}`

is a weighted mean of`d_{ij}^{(k)}`

with weights`w_k \delta_{ij}^{(k)}`

, where`w_k`

`= weigths[k]`

,`\delta_{ij}^{(k)}`

is 0 or 1, and`d_{ij}^{(k)}`

, the k-th variable contribution to the total distance, is a distance between`x[i,k]`

and`x[j,k]`

, see below.The 0-1 weight

`\delta_{ij}^{(k)}`

becomes zero when the variable`x[,k]`

is missing in either or both rows (i and j), or when the variable is asymmetric binary and both values are zero. In all other situations it is 1.The contribution

`d_{ij}^{(k)}`

of a nominal or binary variable to the total dissimilarity is 0 if both values are equal, 1 otherwise. The contribution of other variables is the absolute difference of both values, divided by the total range of that variable. Note that “standard scoring” is applied to ordinal variables, i.e., they are replaced by their integer codes`1:K`

. Note that this is not the same as using their ranks (since there typically are ties).As the individual contributions

`d_{ij}^{(k)}`

are in`[0,1]`

, the dissimilarity`d_{ij}`

will remain in this range. If all weights`w_k \delta_{ij}^{(k)}`

are zero, the dissimilarity is set to`NA`

.

an object of class `"dissimilarity"`

containing the
dissimilarities among the rows of `x`

. This is typically the
input for the functions `pam`

, `fanny`

, `agnes`

or
`diana`

. For more details, see `dissimilarity.object`

.

Dissimilarities are used as inputs to cluster analysis and multidimensional scaling. The choice of metric may have a large impact.

Anja Struyf, Mia Hubert, and Peter and Rousseeuw, for the original
version.

Martin Maechler improved the `NA`

handling and
`type`

specification checking, and extended functionality to
`metric = "gower"`

and the optional `weights`

argument.

Gower, J. C. (1971)
A general coefficient of similarity and some of its properties,
*Biometrics* **27**, 857–874.

Kaufman, L. and Rousseeuw, P.J. (1990)
*Finding Groups in Data: An Introduction to Cluster Analysis*.
Wiley, New York.

Struyf, A., Hubert, M. and Rousseeuw, P.J. (1997)
Integrating Robust Clustering Techniques in S-PLUS,
*Computational Statistics and Data Analysis* **26**, 17–37.

`dissimilarity.object`

, `dist`

,
`pam`

, `fanny`

, `clara`

,
`agnes`

, `diana`

.

```
data(agriculture)
## Example 1 in ref:
## Dissimilarities using Euclidean metric and without standardization
d.agr <- daisy(agriculture, metric = "euclidean", stand = FALSE)
d.agr
as.matrix(d.agr)[,"DK"] # via as.matrix.dist(.)
## compare with
as.matrix(daisy(agriculture, metric = "gower"))
## Example 2 in reference, extended --- different ways of "mixed" / "gower":
example(flower) # -> data(flower) *and* provide 'flowerN'
summary(d0 <- daisy(flower)) # -> the first 3 {0,1} treated as *N*ominal
summary(dS123 <- daisy(flower, type = list(symm = 1:3))) # first 3 treated as *S*ymmetric
stopifnot(dS123 == d0) # i.e., *S*ymmetric <==> *N*ominal {for 2-level factor}
summary(dNS123<- daisy(flowerN, type = list(symm = 1:3)))
stopifnot(dS123 == d0)
## by default, however ...
summary(dA123 <- daisy(flowerN)) # .. all 3 logicals treated *A*symmetric binary (w/ warning)
summary(dA3 <- daisy(flower, type = list(asymm = 3)))
summary(dA13 <- daisy(flower, type = list(asymm = c(1, 3), ordratio = 7)))
## Mixing variable *names* and column numbers (failed in the past):
summary(dfl3 <- daisy(flower, type = list(asymm = c("V1", "V3"), symm= 2,
ordratio= 7, logratio= 8)))
## If we'd treat the first 3 as simple {0,1}
Nflow <- flower
Nflow[,1:3] <- lapply(flower[,1:3], \(f) as.integer(as.character(f)))
summary(dN <- daisy(Nflow)) # w/ warning: treated binary .. 1:3 as interval
## Still, using Euclidean/Manhattan distance for {0-1} *is* identical to treating them as "N" :
stopifnot(dN == d0)
stopifnot(dN == daisy(Nflow, type = list(symm = 1:3))) # or as "S"
```

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.