Learn R Programming

lsa (version 0.73.3)

cosine: Cosine Measure (Matrices)

Description

Calculates the cosine measure between two vectors or between all column vectors of a matrix.

Usage

cosine(x, y = NULL)

Arguments

x

A vector or a matrix (e.g., a document-term matrix).

y

Optional: a vector with compatible dimensions to x. If `NULL', all column vectors of x are correlated.

Value

Returns a \(n*n\) similarity matrix of cosine values, comparing all \(n\) column vectors against each other. Executed on two vectors, their cosine similarity value is returned.

Details

cosine() calculates a similarity matrix between all column vectors of a matrix x. This matrix might be a document-term matrix, so columns would be expected to be documents and rows to be terms.

When executed on two vectors x and y, cosine() calculates the cosine similarity between them.

References

Leydesdorff, L. (2005) Similarity Measures, Author Cocitation Analysis,and Information Theory. In: JASIST 56(7), pp.769-772.

See Also

cor

Examples

Run this code
# NOT RUN {
## the cosinus measure between two vectors

vec1 = c( 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 )
vec2 = c( 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0 )
cosine(vec1,vec2) 

# }
# NOT RUN {
<!-- % => -->
# }
# NOT RUN {
<!-- %    0.2357023 -->
# }
# NOT RUN {
## the cosine measure for all document vectors of a matrix

vec3 = c( 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0 )
matrix = cbind(vec1,vec2, vec3)
cosine(matrix)

# }
# NOT RUN {
<!-- % => -->
# }
# NOT RUN {
<!-- %           vec1      vec2      vec3 -->
# }
# NOT RUN {
<!-- % vec1 1.0000000 0.2357023 0.2886751 -->
# }
# NOT RUN {
<!-- % vec2 0.2357023 1.0000000 0.4082483 -->
# }
# NOT RUN {
<!-- % vec3 0.2886751 0.4082483 1.0000000 -->
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab