# NOT RUN {
# }
# NOT RUN {
if (requireNamespace("stm", quietly = TRUE) && requireNamespace("quanteda", quietly = TRUE)) {
library(dplyr)
library(ggplot2)
library(stm)
library(quanteda)
inaug <- dfm(data_corpus_inaugural, remove = stopwords("english"), remove_punct = TRUE)
topic_model <- stm(inaug, K = 3, verbose = FALSE, init.type = "Spectral")
# tidy the word-topic combinations
td_beta <- tidy(topic_model)
td_beta
# Examine the three topics
td_beta %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
ggplot(aes(term, beta)) +
geom_col() +
facet_wrap(~ topic, scales = "free") +
coord_flip()
# tidy the document-topic combinations, with optional document names
td_gamma <- tidy(topic_model, matrix = "gamma",
document_names = rownames(inaug))
td_gamma
# find the assignments of each word in each document
assignments <- augment(topic_model, inaug)
assignments
}
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab