Fit a structural topic model (STM) on open-ended text
Source:R/text_analysis.R
sframe_run_stm_topics.RdCleans the text item via clean_text_responses(), tokenises with
tidytext::unnest_tokens() (tidyeval column names via rlang::sym() and
!!, not bare symbols; see the source comment at the tokenising step for
why), casts to a document-term matrix, converts it to stm's corpus
format with stm::readCorpus(type = "slam") and stm::prepDocuments(),
and fits stm::stm() after a fixed set.seed() (stm's own fit is not
otherwise seed-stable).
Arguments
- data
A data.frame of responses.
- roles
A list with
item, the text/textarea item id.- options
A list;
k(topic count, default4L– a demonstration value, not a recommendation; seevignette("text-analysis")'s topic- modelling section fortopicmodels::perplexity()-based selection),seed(default42L),stop_words(passed through to the tokeniser).- instrument
Optional
sframeinstrument, passed toclean_text_responses()for item-type validation.
Value
A runner-contract result list: test = "stm_topics", table
(topic/proportion/term/beta/rank; proportion is the topic's mean
document weight, beta its per-term probability, both from the fitted
stm object), fit (a runtime-only list holding the stm model
object and the document-to-respondent mapping needed by
extract_quotes()), apa, prompt. On failure:
list(test = "stm_topics", error = <message>).