Skip to contents

Computes a robust covariance matrix by calculating pairwise covariances between columns, excluding outliers identified via Z-score thresholding. For each pair of variables, rows containing outliers in either variable are excluded from the covariance calculation. Diagonal entries are filled with the variance of each column after excluding its own outliers.

Usage

robust_pairwise_cov_zscore(data, z_thresh = 3)

Arguments

data

A numeric matrix or data frame where rows are observations and columns are variables.

z_thresh

A numeric value specifying the Z-score threshold for outlier detection. Observations with absolute Z-scores greater than this value are considered outliers. Default is 3.

Value

A symmetric covariance matrix with robust pairwise estimates.

Examples

set.seed(123)
data <- matrix(rnorm(100 * 5), ncol = 5)
data[1:5, ] <- data[1:5, ] + rnorm(5 * 5, sd = 10)  # Inject outliers
cov_matrix <- robust_pairwise_cov_zscore(data)
print(cov_matrix)
#>             V1          V2          V3          V4          V5
#> V1  1.87918786  0.16082475  0.02540392 -0.03690512 -0.40304445
#> V2  0.16082475  1.00996966  0.04786458  0.07554849 -0.16096648
#> V3  0.02540392  0.04786458  1.04540342 -0.09561069 -0.15953374
#> V4 -0.03690512  0.07554849 -0.09561069  1.21467564  0.01297998
#> V5 -0.40304445 -0.16096648 -0.15953374  0.01297998  1.12674036