library(stm)
library(dplyr)
library(ggplot2)
library(quanteda)
# prepare data
data <- corpus(gadarian, text_field = 'open.ended.response')
docvars(data)$text <- as.character(data)
data <- tokens(data, remove_punct = TRUE) |>
tokens_wordstem() |>
tokens_remove(stopwords('english')) |> dfm() |>
dfm_trim(min_termfreq = 2)
out <- convert(data, to = 'stm')
# fit models
gadarian_3 <- stm(documents = out$documents,
vocab = out$vocab,
data = out$meta,
prevalence = ~ treatment + s(pid_rep),
K = 3,
max.em.its = 1, # reduce computation time for example
verbose = FALSE)
gadarian_5 <- stm(documents = out$documents,
vocab = out$vocab,
data = out$meta,
prevalence = ~ treatment + s(pid_rep),
K = 5,
max.em.its = 1, # reduce computation time for example
verbose = FALSE)
# get diagnostics
diag <- get_diag(models = list(
model_3 = gadarian_3,
model_5 = gadarian_5),
outobj = out)
if (FALSE) {
# plot diagnostics
diag |>
ggplot(aes(x = coherence, y = exclusivity, color = statistic)) +
geom_text(aes(label = name), nudge_x = 5) + geom_point() +
labs(x = 'Semantic Coherence', y = 'Exclusivity') + theme_light()
}
Run the code above in your browser using DataLab