Robust Pairwise Covariance Matrix Using Z-score Outlier Removal
Source:R/robust_pairwise_cov_zscore.R
robust_pairwise_cov_zscore.RdComputes 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.
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