Skip to content

Instantly share code, notes, and snippets.

@subwaymatch
Created February 2, 2023 02:40
Show Gist options
  • Save subwaymatch/dae2a7da18930fcf9085464ea67ce718 to your computer and use it in GitHub Desktop.
Save subwaymatch/dae2a7da18930fcf9085464ea67ce718 to your computer and use it in GitHub Desktop.
LDAvis with topicmodels package in R
# install.packages("topicmodels")
# install.packages("reshape2")
# install.packages("LDAvis")
# install.packages("servr")
library(topicmodels)
library(reshape2)
library(LDAvis)
library(servr)
# create LDA model
tweets_lda <- topicmodels::LDA(dtm, k = 3, control = list(seed = 12))
post <- topicmodels::posterior(tweets_lda)
mat <- tweets_lda@wordassignments
# create input JSON for LDAvis
json <- LDAvis::createJSON(
phi = post$terms,
theta = post$topics,
vocab = colnames(post$terms),
doc.length = slam::row_sums(mat, na.rm = TRUE),
term.frequency = slam::col_sums(mat, na.rm = TRUE)
)
serVis(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment