Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt
Forked from anonymous/ldavis_json.r
Last active November 5, 2017 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanatteveldt/6269f1336ac54e752fa22c85bf2354fc to your computer and use it in GitHub Desktop.
Save vanatteveldt/6269f1336ac54e752fa22c85bf2354fc to your computer and use it in GitHub Desktop.
ldavis_json <- function(m, dtm){
dtm = dtm[slam::row_sums(dtm) > 0, ]
# Find required quantities
phi <- posterior(m)$terms %>% as.matrix
theta <- posterior(m)$topics %>% as.matrix
vocab <- colnames(phi)
doc.length = slam::row_sums(dtm)
term.freq = slam::col_sums(dtm)[match(vocab, colnames(dtm))]
createJSON(phi = phi, theta = theta,
vocab = vocab,
doc.length = doc.length,
term.frequency = term.freq)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment