MyNixOS website logo
Description

Invertible Transforms for Matrices.

Provides composable invertible transforms for (sparse) matrices.

invertiforms

R-CMD-check Codecov testcoverage

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 with:

install.packages("invertiforms")

You can install the development version from GitHub with:

# 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.

library(invertiforms)
#> Loading required package: Matrix
#> 
#> Attaching package: 'invertiforms'
#> The following object is masked from 'package:base':
#> 
#>     transform
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(igraphdata)

data("karate", package = "igraphdata")

A <- get.adjacency(karate)
image(A)

Now we construct the regularized degree normalized graph Laplacian and visualize it.

iform <- RegularizedLaplacian(A, tau_row = 35, tau_col = 35)

L <- transform(iform, A)
image(L)

Recovering A from L is straightforward:

A_recovered <- inverse_transform(iform, L)

all.equal(A, A_recovered, check.attributes = FALSE)
#> [1] TRUE

Once we have L we can do spectral tricks with it. Here we estimate k = 2 clusters.

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 provides non-invertible, composable transformations for tabular data. softImpute provides an Incomplete S4 Matrix class that can be used for double centering sparse matrices.

Metadata

Version

0.1.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows