knn_graph {speakeasyR} | R Documentation |
K-nearest neighbors graph
Description
Create a directed sparse graph with edges to each nodes k
nearest
neighbors. Nearness is calculated as the inverse of the euclidean distance
between two columns.
Usage
knn_graph(mat, k, weighted = FALSE)
Arguments
mat |
A matrix to be compared column-by-column. |
k |
How many nearest neighbors to collect. |
weighted |
By default, a binary edge is made between a node and each of
it's |
Value
A directed sparse adjacency matrix with k * ncol(mat)
nonzero
edges. Each column has k edges connected to the k closest columns (not
including itself).
Examples
# Simple random graph
mat <- matrix(runif(100) > 0.75, nrow = 5)
knn_graph(mat, 3)
## Don't run because loading data is slow.
if (requireNamespace("scRNAseq") &&
requireNamespace("SummarizedExperiment")) {
# Single Cell RNA data
library(Matrix)
expression <- scRNAseq::FletcherOlfactoryData()
cell_types <- expression$cluster_id
## Filter genes with low expression. Remove any genes with less than 10
## cells with with any reads.
counts <- SummarizedExperiment::assay(expression, "counts")
indices <- rowSums(counts > 0) > 10
counts <- counts[indices, ]
## Normalize by shifted logarithm
target <- median(colSums(counts))
size_factors <- colSums(counts) / target
counts_norm <- log(t(t(counts) / size_factors + 1))
## Dimension reduction
counts_norm <- t(prcomp(t(counts_norm), scale. = FALSE)$x)[1:50, ]
adj <- knn_graph(counts_norm, 10)
}
[Package speakeasyR version 0.1.2 Index]