| Title: | Statistical Significance Testing for Principal Components |
|---|---|
| Description: | Identifies principal components whose eigenvalues exceed those expected under noise. Implements analytical thresholds derived from the Marchenko-Pastur distribution (Marchenko and Pastur, 1967) <doi:10.1070/SM1967v001n04ABEH001994> and empirical permutation tests, and provides functions for visualizing observed and null eigenvalue spectra. |
| Authors: | Guillermo de Anda-Jáuregui [aut, cre, cph], Enrique Hernández-Lemus [aut, cph] |
| Maintainer: | Guillermo de Anda-Jáuregui <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-09-22 09:59:41 UTC |
| Source: | https://github.com/guillermodeandajauregui/sigpca |
Compute Eigenvalues of the Covariance Matrix
compute_eigenvalues(data)compute_eigenvalues(data)
data |
A numeric matrix or data frame. Observations in rows, features in columns. |
A numeric vector of eigenvalues, sorted decreasingly.
set.seed(123) mat <- matrix(rnorm(50), nrow = 10, ncol = 5) compute_eigenvalues(mat)set.seed(123) mat <- matrix(rnorm(50), nrow = 10, ncol = 5) compute_eigenvalues(mat)
Computes the lower and upper bounds of the Marchenko–Pastur distribution, based on the aspect ratio of the data matrix.
marchenko_pastur_bounds(p, n)marchenko_pastur_bounds(p, n)
p |
Number of features (columns) |
n |
Number of observations (rows) |
A named list with lambda_min and lambda_max.
bounds <- marchenko_pastur_bounds(p = 10, n = 100) print(bounds)bounds <- marchenko_pastur_bounds(p = 10, n = 100) print(bounds)
Visualizes the eigenvalue spectrum of a PCA, highlighting significant components based on Marchenko–Pastur bounds.
plot_sigPCA(eigenvalues, mp_bounds, highlight = NULL, add_null = NULL)plot_sigPCA(eigenvalues, mp_bounds, highlight = NULL, add_null = NULL)
eigenvalues |
Numeric vector of eigenvalues from the real data. |
mp_bounds |
A list with |
highlight |
Logical vector (same length as eigenvalues). Defaults to eigenvalues > lambda_max. |
add_null |
Optional numeric vector of null eigenvalues (e.g. from permutation). Default: NULL. |
A ggplot2 object.
set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_mp(x) plot_sigPCA(result$eigenvalues, result$mp_bounds)set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_mp(x) plot_sigPCA(result$eigenvalues, result$mp_bounds)
PCA eigenvalue histogram, showing the empirical eigenvalue distribution together with the Marchenko-Pastur theoretical bounds. Original design by Enrique Hernandez Lemus
plot_sigPCA_histogram(x, mp_bounds = NULL, bins = 30)plot_sigPCA_histogram(x, mp_bounds = NULL, bins = 30)
x |
A result from |
mp_bounds |
Optional list with |
bins |
Number of histogram bins. Default: 30. |
A ggplot2 object.
set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_mp(x) plot_sigPCA_histogram(result)set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_mp(x) plot_sigPCA_histogram(result)
Runs significance testing on PCA eigenvalues using either Marchenko–Pastur bounds, permutation tests, or both. Results are returned as a structured list.
sigPCA( data, method = c("mp", "perm", "both"), num_permutations = 1000, center = TRUE, scale. = TRUE )sigPCA( data, method = c("mp", "perm", "both"), num_permutations = 1000, center = TRUE, scale. = TRUE )
data |
A numeric matrix or data frame. |
method |
One of |
num_permutations |
Integer. Used only if |
center, scale.
|
Logical. Passed to |
A structured list containing method results.
set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) sigPCA(x, method = "mp") sigPCA(x, method = "perm", num_permutations = 100) sigPCA(x, method = "both", num_permutations = 100)set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) sigPCA(x, method = "mp") sigPCA(x, method = "perm", num_permutations = 100) sigPCA(x, method = "both", num_permutations = 100)
Tests which principal components have eigenvalues beyond the theoretical bounds of the Marchenko–Pastur distribution.
sigPCA_mp(data, center = TRUE, scale. = TRUE)sigPCA_mp(data, center = TRUE, scale. = TRUE)
data |
A numeric matrix or data frame. Observations in rows, variables in columns. |
center |
Logical. Whether to center the variables. Default: TRUE. |
scale. |
Logical. Whether to scale variables to unit variance. Default: TRUE. |
A list with:
All eigenvalues of the covariance matrix
A list with lambda_min and lambda_max
Indices of eigenvalues > lambda_max
set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_mp(x) result$significant_componentsset.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_mp(x) result$significant_components
Assesses the statistical significance of PCA components using column-wise permutation of the input data to create a null distribution of eigenvalues.
sigPCA_perm(data, num_permutations = 1000, center = TRUE, scale. = TRUE)sigPCA_perm(data, num_permutations = 1000, center = TRUE, scale. = TRUE)
data |
A numeric matrix or data frame. |
num_permutations |
Number of permutations to generate. Default: 1000. |
center |
Logical. Whether to center variables before analysis. Default: TRUE. |
scale. |
Logical. Whether to scale variables before analysis. Default: TRUE. |
A list with:
Observed eigenvalues
Matrix of eigenvalues from permutations
Empirical p-values per component
Indices where p < 0.05
set.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_perm(x, num_permutations = 100) result$significant_componentsset.seed(123) x <- matrix(rnorm(100 * 10), nrow = 100, ncol = 10) result <- sigPCA_perm(x, num_permutations = 100) result$significant_components