The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

msPCA

Sparse PCA with multiple principal components in R.

The msPCA package computes sparse loading vectors that explain a high fraction of variance while controlling non-redundancy across components. It supports two non-redundancy definitions:

Installation

Install from CRAN:

install.packages("msPCA")
library(msPCA)

Install development version from GitHub:

install.packages("devtools")
devtools::install_github("jeanpauphilet/msPCA")
library(msPCA)

Quick start

The main function is mspca().

Inputs (following the elasticnet convention, the data is a single argument M plus a type selector):

With type = "X", mspca() applies the algorithm to the data directly via the products t(X) %*% (X %*% beta) and never forms the p x p matrix. This is substantially faster and more memory-efficient when n << p. Pass type = "X" whenever the number of variables greatly exceeds the number of observations.

Output fields:

Example on mtcars:

library(msPCA)

Sigma <- cor(datasets::mtcars)
set.seed(42)

res <- mspca(Sigma, r = 2, ks = c(4, 4), verbose = FALSE)   # type = "Sigma" is the default
print_mspca(res, Sigma)

feasibility_violation_off(Sigma, res$x_best, feasibilityConstraintType = 0)
fraction_variance_explained(Sigma, res$x_best)

Equivalent workflow from the raw data matrix (no covariance matrix needed):

library(msPCA)

X <- as.matrix(datasets::mtcars)
set.seed(42)

# type = "X" treats the first argument as raw data; scale = TRUE operates on the
# correlation matrix, matching cor(mtcars) above.
res <- mspca(X, r = 2, ks = c(4, 4), type = "X", scale = TRUE, verbose = FALSE)
print_mspca(res)                      # type = "X" results carry their own variance summary

fraction_variance_explained(cor(X), res$x_best)

For datasets with n << p, this raw-data path avoids the O(np^2) cost of forming Sigma and reduces each iteration’s matrix–vector product from O(p^2) to O(np).

Optional dense PCA comparison:

pca_res <- prcomp(datasets::mtcars, scale. = TRUE)
fraction_variance_explained(Sigma, pca_res$rotation[, 1:2])

Interpretation:

See vignette("msPCA") for a worked example built from the same mtcars workflow.

Synthetic benchmark

The script test/notebook_synthetic.R compares msPCA with elasticnet::spca() on synthetic data across sample sizes and exports the figures below.

Orthogonality violation on synthetic data
Out-of-sample fraction of variance explained on synthetic data

To regenerate these files, run test/notebook_synthetic.R from the repository root.

Choosing parameters

Sparsity budgets (ks)

ks is the main tuning input. A practical workflow is to run mspca() for multiple sparsity budgets and evaluate:

Constraint type (feasibilityConstraintType)

Use 0 when loadings are used as a geometric projection basis. Use 1 when statistical decorrelation of component scores is the priority.

Main functions

Useful optional arguments in mspca():

Raw-data arguments (type = "X"):

Covariance-matrix validation arguments (type = "Sigma"):

Diagnostic functions

Citation

If you use msPCA in academic work, please cite the package and the underlying paper.

You can retrieve the package citation in R with:

citation("msPCA")

Reference paper:

@article{cory2026sparse,
  title   = {Sparse PCA with Multiple Principal Components},
  author  = {Cory-Wright, Ryan and Pauphilet, Jean},
  year    = {2026},
  journal = {Operations Research},
  doi     = {10.1287/opre.2023.0598}
}

Development

Package structure overview:

For interface changes, regenerate exports and documentation with Rcpp::compileAttributes() and devtools::document().

License

See LICENSE.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.