Learn R Programming

dynutils (version 1.0.11)

calculate_distance: Calculate (column-wise) distances/similarity between two matrices

Description

These matrices can be dense or sparse.

Usage

calculate_distance(
  x,
  y = NULL,
  method = c("pearson", "spearman", "cosine", "euclidean", "chisquared", "hamming",
    "kullback", "manhattan", "maximum", "canberra", "minkowski"),
  margin = 1,
  diag = FALSE,
  drop0 = FALSE
)

list_distance_methods()

calculate_similarity( x, y = NULL, margin = 1, method = c("spearman", "pearson", "cosine"), diag = FALSE, drop0 = FALSE )

list_similarity_methods()

Arguments

x

A numeric matrix, dense or sparse.

y

(Optional) a numeric matrix, dense or sparse, with nrow(x) == nrow(y).

method

Which distance method to use. Options are: "cosine", "pearson", "spearman", "euclidean", and "manhattan".

margin

integer indicating margin of similarity/distance computation. 1 indicates rows or 2 indicates columns.

diag

if TRUE, only compute diagonal elements of the similarity/distance matrix; useful when comparing corresponding rows or columns of x and y.

drop0

if TRUE, zero values are removed regardless of min_simil or rank.

Examples

Run this code
## Generate two matrices with 50 and 100 samples
library(Matrix)
x <- Matrix::rsparsematrix(50, 1000, .01)
y <- Matrix::rsparsematrix(100, 1000, .01)

dist_euclidean <- calculate_distance(x, y, method = "euclidean")
dist_manhattan <- calculate_distance(x, y, method = "manhattan")
dist_spearman <- calculate_distance(x, y, method = "spearman")
dist_pearson <- calculate_distance(x, y, method = "pearson")
dist_angular <- calculate_distance(x, y, method = "cosine")

Run the code above in your browser using DataLab