residuals.ssMRCD: Residual Method from an ssMRCD Object

View source: R/ssMRCD_methods.R

residuals.ssMRCDR Documentation

Residual Method from an ssMRCD Object

Description

Computes group-wise Mahalanobis residuals (standardized distances) using the robust local covariance and location estimates from an ssMRCD object. Residuals can be computed for the fitted data or for new data, and optionally summarized as a trimmed mean.

Usage

## S3 method for class 'ssMRCD'
residuals(object, ...)

Arguments

object

An object of class "ssMRCD", typically the result of ssMRCD.

...

Additional arguments, see Details.

Details

The function supports several modes of use, controlled by the type argument in ...:

type

"residuals" (default), "trimmed_mean", or "additional_data".

X

A numeric matrix of new observations to compute residuals for. Required if type = "additional_data".

groups

A vector of group assignments for the new data in X. Required if type = "additional_data".

alpha

A numeric value (default taken from the ssMRCD object if missing) indicating the quantile for trimmed mean calculation. Only used if type = "trimmed_mean".

Notes:

  • If type = "residuals", residuals are computed for the original data stored in the ssMRCD object.

  • If type = "additional_data", both X and groups must be provided. All residuals of X are returned (i.e., alpha = 1 is used internally).

  • If type = "trimmed_mean", the mean of the alpha proportion of smallest residual norms is returned. This is also used for parameter tuning.

Value

Depending on the type:

"residuals" or "additional_data"

A numeric matrix of residuals.

"trimmed_mean"

A single numeric value: the trimmed mean of residual norms.

See Also

ssMRCD

Examples

# Create data
x1 <- matrix(runif(200), ncol = 2)
x2 <- matrix(rnorm(200), ncol = 2)
x <- list(x1, x2)

# Define neighborhood weights
W <- matrix(c(0, 1, 1, 0), ncol = 2)

# Compute ssMRCD
localCovs <- ssMRCD(x, weights = W, lambda = 0.5)

# Residuals for original data (all)
head(residuals(localCovs, type = "residuals"))

# Trimmed mean of residual norms
residuals(localCovs, type = "trimmed_mean", alpha = 0.8)

# Residuals for new data
newX <- matrix(rnorm(20), ncol = 2, nrow = 10)
newGroups <- rep(2, 10)
residuals(localCovs, type = "additional_data", X = newX, groups = newGroups)


ssMRCD documentation built on Nov. 5, 2025, 7:44 p.m.