Created
February 2, 2023 02:40
-
-
Save subwaymatch/dae2a7da18930fcf9085464ea67ce718 to your computer and use it in GitHub Desktop.
LDAvis with topicmodels package in R
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
# 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