cpsievert / LDAvis

R package for web-based interactive topic model visualization.
Other
557 stars 131 forks source link

Error with 2 topics #54

Open yogesh-bansal opened 8 years ago

yogesh-bansal commented 8 years ago

I was using this package's createJSON() function while my topicmodel was for 2 topics and received this error Error in stats::cmdscale(dist.mat, k = 2) : 'k' must be in {1, 2, .. n - 1} Then I tested with reproducible example given here http://cpsievert.github.io/LDAvis/reviews/reviews.html by putting K=2 and keeping everything same and bumped into this error again in createJSON().

Upon seeing the source code of createJSON(), the issue is in function jsPCA(). In jsPCA(), while K=2, the dist.mat comes out to be a single value which throws an error in line pca.fit <- stats::cmdscale(dist.mat, k = 2)

Any advice how to get past this error?

connieshen0503 commented 6 years ago

I got the same error, do you find a way to fix this problem? k=2

ThomasK81 commented 3 years ago

I know this is a super old issue and I am not sure whether it is still needed, but I noticed some other errors occasionally with jsPCA(). This is why I rewrote the function in the following way:

jsPCA_updated <- function(phi) {
  dist.mat <- philentropy::distance(phi, 
                       method = "jensen-shannon",
                       unit = "log2")
  pca.fit <- stats::cmdscale(dist.mat, k = 2)
  data.frame(x = pca.fit[,1], y = pca.fit[,2])
}

You can then call it in the createJSON() function by setting mds.method = jsPCA_updated.

@cpsievert I haven't yet figured out why this function occasionally throws errors. I particularly noticed that it occurs more often, when using models created with the topicmodels package. Sometimes the built-in jsPCA creates one or more infinite values and stats::cmdscale throws an error. createJSON catches the error, but reports NA values instead of infinite values. If you don't mind the philentropy dependency, I'd open a pull request.

PS: Thanks for generating this package. I think it's super handy!