Skip to contents

Wrap a document-topic matrix and a topic-word matrix into the model object every OpTop tool accepts. This is the public entry point for engines the package has no adapter for: estimate the two matrices with any software, hand them to optop_model(), and the result flows through optop_select(), the discrepancy indices, and the grid summaries like any supported fit.

Usage

optop_model(theta, phi)

Arguments

theta

Numeric matrix, documents by topics: row j holds the topic weights of document j and sums to 1. Row names are the document identifiers, required and unique.

phi

Numeric matrix, topics by words: row k holds the word distribution of topic k and sums to 1. Column names are the terms, required and unique, in the same order as the columns of the document-term matrix the model was estimated on.

Value

An object of class optop_theta_phi: a list with elements theta, phi, K (the number of topics), docs, and terms.

Details

The contract is validated on construction: theta is documents by topics and phi is topics by words, both nonnegative with rows summing to 1 (tolerance 1e-6); theta needs one unique document identifier per row in its row names and phi one unique term per column in its column names, because OpTop aligns by name and never by position.

One limitation follows from having no fitted engine behind the object: an optop_model() cannot fold new documents in, so the held-out tools (optop_index_holdout() and everything built on it), which must adapt the model to unseen documents, do not accept it. Use the engine's own object there instead.

See also

optop_as_theta_phi() for the adapter generic behind the supported engines, optop_warplda() for text2vec WarpLDA fits.

Examples

theta <- matrix(c(0.7, 0.3,
                  0.2, 0.8), 2, 2, byrow = TRUE,
                dimnames = list(c("doc1", "doc2"), NULL))
phi <- matrix(c(0.5, 0.3, 0.2,
                0.1, 0.2, 0.7), 2, 3, byrow = TRUE,
              dimnames = list(NULL, c("alpha", "beta", "gamma")))
m <- optop_model(theta, phi)
m
#> <optop_theta_phi>: a topic model for OpTop
#> K = 2 topics, 2 documents, 3 terms
#> theta: 2 x 2, phi: 2 x 3; rows sum to 1