
Collapse a content-covariate STM to one word distribution per topic
Source:R/stm_helpers.R
marginal_topic_model.RdIn an STM with a content covariate (for instance the type
of document: article, report, speech), a topic does not have one list of
words but one per level of the covariate: the same topic is expressed
with different words in an article and in a report. The fitted model
then stores one word distribution per level (model$beta$logbeta is a
list of matrices), and stm::labelTopics() and the other functions of
this package, which expect a single distribution per topic, cannot read
it (they stop with an error pointing here).
marginal_topic_model() returns a copy of the model with a single
distribution per topic, the average of the levels' distributions. Use it
before topic_labels(), topic_terms(), topic_word_mass() or the
metrics, to describe each topic with its words across all document
types. Each level counts in proportion to the number of tokens of the
documents that belong to it, so that a type with few documents weighs
little:
$$\beta_{kv} = \sum_{a} \pi_{a} \, \beta_{kv}^{a}$$
where \(\beta_{kv}^{a}\) is the probability of word \(v\) in topic \(k\) for level \(a\), and \(\pi_{a}\) is the share of the corpus tokens that are in documents of level \(a\).