GIORIGO
GIORIGO

Reputation: 59

Calculate cosine similarity and create a graph in R

I have datasets like this:

x <- c(1,4,6,8,0,5)
y<-c(2,3,5,8,1,14)
z<-c(3,5,23,51,3,15)
t<- c(14,14,23,4,16,17)

NB. I actually have 20 of vectors, this is a simplified example.

I want to automatically calculate the cosine similarity between all vectors and then create a network of these vectors (each vector will be connected with all the others), where the size of the bridge depends on the value of the cosine similarity. I hope I explained myself well, Thanks

Upvotes: 1

Views: 1053

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101129

Maybe you can try cosine from package lsa

> lsa::cosine(cbind(x,y,z,t))
          x         y         z         t
x 1.0000000 0.8638538 0.9271073 0.7084596
y 0.8638538 1.0000000 0.7510150 0.7075566
z 0.9271073 0.7510150 1.0000000 0.5115712
t 0.7084596 0.7075566 0.5115712 1.0000000

For its visualization, you can try heatmap

heatmap(lsa::cosine(cbind(x, y, z, t)))

enter image description here

Upvotes: 2

Related Questions