An R Package for Sparse PCA with Multiple Principal Components
This package can be installed from CRAN directly:
install.packages("msPCA")
Alternatively, it can be installed from this Github repository using the devtools package. You would first need to install devtools:
install.packages("devtools")
and then run the following commands:
library(devtools)
install_github('jeanpauphilet/msPCA')
The package consists of one main function, msPCA, which takes as input:
- a data matrix (either the correlation or covariance matrix of the dataset),
- the number of principal components (PCs) to be computed, r,
- a list of r integers corresponding to the sparsity of each PC.
It returns an object with 4 fields
- x_best (p x r array containing the sparse PCs),
- objective_value
- feasibility_violation
- runtime.
Here is a short example demonstrating how to use the package. First, you need to load the library.
library(msPCA)
Then, define the input variables.
library(datasets)
df <- datasets::mtcars
TestMat <- cor(df)
And then simply call the function
mspca(TestMat, 2, c(4,4))
Here, we provide more information about the code structure and organization to help developers that would like to improve the method or build up on it.
Rcpp::compileAttributes().devtools::document() command. Rcpp::compileAttributes().Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.