sparsepca-package: Sparse principal component analysis via variable projection

sparsepca-packageR Documentation

Sparse principal component analysis via variable projection

Description

Fit sparse principal components with deterministic, randomized, or robust algorithms. The package also supports prediction, regularization paths, and searches for an approximate number of selected variables.

Details

Principal components summarize many measured variables in a few score coordinates. Ordinary PCA usually uses every variable in each component, which can obscure what a score represents. Sparse PCA permits some coefficients to be exactly zero, giving each score a shorter list of contributing measurements. This exchanges some reconstruction quality for a representation that may be easier to inspect and use.

Rows of the input are observations and columns are variables. For the centered and/or scaled data X, the fitted scores are XB, where B contains sparse coefficients. A separate orthogonal factor A reconstructs the data as XBA^T. Both factors have p rows and k columns, and the score matrix has n rows and k columns. The choice of orientation matters: transposing the data changes which objects get sparse coefficients.

The functions preserve the fitted coefficient magnitudes. Scores may be correlated, and the historical eigenvalues output describes adjusted score energies rather than general covariance eigenvalues. The spca topic develops the mathematical model, preprocessing, parameter scaling, output definitions, and these interpretation conventions in detail.

Requirements

The package requires R 4.4.0 or later, Matrix 1.7-0 or later, and rsvd 2.0.0 or later. The R and Matrix requirements match those of rsvd 2.0.0. Earlier versions are no longer supported.

Choosing a routine

spca

A deterministic fit using the full processed data. It is a useful starting point and numerical reference for manageable dense problems. Sparse input is accepted but converted to a dense workspace.

rspca

A fit to a randomized compressed problem, with full-data scores afterward. It is most useful when a sketch much smaller than the data captures the relevant variation. Sparse Matrix input retains sparse storage for products and implicit preprocessing.

robspca

An additive entrywise sparse-error model with a Huber reconstruction loss. It separates unusually large residual entries from the low-rank part and uses dense workspaces.

spca_path

A sequence of candidate penalties sharing preprocessing and a factorization or sketch, with warm starts.

spca_target

A bounded penalty search returning the closest converged candidate to a desired number of variables.

predict.spca

The fitted training transformation applied to future observations, with no refitting.

Sparse loadings use a subset of variables. Sparse input storage is a separate property of a matrix; only the randomized fitting methods preserve it during centering and scaling. All fits return dense loading and score matrices. A sparse input does not force sparse components, and sparse components do not make a dense input cheap to store.

A practical workflow

Choose the observation/variable orientation, retain numeric measurements, and decide whether centering and scaling match the scientific question. Choose k as the number of score coordinates. Explore the tradeoff between loading sparsity and reconstruction with a short alpha path, keeping the preprocessing fixed. Inspect the actual coefficients and score correlations, not only a rounded summary. When an application suggests a desired number of measurements, spca_target searches for a nearby count and reports the achieved count explicitly. Use training-only preprocessing and predict for validation or downstream predictive models.

Numerical convergence, approximation quality, and statistical usefulness are separate questions. The joint optimization is nonconvex; an objective-based converged flag is not a global-optimality or scientific-validity certificate. A randomized fit additionally needs a sufficiently informative sketch. Sparsity does not itself guarantee stable feature selection, generalization, or causal interpretation.

To make an analysis reproducible, record k, preprocessing, relative penalties, convergence settings, and the chosen model. For randomized methods also record o, q, and the seed; for robust fits record gamma in the processed units. Fitted factors can differ by signs or bases within tied subspaces while representing the same reconstruction.

Author(s)

N. Benjamin Erichson, Peng Zheng, and Sasha Aravkin. Maria A. Sans-Fuentes (ORCID) contributed the report and correction of the robust objective error.

References

Erichson, N. B., Zheng, P., Manohar, K., Brunton, S. L., Kutz, J. N., and Aravkin, A. Y. (2020). Sparse Principal Component Analysis via Variable Projection. SIAM Journal on Applied Mathematics, 80(2), 977–1002. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1137/18M1211350")}.

See Also

The repository https://github.com/erichson/spca#readme provides a tutorial, application illustrations, and reproducible performance experiments. See prcomp for ordinary PCA.

Examples

X <- as.matrix(iris[, 1:4])
fit <- spca(X, k = 2, alpha = 0.01, scale = TRUE, verbose = FALSE)
dim(fit$loadings)  # variables by components
dim(fit$scores)    # observations by components
colSums(fit$loadings != 0)
summary(fit)
predict(fit, X[1:3, , drop = FALSE])


sparsepca documentation built on Nov. 6, 2026, 5:06 p.m.