sparsePCA: Sparse Principal Components Analysis

View source: R/SparcePCA.R

sparsePCAR Documentation

Sparse Principal Components Analysis

Description

Estimate sparse sparse principal components via SPCA according to \insertRefzou2006sparseTwoStepSDFM.

Usage

sparsePCA(
  data,
  delay,
  selected,
  no_of_factors,
  ridge_penalty = 1e-06,
  lasso_penalty = NULL,
  max_iterations = 1000,
  weights = NULL,
  max_no_steps = NULL,
  comp_null = 1e-15,
  spca_conv_crit = 1e-04,
  parallel = FALSE,
  svd_method = "precise",
  normalise = TRUE,
  comp_var_expl = TRUE
)

Arguments

data

Numeric (no_of_vars \times no_of_obs) matrix of data or zoo/xts object sampled at the same frequency.

delay

Integer vector of variable delays, measured as the number of months since the latest available observation.

selected

Integer vector of the number of selected variables for each factor.

no_of_factors

Integer number of factors.

ridge_penalty

Numeric ridge penalty.

lasso_penalty

Numeric vector, lasso penalties for each factor (set to NULL to disable as stopping criterion).

max_iterations

Integer maximum number of iterations.

weights

Numeric vector, weights for each variable weighing the \ell_1 size constraint.

max_no_steps

Integer number of LARS steps (set to NULL to disable as stopping criterion).

comp_null

Numeric computational zero.

spca_conv_crit

Conversion threshold for the SPCA algorithm.

parallel

Logical, whether or not to use Eigen's internal parallel matrix operations.

svd_method

Either "fast" or "precise". Option "fast" uses Eigen's BDCSVD divide and conquer method for the computation of the singular values. Option "precise" (default) implements the slower, but numerically more stable JacobiSVD method.

normalise

Logical, whether to normalise the loading matrix as in \insertRefzou2020elnetTwoStepSDFM. Default is TRUE.

comp_var_expl

Logical, whether to compute the relative variance explained by each factor. Default is ⁠TRUE``.⁠

Details

The function takes three stopping criteria: selected, lasso_penalty, and max_no_steps. With selected the SPCA algorithm stops if each column of the estimated loading matrix has the corresponding number of non-zero loadings. This allows the user to directly control the degree of sparsity of each factor loading. With lasso_penalty, the SPCA algorithm stops as soon as the side-constraints of the inherent elastic-net problem are no longer satisfied. With max_no_steps, the SPCA algorithm only takes that many LARS steps for each factor loading's individual elastic-net problem before stopping. If all criteria are provided, the first one satisfied will stop the algorithm. For details see also \insertCitezou2006sparseTwoStepSDFM and \insertCitezou2020elnetTwoStepSDFM.

Loosely, each SPCA algorithm iteration solves an elastic-net type problem for each column of the loading matrix. One can extend this problem to the adaptive elastic-net \insertCitezou2009adaptiveTwoStepSDFM. The variable weights lets the user provide weights for each observation. These weights must be strictly greater than zero and are normalised internally to represent relative weights. For more information on the computational implementation of the weight extension in the context of SPCA see \insertRefzou2024generalTwoStepSDFM.

In each SPCA algorithm iteration, the function executes an SVD. To this end, Eigen provides two alternatives \insertCiteeigenwebTwoStepSDFM: Option precise makes use of JacobiSVD. This method is numerically more stable, but computationally costly, especially for medium to large matrices. Option fast makes use of BDCSVD. This divide-and-conquer approach can lead to significant performance gains with respect to large matrices. BDCSVD, however, can be numerically unstable when Eigen is compiled with aggressive speed optimisations. In the context of the R, this should be of no concern. By default, R and most packages are compiled with "mild" -O2 optimisation and without any additional aggressive optimisation flags. Nonetheless, one should checker whether both variants provide reasonably close results before switching to fast. For more information see \insertRefeigenwebTwoStepSDFM.

Value

An object of class SPCAFit with components:

data

Original data matrix.

loading_matrix_estim

Numeric matrix of estimated factor loadings.

factor_estim

Object containing the SPCA factor estimates. The object inherits its class from data: If data is provided as zoo, factor_estim will be a zoo object. If data is provided as matrix, factor_estim will be a (no_of_factors \times no_of_obs) matrix.

total_var_expl

Numeric total variance explained.

pct_var_expl

Numeric vector relative variance explained by each factor.

Author(s)

Domenic Franjic

References

\insertRef

zou2006sparseTwoStepSDFM

\insertRef

zou2009adaptiveTwoStepSDFM

\insertRef

eigenwebTwoStepSDFM

\insertRef

zou2020elnetTwoStepSDFM

\insertRef

zou2024generalTwoStepSDFM

Examples

data(factor_model)
set.seed(17032026)
no_of_factors <- 3
no_of_vars <- dim(factor_model$data)[2]
selected <- rep(floor(0.5 * no_of_vars), no_of_factors)
lasso_penalty <- exp(runif(no_of_factors, -10, 1))
max_no_steps <- 1000
spca_fit <- sparsePCA(data = factor_model$data, delay = factor_model$delay, 
                      selected = selected, no_of_factors = no_of_factors, 
                      ridge_penalty = 1e-2, lasso_penalty = lasso_penalty,
                      max_iterations = 1000, weights = NULL, 
                      max_no_steps = max_no_steps, comp_null = 1e-15,
                      spca_conv_crit = 1e-04, parallel = FALSE, 
                      svd_method = "precise", normalise = FALSE,
                      comp_var_expl = TRUE)
print(spca_fit)
spca_plots <- plot(spca_fit)
spca_plots$`Factor Time Series Plots`
spca_plots$`Loading Matrix Heatmap`
spca_plots$`Meas. Error Var.-Cov. Matrix Heatmap`
spca_plots$`Eigenvalue Plot`
spca_plots$`Variance Explained Chart`


TwoStepSDFM documentation built on May 19, 2026, 9:07 a.m.