Skip to contents

The words that describe a topic, by four measures:

  • "prob": the most probable terms, \(\beta_{kv}\). Frequent words shared by several topics come first.

  • "frex": FREX (Bischof and Airoldi 2012; Roberts et al. 2014) balances frequency in the topic and exclusivity to the topic. With \(F_{kv}\) the rank share of \(\beta_{kv}\) among the terms of topic \(k\), and \(E_{kv}\) the rank share of \(\beta_{kv} / \sum_j \beta_{jv}\) (exclusivity), $$\mathrm{FREX}_{kv} = \left(\frac{w}{F_{kv}} + \frac{1 - w}{E_{kv}}\right)^{-1},$$ with \(w\) = frexw, the weight of frequency.

  • "score": the score of stm::labelTopics() (after the lda package), which down-weights terms probable in every topic: $$\mathrm{score}_{kv} = \beta_{kv}\left(\log\beta_{kv} - \frac{1}{K}\sum_j \log\beta_{jv}\right).$$

  • "lift" (Taddy 2012): the probability in the topic relative to the empirical frequency in the corpus, on the log scale, $$\log \mathrm{lift}_{kv} = \log\beta_{kv} - \log\frac{c_v}{\sum_u c_u},$$ with \(c_v\) the count of term \(v\). It favours rare, topic-specific terms; it needs the counts (counts or stm_data).

Usage

topic_terms(
  model,
  measure = "prob",
  n_terms = 10L,
  frexw = 0.5,
  counts = NULL,
  stm_data = NULL,
  ...
)

Arguments

model

A fitted stm model without content covariate (see marginal_topic_model()), or a list with beta$logbeta[[1]] (topics x terms log-probabilities) and vocab.

measure

Character vector, one or several of "prob", "frex", "score", "lift".

n_terms

Integer scalar, number of terms per topic and measure.

frexw

Numeric scalar in [0, 1], weight of frequency against exclusivity in FREX (stm::labelTopics() uses 0.5).

counts

For "lift": a table with term and count, the empirical counts (e.g. from term_counts()).

stm_data

For "lift", when counts is NULL: the STM input the model was fitted on (documents, vocab), from which the counts are computed.

...

Not used: an argument renamed in the development version of mintR gives an error naming its replacement.

Value

A data.table with topic, measure, rank, term and value (the value of the measure). For lift, terms with a zero count have no defined lift and are never returned. The mean FREX of a model (an exclusivity-like summary) is topic_terms(model, "frex")[, mean(value)].

Details

The FREX of topic_terms() follows the formula above; the FREX labels of topic_labels() reproduce stm::labelTopics(), which shrinks exclusivity towards the corpus word counts (James-Stein estimator): the two can differ slightly.

References

Bischof, J. and Airoldi, E. M. (2012). Summarizing topical content with word frequency and exclusivity. Proceedings of ICML 2012, 201-208.

Roberts, M. E., Stewart, B. M., Tingley, D., Lucas, C., Leder-Luis, J., Gadarian, S. K., Albertson, B. and Rand, D. G. (2014). Structural topic models for open-ended survey responses. American Journal of Political Science, 58(4), 1064-1082. doi:10.1111/ajps.12103

Taddy, M. (2012). On estimation and selection for topic models. Proceedings of AISTATS 2012, 1184-1193.

Examples

logbeta <- log(matrix(c(0.5, 0.3, 0.2, 0.1, 0.2, 0.7), nrow = 2, byrow = TRUE))
mdl <- list(beta = list(logbeta = list(logbeta)), vocab = c("wage", "labour", "trade"))
topic_terms(mdl, n_terms = 2)
#>    topic measure  rank   term value
#>    <int>  <char> <int> <char> <num>
#> 1:     1    prob     1   wage   0.5
#> 2:     1    prob     2 labour   0.3
#> 3:     2    prob     1  trade   0.7
#> 4:     2    prob     2 labour   0.2
counts <- data.frame(term = c("wage", "labour", "trade"), count = c(10, 5, 20))
topic_terms(mdl, c("frex", "lift"), n_terms = 2, counts = counts)
#>    topic measure  rank   term     value
#>    <int>  <char> <int> <char>     <num>
#> 1:     1    frex     1   wage 1.0000000
#> 2:     1    frex     2 labour 0.6666667
#> 3:     2    frex     1  trade 1.0000000
#> 4:     2    frex     2 labour 0.6666667
#> 5:     1    lift     1 labour 0.7419373
#> 6:     1    lift     2   wage 0.5596158
#> 7:     2    lift     1 labour 0.3364722
#> 8:     2    lift     2  trade 0.2029408