-
Notifications
You must be signed in to change notification settings - Fork 13
/
functions.R
30 lines (25 loc) · 895 Bytes
/
functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
topicmodels_json_ldavis <- function(fitted, dfm, dtm){
# Required packages
library(topicmodels)
library(dplyr)
library(stringi)
library(quanteda)
library(LDAvis)
library(tm)
# 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 <- as.matrix(dtm)
freq_matrix <- data.frame(ST = colnames(temp_frequency),
Freq = colSums(temp_frequency),
stringsAsFactors = F)
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)
}