if (FALSE) { # interactive() || identical(Sys.getenv("IN_PKGDOWN"), "true")
library(dplyr)
library(ggplot2)
library(stm)
library(janeaustenr)
austen_sparse <- austen_books() %>%
unnest_tokens(word, text) %>%
anti_join(stop_words) %>%
count(book, word) %>%
cast_sparse(book, word, n)
topic_model <- stm(austen_sparse, K = 12, verbose = FALSE)
# tidy the word-topic combinations
td_beta <- tidy(topic_model)
td_beta
# Examine the topics
td_beta %>%
group_by(topic) %>%
slice_max(beta, n = 10) %>%
ungroup() %>%
ggplot(aes(beta, term)) +
geom_col() +
facet_wrap(~ topic, scales = "free")
# high FREX words per topic
tidy(topic_model, matrix = "frex")
# high lift words per topic
tidy(topic_model, matrix = "lift")
# tidy the document-topic combinations, with optional document names
td_gamma <- tidy(topic_model, matrix = "gamma",
document_names = rownames(austen_sparse))
td_gamma
# using stm's gardarianFit, we can tidy the result of a model
# estimated with covariates
effects <- estimateEffect(1:3 ~ treatment, gadarianFit, gadarian)
glance(effects)
td_estimate <- tidy(effects)
td_estimate
}
Run the code above in your browser using DataLab