-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist.R
57 lines (47 loc) · 1.81 KB
/
dist.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# DISTANCES
#' @include AllGenerics.R
NULL
# Distances ====================================================================
#' @export
#' @method dist CompositionMatrix
dist.CompositionMatrix <- function(x, method = "euclidean",
diag = FALSE, upper = FALSE, p = 2) {
x <- transform_clr(x)
stats::dist(x, method = method, diag = diag, upper = upper, p = p)
}
#' @export
#' @rdname dist
#' @aliases dist,CompositionMatrix-method
setMethod("dist", "CompositionMatrix", dist.CompositionMatrix)
# Mahalanobis ==================================================================
#' @export
#' @method mahalanobis CompositionMatrix
mahalanobis.CompositionMatrix <- function(x, center, cov, ..., robust = TRUE,
method = c("mve", "mcd")) {
## Transformation
x <- transform_ilr(x)
mahalanobis(x, center, cov, ..., robust = robust, method = method)
}
#' @export
#' @rdname mahalanobis
#' @aliases mahalanobis,CompositionMatrix-method
setMethod("mahalanobis", "CompositionMatrix", mahalanobis.CompositionMatrix)
#' @export
#' @method mahalanobis ILR
mahalanobis.ILR <- function(x, center, cov, ..., robust = TRUE,
method = c("mve", "mcd")) {
if (missingORnull(center) | missingORnull(cov)) {
if (!robust) method <- "classical" # Standard estimators
else method <- match.arg(method, several.ok = FALSE) # Robust estimators
v <- MASS::cov.rob(x, method = method, ...)
}
est <- list(center = NULL, cov = NULL)
est$center <- if (missingORnull(center)) v$center else center
est$cov <- if (missingORnull(cov)) v$cov else cov
message(v$sing)
stats::mahalanobis(x, center = est$center, cov = est$cov)
}
#' @export
#' @rdname mahalanobis
#' @aliases mahalanobis,ILR-method
setMethod("mahalanobis", "ILR", mahalanobis.ILR)