Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Use MASS instead of robustbase
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Sep 6, 2023
1 parent d26889b commit 2c99a3d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Imports:
graphics,
grDevices,
methods,
robustbase,
MASS,
stats,
utils
Suggests:
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ exportMethods(transform_plr)
exportMethods(variance)
exportMethods(variation)
import(arkhe)
importFrom(MASS,cov.rob)
importFrom(arkhe,remove_NA)
importFrom(arkhe,remove_zero)
importFrom(arkhe,replace_NA)
Expand All @@ -72,7 +73,6 @@ importFrom(methods,setMethod)
importFrom(methods,slot)
importFrom(methods,slotNames)
importFrom(methods,validObject)
importFrom(robustbase,covMcd)
importFrom(stats,as.dist)
importFrom(stats,contr.helmert)
importFrom(stats,cov)
Expand Down
8 changes: 5 additions & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,11 @@ NULL
#' @param cov A [`numeric`] matrix giving the covariance of the
#' distribution. If missing, will be estimated from `x`.
#' @param robust A [`logical`] scalar: should robust location and scatter
#' estimation be used (see [robustbase::covMcd()])?
#' @param ... Extra parameters to be passed to [robustbase::covMcd()].
#' estimation be used?
#' @param method A [`character`] string specifying the method to be used.
#' It must be one of "`mve`" (minimum volume ellipsoid) or "`mcd`" (minimum
#' covariance determinant). Only used if `robust` is `TRUE`.
#' @param ... Extra parameters to be passed to [MASS::cov.rob()].
#' Only used if `robust` is `TRUE`.
#' @return A [`numeric`] vector.
#' @seealso [stats::mahalanobis()]
Expand Down Expand Up @@ -666,7 +669,6 @@ NULL
#' `quantile` is used as a cut-off value for outlier detection: observations
#' with larger (squared) Mahalanobis distance are considered as potential
#' outliers.
#' @param ... Extra parameters to be passed to [robustbase::covMcd()].
#' @details
#' An outlier can be defined as having a very large Mahalanobis distance from
#' all observations. In this way, a certain proportion of the observations can
Expand Down
2 changes: 1 addition & 1 deletion R/nexus-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#' @importFrom methods as as<- callGeneric callNextMethod
#' .hasSlot initialize is new setClass setGeneric setMethod slot slot<-
#' slotNames validObject .valueClassTest
#' @importFrom robustbase covMcd
#' @importFrom MASS cov.rob
#' @importFrom stats as.dist contr.helmert cov dist mahalanobis ppoints qchisq
#' var
#' @importFrom utils combn
Expand Down
7 changes: 4 additions & 3 deletions R/outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ NULL
setMethod(
f = "outliers",
signature = c(object = "CompositionMatrix"),
definition = function(object, center = NULL, cov = NULL, robust = FALSE,
quantile = 0.975, ...) {
definition = function(object, center = NULL, cov = NULL, robust = TRUE,
method = c("mve", "mcd"), quantile = 0.975, ...) {

dof <- ncol(object) - 1L
d2 <- mahalanobis(object, center = center, cov = cov, ..., robust = robust)
d2 <- mahalanobis(object, center = center, cov = cov, ...,
robust = robust, method = method)
d <- sqrt(d2)
limit <- sqrt(stats::qchisq(p = quantile, df = dof))
out <- d > limit
Expand Down
23 changes: 11 additions & 12 deletions R/statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,23 @@ setMethod("dist", "CompositionMatrix", dist.CompositionMatrix)

#' @export
#' @method mahalanobis CompositionMatrix
mahalanobis.CompositionMatrix <- function(x, center, cov, ..., robust = TRUE) {
mahalanobis.CompositionMatrix <- function(x, center, cov, ..., robust = TRUE,
method = c("mve", "mcd")) {
## Transformation
x <- transform_ilr(x)

if (missingORnull(center) | missingORnull(cov)) {
if (robust) {
## Robust estimators
v <- robustbase::covMcd(x, ...)
} else {
## Standard estimators
v <- list(center = colMeans(x), cov = stats::cov(x))
}

est <- list(center = NULL, cov = NULL)
est$center <- if (missingORnull(center)) v$center else center
est$cov <- if (missingORnull(cov)) v$cov else 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)
}

Expand Down
10 changes: 7 additions & 3 deletions man/mahalanobis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions man/outliers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c99a3d

Please sign in to comment.