daisy.Rd
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 \(n\times p\),
say. Dissimilarities will be computed
between the rows of |
---|---|
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 |
weights | an optional numeric vector of length \(p\)(= |
warnBin, warnAsym, warnConst | logicals indicating if the corresponding type checking warnings should be signalled (when found). |
warnType | logical indicating if all the type checking warnings should be active or not. |
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
.
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
.
Dissimilarities are used as inputs to cluster analysis and multidimensional scaling. The choice of metric may have a large impact.
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.
data(agriculture) ## Example 1 in ref: ## Dissimilarities using Euclidean metric and without standardization d.agr <- daisy(agriculture, metric = "euclidean", stand = FALSE) d.agr#> Dissimilarities : #> B DK D GR E F IRL #> DK 5.408327 #> D 2.061553 3.405877 #> GR 22.339651 22.570113 22.661200 #> E 9.818350 11.182576 10.394710 12.567418 #> F 3.448188 3.512834 2.657066 20.100995 8.060397 #> IRL 12.747549 13.306014 13.080138 9.604166 3.140064 10.564563 #> I 5.803447 5.470832 5.423099 17.383325 5.727128 2.773085 7.920859 #> L 4.275512 2.220360 2.300000 24.035391 12.121056 4.060788 14.569145 #> NL 1.649242 5.096077 2.435159 20.752349 8.280097 2.202272 11.150785 #> P 17.236299 17.864490 17.664088 5.162364 7.430343 15.164432 4.601087 #> UK 2.828427 8.052950 4.850773 21.485344 8.984431 5.303772 12.103718 #> I L NL P #> DK #> D #> GR #> E #> F #> IRL #> I #> L 6.660330 #> NL 4.204759 4.669047 #> P 12.515990 19.168985 15.670673 #> UK 6.723095 7.102112 3.124100 16.323296 #> #> Metric : euclidean #> Number of objects : 12#> B DK D GR E F IRL I #> 5.408327 0.000000 3.405877 22.570113 11.182576 3.512834 13.306014 5.470832 #> L NL P UK #> 2.220360 5.096077 17.864490 8.052950#> B DK D GR E F IRL #> B 0.00000000 0.22148078 0.08178881 0.8438459 0.38135483 0.11538211 0.47547804 #> DK 0.22148078 0.00000000 0.13969197 0.9145729 0.45208184 0.12117405 0.54620505 #> D 0.08178881 0.13969197 0.00000000 0.8854337 0.42294264 0.09203485 0.51706585 #> GR 0.84384585 0.91457286 0.88543366 0.0000000 0.46249103 0.79339881 0.36836781 #> E 0.38135483 0.45208184 0.42294264 0.4624910 0.00000000 0.33090779 0.09412321 #> F 0.11538211 0.12117405 0.09203485 0.7933988 0.33090779 0.00000000 0.42503100 #> IRL 0.47547804 0.54620505 0.51706585 0.3683678 0.09412321 0.42503100 0.00000000 #> I 0.15222215 0.22294916 0.19380996 0.6916237 0.22913268 0.10177511 0.32325589 #> L 0.15646414 0.06501664 0.07467532 0.9601090 0.49761796 0.16671017 0.59174117 #> NL 0.05318802 0.19426679 0.09477583 0.7906578 0.32816681 0.08816811 0.42229002 #> P 0.66155453 0.73228154 0.70314234 0.1822913 0.28019970 0.61110749 0.18607649 #> UK 0.10095934 0.32244012 0.18274816 0.7629870 0.30049599 0.21634145 0.39461920 #> I L NL P UK #> B 0.1522221 0.15646414 0.05318802 0.6615545 0.1009593 #> DK 0.2229492 0.06501664 0.19426679 0.7322815 0.3224401 #> D 0.1938100 0.07467532 0.09477583 0.7031423 0.1827482 #> GR 0.6916237 0.96010899 0.79065783 0.1822913 0.7629870 #> E 0.2291327 0.49761796 0.32816681 0.2801997 0.3004960 #> F 0.1017751 0.16671017 0.08816811 0.6111075 0.2163414 #> IRL 0.3232559 0.59174117 0.42229002 0.1860765 0.3946192 #> I 0.0000000 0.26848528 0.11202114 0.5093324 0.2401945 #> L 0.2684853 0.00000000 0.16945115 0.7778177 0.2574235 #> NL 0.1120211 0.16945115 0.00000000 0.6083665 0.1281733 #> P 0.5093324 0.77781766 0.60836651 0.0000000 0.5806957 #> UK 0.2401945 0.25742348 0.12817333 0.5806957 0.0000000#> 153 dissimilarities, summarized : #> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0.1418 0.4164 0.5101 0.5098 0.6051 0.8875 #> Metric : mixed ; Types = N, N, A, N, O, O, I, I #> Number of objects : 18#> 153 dissimilarities, summarized : #> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0.1647 0.4387 0.5265 0.5293 0.6252 0.9007 #> Metric : mixed ; Types = A, N, A, N, O, O, T, I #> Number of objects : 18## this failed earlier: summary(dfl3 <- daisy(flower, type = list(asymm = c("V1", "V3"), symm= 2, ordratio= 7, logratio= 8)))#> 153 dissimilarities, summarized : #> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0.1649 0.4378 0.5350 0.5288 0.6318 0.8972 #> Metric : mixed ; Types = A, S, A, N, O, O, T, I #> Number of objects : 18