tj_digital
tj_digital

Reputation: 21

Error in R: Cosine similarity and MDS

I calculate the cosine similarity with cosine() from the package 'lsa'. Here with three test vectors:

d <- data.frame(c(-1,1,0,-1,1,1,-1,1,0),c(-1,1,1,1,-1,1,-1,0,1),c(0,0,1,0,-1,-1,0,1,-1))
colnames(d) <- c("vector1","vector2","vector3")
d_dist <- cosine(as.matrix(d))

Now, I want to do dimensionality reduction with cmdscale and after that plot it as a scatterplot:

fit <- cmdscale(d_dist,k=2)

x <- fit[,2]
y <- fit[,1]
plot(x,y)

But I always get the warning In cmdscale (d_dist, k = 2): only 0 of the first 2 eigenvalues ​​are> 0 [translated from German] and an empty fit object.

What am I doing wrong? Thank you so much for your help!

Upvotes: 2

Views: 657

Answers (1)

thc
thc

Reputation: 9705

The input should be a distance matrix. E.g.:

d_dist <- 1-d_dist
fit <- cmdscale(d_dist,k=2)

x <- fit[,2]
y <- fit[,1]
plot(x,y)

Upvotes: 1

Related Questions