-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
76 lines (53 loc) · 2 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# sparseLRMatrix
<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/gh/RoheLab/sparseLRMatrix/branch/master/graph/badge.svg)](https://codecov.io/gh/RoheLab/sparseLRMatrix?branch=master)
[![R-CMD-check](https://github.com/RoheLab/sparseLRMatrix/workflows/R-CMD-check/badge.svg)](https://github.com/RoheLab/sparseLRMatrix/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/sparseLRMatrix)](https://CRAN.R-project.org/package=sparseLRMatrix)
<!-- badges: end -->
`sparseLRMatrix` provides a single matrix S4 class called `sparseLRMatrix` which represents matrices that can be expressed as the sum of sparse matrix and a low rank matrix. We also provide an efficient SVD method for these matrices by wrapping the `RSpectra` SVD implementation.
Eventually, we will fully subclass `Matrix::Matrix` objects, but the current implementation is extremely minimal.
## Installation
You can install the released version of sparseLRMatrix from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("sparseLRMatrix")
```
You can install the development version with:
``` r
# install.packages("remotes")
remotes::install_github("RoheLab/sparseLRMatrix")
```
## Usage
```{r example}
library(sparseLRMatrix)
library(RSpectra)
set.seed(528491)
n <- 50
m <- 40
k <- 3
A <- rsparsematrix(n, m, 0.1)
U <- Matrix(rnorm(n * k), nrow = n, ncol = k)
V <- Matrix(rnorm(m * k), nrow = m, ncol = k)
# construct the matrix, which represents A + U %*% t(V)
X <- sparseLRMatrix(sparse = A, U = U, V = V)
s <- svds(X, 5) # efficient
```
And a quick sanity check
```{r}
Y <- A + tcrossprod(U, V)
s2 <- svds(Y, 5) # inefficient, but same calculation
# singular values match up, you can check for yourself
# that the singular vectors do as well!
all.equal(s$d, s2$d)
```