CosSim: Calculates cosine similarity between the columns of a matrix

Description Usage Arguments Value Examples

Description

Calculates cosine similarity between the columns of a matrix

Usage

1
CosSim(x, lower.tri.only = FALSE)

Arguments

x

Matrix for which cosine similarities are to be calculated. Currently, the function requires the matrix to be symmetric and free of missing values. The matrix must have named columns specifying the identity of the things across which similarity is measured. (In my application, these are fund WFICNs.)

lower.tri.only

Should the returned matrix include only the lower triangle of similarities? (Useful for efficient storage of large matrices, as the returned matrix of cosine similarities is symmetric.)

Value

Matrix of cosine similarities, with row and column names of what is being compared.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
x.same <- matrix(0.5, 2, 2)
colnames(x.same) <- c("fund1", "fund2")
rownames(x.same) <- c("stock1", "stock2")
CosSim(x.same)
x.orthogonal <- cbind(c(1, 0), c(0, 1))
colnames(x.orthogonal) <- c("fund1", "fund2")
rownames(x.orthogonal) <- c("stock1", "stock2")
CosSim(x.orthogonal)
x <- cbind(c(0.75, 0.25), c(0.25, 0.75))
colnames(x) <- c("fund1", "fund2")
rownames(x) <- c("stock1", "stock2")
CosSim(x)

laszlo-jakab/compsizer documentation built on May 17, 2019, 7:30 p.m.