# NOT RUN {
if (requireNamespace("topicmodels", quietly = TRUE)) {
set.seed(2016)
library(dplyr)
library(topicmodels)
data("AssociatedPress", package = "topicmodels")
ap <- AssociatedPress[1:100, ]
lda <- LDA(ap, control = list(alpha = 0.1), k = 4)
# get term distribution within each topic
td_lda <- tidy(lda)
td_lda
library(ggplot2)
# visualize the top terms within each topic
td_lda_filtered <- td_lda %>%
filter(beta > .004) %>%
mutate(term = reorder(term, beta))
ggplot(td_lda_filtered, aes(term, beta)) +
geom_bar(stat = "identity") +
facet_wrap(~ topic, scales = "free") +
theme(axis.text.x = element_text(angle = 90, size = 15))
# get classification of each document
td_lda_docs <- tidy(lda, matrix = "gamma")
td_lda_docs
doc_classes <- td_lda_docs %>%
group_by(document) %>%
top_n(1) %>%
ungroup()
doc_classes
# which were we most uncertain about?
doc_classes %>%
arrange(gamma)
}
# }
Run the code above in your browser using DataLab