-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
110 lines (75 loc) · 3.22 KB
/
README.Rmd
File metadata and controls
110 lines (75 loc) · 3.22 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
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%"
)
```
# invertiforms
<!-- badges: start -->
[](https://github.com/RoheLab/invertiforms/actions)
[](https://app.codecov.io/gh/RoheLab/invertiforms?branch=master)
<!-- badges: end -->
The goal of invertiforms is to provide composable, invertible transformations of sparse matrices. These transformations are primarily useful for network analysis. For example, we provide transforms to row and column center a graph adjacency matrix. This transformation could then be composed with a degree-normalized transformation, and so on. The original adjacency matrix can be recovered by applying inverse transformation. Primarily, this package is for other developers working on spectral matrix packages, but it may also be useful for advanced users who don't want to go to the hassle of coding up matrix transformations on their own.
## Installation
You can install the released version of invertiforms from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("invertiforms")
```
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("RoheLab/invertiforms")
```
## Transformations
At the moment, we provide convenient tools to:
- row center,
- column center, and
- row and column center
a matrix. Similarly, we provide tools to transform a graph adjacency matrix into the:
- normalized graph Laplacian,
- regularized graph Laplacian, and
- perturbed graph Laplacian.
## Example usage
Here we show how `invertiforms` might help you quickly perform regularized spectral clustering. First we grab some network data and quickly visualize the adjacency matrix.
```{r}
library(invertiforms)
library(igraph)
library(igraphdata)
data("karate", package = "igraphdata")
A <- get.adjacency(karate)
image(A)
```
Now we construct the regularized degree normalized graph Laplacian and visualize it.
```{r}
iform <- RegularizedLaplacian(A, tau_row = 35, tau_col = 35)
L <- transform(iform, A)
image(L)
```
Recovering `A` from `L` is straightforward:
```{r}
A_recovered <- inverse_transform(iform, L)
all.equal(A, A_recovered, check.attributes = FALSE)
```
Once we have `L` we can do spectral tricks with it. Here we estimate `k = 2` clusters.
```{r}
set.seed(27)
k <- 2
L_svd <- svd(L, k)
safe_project_sphere_rowwise <- function(x, eps = 1e-10) {
sc <- drop(apply(x, 1L, function(y) sum(y^2)))
sc[sc < eps] <- 1
x / sqrt(sc)
}
U <- safe_project_sphere_rowwise(L_svd$u)
km <- kmeans(U, k, nstart = 50)
V(karate)$color <- km$cluster
plot(karate)
```
## Related work
[`recipes`](https://cran.r-project.org/package=recipes) provides non-invertible, composable transformations for tabular data. [`softImpute`](https://cran.r-project.org/package=softImpute) provides an `Incomplete` S4 Matrix class that can be used for double centering sparse matrices.