| Title: | Sparse Principal Component Analysis with Multiple Principal Components |
|---|---|
| Description: | Implements an algorithm for computing multiple sparse principal components of a dataset. The method is based on Cory-Wright and Pauphilet "Sparse PCA with Multiple Principal Components" (2026) <doi:10.48550/arXiv.2209.14790>. The algorithm uses an iterative deflation heuristic with a truncated power method applied at each iteration to compute sparse principal components with controlled sparsity. |
| Authors: | Ryan Cory-Wright [aut, cph] (ORCID: <https://orcid.org/0000-0002-4485-0619>), Jean Pauphilet [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6352-0984>) |
| Maintainer: | Jean Pauphilet <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-05-22 16:27:16 UTC |
| Source: | https://github.com/jeanpauphilet/mspca |
Computes the feasibility violation defined as if orthogonality constraints are enforced (feasibilityConstraintType = 0) and if zero-correlation constraints are enforced (feasibilityConstraintType = 1).
feasibility_violation_off(C, U, feasibilityConstraintType)feasibility_violation_off(C, U, feasibilityConstraintType)
C |
A matrix. The correlation or covariance matrix (p x p). |
U |
A matrix. Each column correspond to an p-dimensional PC. |
feasibilityConstraintType |
An integer. Type of feasibility constraints to be enforced. 0: orthogonality constraints; 1: uncorrelatedness constraints. |
A float.
library(datasets) TestMat <- cor(datasets::mtcars) mspcares <- mspca(TestMat, 2, c(4,4)) feasibility_violation_off(TestMat, mspcares$x_best, 0)library(datasets) TestMat <- cor(datasets::mtcars) mspcares <- mspca(TestMat, 2, c(4,4)) feasibility_violation_off(TestMat, mspcares$x_best, 0)
Computes the fraction of variance explained (variance explained normalized by the trace of the covariance/correlation matrix) by a set of PCs.
fraction_variance_explained(C, U)fraction_variance_explained(C, U)
C |
A matrix. The correlation or covariance matrix (p x p). |
U |
A matrix. The matrix containing the r PCs (p x r). |
A float.
library(datasets) TestMat <- cor(datasets::mtcars) mspcares <- mspca(TestMat, 2, c(4,4)) fraction_variance_explained(TestMat, mspcares$x_best)library(datasets) TestMat <- cor(datasets::mtcars) mspcares <- mspca(TestMat, 2, c(4,4)) fraction_variance_explained(TestMat, mspcares$x_best)
Computes the fraction of variance explained (variance explained normalized by the trace of the covariance/correlation matrix) by each PC.
fraction_variance_explained_perPC(C, U)fraction_variance_explained_perPC(C, U)
C |
A matrix. The correlation or covariance matrix (p x p). |
U |
A matrix. The matrix containing the r PCs (p x r). |
An array.
Returns multiple sparse principal component of a matrix using an iterative deflation heuristic.
mspca( Sigma, r, ks, feasibilityConstraintType = 0L, verbose = TRUE, maxIter = 200L, feasibilityTolerance = 1e-04, stallingTolerance = 1e-08, timeLimitTPM = 20L, maxRestartTPM = 20L, minRestartTPM = 10L )mspca( Sigma, r, ks, feasibilityConstraintType = 0L, verbose = TRUE, maxIter = 200L, feasibilityTolerance = 1e-04, stallingTolerance = 1e-08, timeLimitTPM = 20L, maxRestartTPM = 20L, minRestartTPM = 10L )
Sigma |
A matrix. The correlation or covariance matrix, whose sparse PCs will be computed. |
r |
An integer. Number of principal components (PCs) to be computed. |
ks |
A list of integers. Target sparsity of each PC. |
feasibilityConstraintType |
(optional) An integer. Type of feasibility constraints to be enforced. 0: orthogonality constraints; 1: uncorrelatedness constraints. Default 0. |
verbose |
(optional) A Boolean. Controls console output. Default TRUE. |
maxIter |
(optional) An integer. Maximum number of iterations of the algorithm. Default 200. |
feasibilityTolerance |
(optional) A float. Tolerance for the violation of the orthogonality constraints. Default 1e-4 |
stallingTolerance |
(optional) A float. Controls the objective improvement below which the algorithm is considered to have stalled. Default 1e-8 |
timeLimitTPM |
(optional) An integer. Maximum time in seconds for the truncated power method (inner iteration). Default 20. |
maxRestartTPM |
(optional) An integer. Number of random restarts of the truncated power method (inner iteration) for the first outer iteration. Default 20. |
minRestartTPM |
(optional) An integer. Number of random restarts of the truncated power method (inner iteration) for outer iterations >= 2. Default 10. |
An object with 4 fields: 'x_best' (p x r array containing the sparse PCs), 'objective_value', 'feasibility_violation', 'runtime'.
library(datasets) TestMat <- cor(datasets::mtcars) mspca(TestMat, 2, c(4,4))library(datasets) TestMat <- cor(datasets::mtcars) mspca(TestMat, 2, c(4,4))
Displays the output of the msPCA algorithm.
print_mspca(sol_object, C)print_mspca(sol_object, C)
sol_object |
A list. The output of the mspca or tpm function. |
C |
A matrix. The correlation or covariance matrix (p x p). |
None. Prints output to console.
library(datasets) TestMat <- cor(datasets::mtcars) mspcares <- mspca(TestMat, 2, c(4,4)) print_mspca(mspcares, TestMat)library(datasets) TestMat <- cor(datasets::mtcars) mspcares <- mspca(TestMat, 2, c(4,4)) print_mspca(mspcares, TestMat)
Returns the leading sparse principal component of a matrix using the truncated power method.
tpm(Sigma, k, maxIter = 200L, verbose = TRUE, timeLimit = 10L)tpm(Sigma, k, maxIter = 200L, verbose = TRUE, timeLimit = 10L)
Sigma |
A matrix. The correlation or covariance matrix, whose sparse PCs will be computed. |
k |
An integer. Target sparsity of the PC. |
maxIter |
(optional) An integer. Maximum number of iterations of the algorithm. Default 200. |
verbose |
(optional) A Boolean. Controls console output. Default TRUE. |
timeLimit |
(optional) An integer. Maximum time in seconds. Default 10. |
An object with 3 fields: 'x_best' (p x 1 array containing the sparse PC), 'objective_value', 'runtime'.
Yuan, X. T., & Zhang, T. (2013). Truncated power method for sparse eigenvalue problems. The Journal of Machine Learning Research, 14(1), 899-925.
library(datasets) TestMat <- cor(datasets::mtcars) tpm(TestMat, 4)library(datasets) TestMat <- cor(datasets::mtcars) tpm(TestMat, 4)
Computes the variance explained by each PC.
variance_explained_perPC(C, U)variance_explained_perPC(C, U)
C |
A matrix. The correlation or covariance matrix (p x p). |
U |
A matrix. The matrix containing the r PCs (p x r). |
An array.