Skip to contents

Make a WarpLDA topic model usable with every OpTop tool. text2vec's WarpLDA sampler splits its output in two: fit_transform() returns the document-topic matrix to you, while the model object retains only the topic-word distribution. OpTop needs both halves together, so this helper bundles the model object and the matrix you kept into one light object accepted everywhere a fitted topic model is (optop_select(), optop_make_partition(), the index family, optop_index_holdout() and optop_moment_test()).

Usage

optop_warplda(model, doc_topic_distr)

Arguments

model

A fitted text2vec::LDA (WarpLDA) R6 object.

doc_topic_distr

The document-topic probability matrix returned by model$fit_transform() on the corpus under evaluation (rows are documents and carry the document identifiers, columns are topics, rows sum to 1). Keep it when fitting; it cannot be recovered from the model object alone.

Value

An object of class optop_warplda holding the pair; its optop_as_theta_phi() method exposes theta and phi with the usual contract, and the held-out tools fold in new documents through model$transform().

Details

In practice the workflow is: fit with text2vec as usual, keep the matrix that fit_transform() returns, and wrap the two before handing them to OpTop:

lda <- text2vec::LDA$new(n_topics = 10)
theta <- lda$fit_transform(dtm, n_iter = 1000)
fit <- optop_warplda(lda, theta)

Passing the raw R6 object to an OpTop function fails with a pointer to this helper, because the document-topic matrix is not stored inside it.

References

Lewis, C. M. and Grossetti, F. (2026). Goodness-of-fit indices and diagnostics for topic models. Working paper.

Examples

# \donttest{
if (requireNamespace("text2vec", quietly = TRUE)) {
  rdir <- function(n, k, a) {
    g <- matrix(stats::rgamma(n * k, shape = a), nrow = n)
    g / rowSums(g)
  }
  set.seed(42)
  corpus <- sim_dfm(DTW = rdir(60, 4, 0.4), TWW = rdir(4, 200, 0.1),
                    doc_length = rep(300, 60), seed = 1)
  dtm <- quanteda::as.dfm(corpus)

  lda <- text2vec::LDA$new(n_topics = 4)
  theta <- lda$fit_transform(dtm, n_iter = 200, progressbar = FALSE)
  fit <- optop_warplda(lda, theta)

  part <- optop_make_partition(list(fit), dtm)
  base <- optop_make_baseline(dtm)
  optop_index_deviance(fit, dtm, part, base)$r2
}
#> INFO  [12:42:32.114] early stopping at 50 iteration
#> INFO  [12:42:32.179] early stopping at 30 iteration
#> [1] 0.7808694
# }