From 9f8518e1ef17fd23b5923bc37a50ccddcbeb299d Mon Sep 17 00:00:00 2001 From: Chris Sobczak Date: Wed, 10 Jun 2026 20:02:05 -0700 Subject: Add stability_score and signal to noise ratio plot functions --- .gitignore | 3 ++- DESCRIPTION | 14 +++++++++++--- R/plot_snr.R | 39 +++++++++++++++++++++++++++++++++++++++ R/stability_score.R | 26 ++++++++++++++++++++++++++ man/plot_snr.Rd | 20 ++++++++++++++++++++ man/stability_score.Rd | 21 +++++++++++++++++++++ 6 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 R/plot_snr.R create mode 100644 R/stability_score.R create mode 100644 man/plot_snr.Rd create mode 100644 man/stability_score.Rd diff --git a/.gitignore b/.gitignore index 300da48..2c69b7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -src/*.{so,o} +src/*.so +src/*.o diff --git a/DESCRIPTION b/DESCRIPTION index 37c233f..8ded430 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,14 +1,22 @@ Package: RSC Type: Package -Title: Robust and Stability Based Sparse Correlation Matrix +Title: Fork of Robust and Sparse Correlation Matrix Description: Fork of the original RSC. Performs robust and sparse correlation matrix estimation. Robustness is achieved based on a simple robust pairwise correlation estimator, while sparsity is obtained based on a new stability based thresholding method. The original thresholding is tuned via cross-validation. See Serra, Coretto, Fratello and Tagliaferri (2018) . Authors@R: person("Chris", "Sobczak", role = c("cre", "aut"), email = "csobczak@sfu.ca", comment = c(ORCID = "0009-0003-3398-2792")) Author: Chris Sobczak [cre, aut] Maintainer: Chris Sobczak NeedsCompilation: yes -Imports: stats, graphics, Matrix, methods, parallel, foreach, - doParallel, utils +Imports: + stats, + graphics, + Matrix, + methods, + parallel, + foreach, + doParallel, + utils, + ggplot2 Version: 2.0.5 Date: 2026-06-10 License: GPL (>= 2) diff --git a/R/plot_snr.R b/R/plot_snr.R new file mode 100644 index 0000000..3fb1f9c --- /dev/null +++ b/R/plot_snr.R @@ -0,0 +1,39 @@ +#' Plot Signal to Noise Ratio +#' +#' Compute the stability scores for each entry and plot it +#' against the estimate correlation coefficients. +#' +#' @param X The data matrix to operate on +#' @param method Required to select a correlation method +#' +#' @return ggplot object +plot_snr <- function( + X, + method = c('rmad', 'pearson', 'spearman', 'kendall') +) { + method <- match.arg(method) + # Compute reference correlation + R <- if(method == 'rmad'){ + rmad(X) + }else{ + cor(X, method = method) + } + + W <- stability_score(X) + + idx <- which(upper.tri(W), arr.ind = TRUE) + w <- W[upper.tri(W)] + r <- R[upper.tri(R)] + + df <- data.frame(r = r, w = w) + + ggplot2::ggplot(data = df, ggplot2::aes(x = r, y = w)) + + ggplot2::geom_point(alpha = 0.5) + + ggplot2::geom_smooth(se = FALSE) + + ggplot2::labs( + title = paste('Weighted vs', method, 'correlation'), + x = paste(method, 'correlation'), + y = 'Weight' + ) + + ggplot2::theme_classic() +} diff --git a/R/stability_score.R b/R/stability_score.R new file mode 100644 index 0000000..7841214 --- /dev/null +++ b/R/stability_score.R @@ -0,0 +1,26 @@ +#' Stability Score +#' +#' Computing entry wise stability score for selection +#' +#' @param X The data matrix to operate on +#' @param K Number of k-folds (default 25) +#' @param subsample_fraction Hold out fraction (default 0.7) +#' +#' @return Score array +#' @export +stability_score <- function(X, K = 25, subsample_fraction = 0.7){ + n <- nrow(X) + p <- ncol(X) + rho <- vector('list', K) + for(r in seq_len(K)){ + idx <- sample(n, size = floor(subsample_fraction * n)) + rho[[r]] <- cor(X[idx, ]) + } + R <- simplify2array(rho) + theta <- acos(pmax(pmin(R, 1), -1)) + delta <- theta - pi/2 + delta_sd <- apply(delta, c(1, 2), sd) + delta_sd <- pmax(delta_sd, quantile(delta_sd[upper.tri(delta_sd)], 0.05)) + score <- 1 / delta_sd + score / median(score[upper.tri(score)]) +} diff --git a/man/plot_snr.Rd b/man/plot_snr.Rd new file mode 100644 index 0000000..177b6ed --- /dev/null +++ b/man/plot_snr.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_snr.R +\name{plot_snr} +\alias{plot_snr} +\title{Plot Signal to Noise Ratio} +\usage{ +plot_snr(X, method = c("rmad", "pearson", "spearman", "kendall")) +} +\arguments{ +\item{X}{The data matrix to operate on} + +\item{method}{Required to select a correlation method} +} +\value{ +ggplot object +} +\description{ +Compute the stability scores for each entry and plot it +against the estimate correlation coefficients. +} diff --git a/man/stability_score.Rd b/man/stability_score.Rd new file mode 100644 index 0000000..c95ee8e --- /dev/null +++ b/man/stability_score.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/stability_score.R +\name{stability_score} +\alias{stability_score} +\title{Stability Score} +\usage{ +stability_score(X, K = 25, subsample_fraction = 0.7) +} +\arguments{ +\item{X}{The data matrix to operate on} + +\item{K}{Number of k-folds (default 25)} + +\item{subsample_fraction}{Hold out fraction (default 0.7)} +} +\value{ +Score array +} +\description{ +Computing entry wise stability score for selection +} -- cgit v1.2.3