{"id":13700094,"url":"https://github.com/dppalomar/sparseEigen","last_synced_at":"2025-05-04T18:34:15.912Z","repository":{"id":62459174,"uuid":"110079169","full_name":"dppalomar/sparseEigen","owner":"dppalomar","description":"Computation of Sparse Eigenvectors of a Matrix","archived":false,"fork":false,"pushed_at":"2018-12-22T15:14:43.000Z","size":36771,"stargazers_count":13,"open_issues_count":2,"forks_count":10,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T17:02:39.584Z","etag":null,"topics":["covariance-matrix","eigenvectors","pca","sparse"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dppalomar.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-09T06:57:48.000Z","updated_at":"2025-03-28T22:01:51.000Z","dependencies_parsed_at":"2022-11-02T00:45:27.803Z","dependency_job_id":null,"html_url":"https://github.com/dppalomar/sparseEigen","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FsparseEigen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FsparseEigen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FsparseEigen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FsparseEigen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dppalomar","download_url":"https://codeload.github.com/dppalomar/sparseEigen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252382911,"owners_count":21739240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["covariance-matrix","eigenvectors","pca","sparse"],"created_at":"2024-08-02T20:00:48.520Z","updated_at":"2025-05-04T18:34:10.920Z","avatar_url":"https://github.com/dppalomar.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":["Numerical Libraries \u0026 Data Structures","数值库与数据结构"],"readme":"---\noutput:\n  md_document:\n    variant: markdown_github\n  html_document:\n    variant: markdown_github\n    keep_md: true\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, echo = FALSE}\nlibrary(knitr)\nopts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  fig.align = \"center\",\n  fig.retina = 2,\n  out.width = \"75%\",\n  dpi = 96\n)\nknit_hooks$set(pngquant = hook_pngquant)\n```\n\n# sparseEigen\n\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/sparseEigen)](http://cran.r-project.org/package=sparseEigen)\n[![CRAN Downloads](http://cranlogs.r-pkg.org/badges/sparseEigen)](http://cran.r-project.org/package=sparseEigen)\n![CRAN Downloads Total](http://cranlogs.r-pkg.org/badges/grand-total/sparseEigen?color=brightgreen)\n\nComputation of sparse eigenvectors of a matrix (aka sparse PCA)\n    with running time 2-3 orders of magnitude lower than existing methods and\n    better final performance in terms of recovery of sparsity pattern and \n    estimation of numerical values. \n    \nCan handle covariance matrices as well as data matrices with real or \n    complex-valued entries. Different levels of sparsity can be specified \n    for each individual ordered eigenvector and the method is robust in \n    parameter selection. See vignette for a detailed documentation and \n    comparison, with several illustrative examples. \n    \nThe package is based on the paper:\n\nK. Benidis, Y. Sun, P. Babu, and D. P. Palomar, \"Orthogonal Sparse PCA and \nCovariance Estimation via Procrustes Reformulation,\" _IEEE Transactions on \nSignal Processing_, vol. 64, no. 23, pp. 6211-6226, Dec. 2016. \n(\u003chttps://doi.org/10.1109/TSP.2016.2605073\u003e)\n\n\n\n## Installation\n```{r, eval = FALSE}\n# Installation from CRAN\ninstall.packages(\"sparseEigen\")\n\n# Installation from GitHub\n# install.packages(\"devtools\")\ndevtools::install_github(\"dppalomar/sparseEigen\")\n\n# Getting help\nlibrary(sparseEigen)\nhelp(package = \"sparseEigen\")\npackage?sparseEigen\n?spEigen\n\n# Citing this work\ncitation(\"sparseEigen\")\n```\n\n## Vignette\nFor more detailed information, please check the vignette: [GitHub-html-vignette](https://rawgit.com/dppalomar/sparseEigen/master/vignettes/SparseEigenvectors-vignette.html), [GitHub-pdf-vignette](https://rawgit.com/dppalomar/sparseEigen/master/vignettes/SparseEigenvectors-vignette.pdf),\n[CRAN-pdf-vignette](https://cran.r-project.org/web/packages/sparseEigen/vignettes/SparseEigenvectors.pdf).\n\n## Usage of `spEigen()`\nWe start by loading the package and generating synthetic data with sparse eigenvectors:\n```{r}\nlibrary(sparseEigen)\nset.seed(42)\n\n# parameters \nm \u003c- 500  # dimension\nn \u003c- 100  # number of samples\nq \u003c- 3  # number of sparse eigenvectors to be estimated\nsp_card \u003c- 0.1*m  # cardinality of each sparse eigenvector\n\n# generate non-overlapping sparse eigenvectors\nV \u003c- matrix(0, m, q)\nV[cbind(seq(1, q*sp_card), rep(1:q, each = sp_card))] \u003c- 1/sqrt(sp_card)\nV \u003c- cbind(V, matrix(rnorm(m*(m-q)), m, m-q))\n# keep first q eigenvectors the same (already orthogonal) and orthogonalize the rest\nV \u003c- qr.Q(qr(V))  \n\n# generate eigenvalues\nlmd \u003c- c(100*seq(from = q, to = 1), rep(1, m-q))\n\n# generate covariance matrix from sparse eigenvectors and eigenvalues\nR \u003c- V %*% diag(lmd) %*% t(V)\n\n# generate data matrix from a zero-mean multivariate Gaussian distribution \n# with the constructed covariance matrix\nX \u003c- MASS::mvrnorm(n, rep(0, m), R)  # random data with underlying sparse structure\n```\nThen, we estimate the covariance matrix with `cov(X)` and compute its sparse eigenvectors:\n```{r, cache = TRUE}\n# computation of sparse eigenvectors\nres_standard \u003c- eigen(cov(X))\nres_sparse \u003c- spEigen(cov(X), q)\n```\n\nWe can assess how good the estimated eigenvectors are by computing the inner product with the original eigenvectors (the closer to 1 the better):\n```{r}\n# show inner product between estimated eigenvectors and originals\nabs(diag(t(res_standard$vectors) %*% V[, 1:q]))  #for standard estimated eigenvectors\nabs(diag(t(res_sparse$vectors) %*% V[, 1:q]))    #for sparse estimated eigenvectors\n```\n\nFinally, the following plot shows the sparsity pattern of the eigenvectors (sparse computation vs. classical computation):\n```{r, echo = FALSE, fig.width = 7, fig.height = 7, pngquant = \"--speed=1\"}\npar(mfcol = c(3, 2))\nplot(res_sparse$vectors[, 1]*sign(res_sparse$vectors[1, 1]), \n     main = \"First sparse eigenvector\", xlab = \"index\", ylab = \"\", type = \"h\")\nlines(V[, 1]*sign(V[1, 1]), col = \"red\")\nplot(res_sparse$vectors[, 2]*sign(res_sparse$vectors[sp_card+1, 2]), \n     main = \"Second sparse eigenvector\", xlab = \"index\", ylab = \"\", type = \"h\")\nlines(V[, 2]*sign(V[sp_card+1, 2]), col = \"red\")\nplot(res_sparse$vectors[, 3]*sign(res_sparse$vectors[2*sp_card+1, 3]), \n     main = \"Third sparse eigenvector\", xlab = \"index\", ylab = \"\", type = \"h\")\nlines(V[, 3]*sign(V[2*sp_card+1, 3]), col = \"red\")\n\nplot(res_standard$vectors[, 1]*sign(res_standard$vectors[1, 1]), \n     main = \"First regular eigenvector\", xlab = \"index\", ylab = \"\", type = \"h\")\nlines(V[, 1]*sign(V[1, 1]), col = \"red\")\nplot(res_standard$vectors[, 2]*sign(res_standard$vectors[sp_card+1, 2]), \n     main = \"Second regular eigenvector\", xlab = \"index\", ylab = \"\", type = \"h\")\nlines(V[, 2]*sign(V[sp_card+1, 2]), col = \"red\")\nplot(res_standard$vectors[, 3]*sign(res_standard$vectors[2*sp_card+1, 3]), \n     main = \"Third regular eigenvector\", xlab = \"index\", ylab = \"\", type = \"h\")\nlines(V[, 3]*sign(V[2*sp_card+1, 3]), col = \"red\")\n```\n\n## Usage of `spEigenCov()`\n\nThe function `spEigenCov()` requires more samples than the dimension (otherwise some regularization is required). Therefore, we generate data as previously with the only difference that we set the number of samples to be `n=600`.\n\n```{r, echo = FALSE}\nn \u003c- 600  # number of samples\nX \u003c- MASS::mvrnorm(n, rep(0, m), R)  # random data with underlying sparse structure\n```\n\nThen, we compute the covariance matrix through the joint estimation of sparse eigenvectors and eigenvalues:\n```{r}\n# computation of covariance matrix\nres_sparse2 \u003c- spEigenCov(cov(X), q)\n```\n\nAgain, we can assess how good the estimated eigenvectors are by computing the inner product with the original eigenvectors:\n```{r}\n# show inner product between estimated eigenvectors and originals\nabs(diag(t(res_sparse2$vectors[, 1:q]) %*% V[, 1:q]))    #for sparse estimated eigenvectors\n```\n\nFinally, we can compute the error of the estimated covariance matrix (sparse eigenvector computation vs. classical computation):\n```{r}\n# show error between estimated and true covariance \nnorm(cov(X) - R, type = 'F') #for sample covariance matrix\nnorm(res_sparse2$cov - R, type = 'F') #for covariance with sparse eigenvectors\n```\n\n## Links\nPackage: [CRAN](https://CRAN.R-project.org/package=sparseEigen) and [GitHub](https://github.com/dppalomar/sparseEigen).\n\nREADME file: [GitHub-readme](https://rawgit.com/dppalomar/sparseEigen/master/README.html) and [CRAN-readme](https://cran.r-project.org/web/packages/sparseEigen/README.html).\n\nVignette: [GitHub-html-vignette](https://rawgit.com/dppalomar/sparseEigen/master/vignettes/SparseEigenvectors-vignette.html), [GitHub-pdf-vignette](https://rawgit.com/dppalomar/sparseEigen/master/vignettes/SparseEigenvectors-vignette.pdf),\n[CRAN-pdf-vignette](https://cran.r-project.org/web/packages/sparseEigen/vignettes/SparseEigenvectors.pdf).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdppalomar%2FsparseEigen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdppalomar%2FsparseEigen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdppalomar%2FsparseEigen/lists"}