Skip to content

Instantly share code, notes, and snippets.

@wesslen
Created July 22, 2016 22:32
Show Gist options
  • Save wesslen/59cd0a8b0c83a8d09f2db253ea12ca81 to your computer and use it in GitHub Desktop.
Save wesslen/59cd0a8b0c83a8d09f2db253ea12ca81 to your computer and use it in GitHub Desktop.
Convert LDA (topicmodels) output in R to json using R quanteda package for LDAvis
topicmodels_json_ldavis <- function(fitted, dfm, dtm){
# Required packages
library(topicmodels)
library(dplyr)
library(stringi)
library(quanteda)
library(LDAvis)
# Find required quantities
phi <- posterior(fitted)$terms %>% as.matrix
theta <- posterior(fitted)$topics %>% as.matrix
vocab <- colnames(phi)
doc_length <- ntoken(dfm[rownames(dtm)])
temp_frequency <- inspect(dtm)
freq_matrix <- data.frame(ST = colnames(temp_frequency),
Freq = colSums(temp_frequency))
rm(temp_frequency)
# Convert to json
json_lda <- LDAvis::createJSON(phi = phi, theta = theta,
vocab = vocab,
doc.length = doc_length,
term.frequency = freq_matrix$Freq)
return(json_lda)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment