Created
July 22, 2016 22:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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