| Title: | Gaussian Mixture Topic Models |
|---|---|
| Description: | Gaussian mixture models (GMM) and k-means for topic analysis of dense document vectors. The underlying clustering functions rely on the Armadillo library (Sanderson & Curtin, 2017) <doi:10.1109/ICSPCS.2017.8270510>. |
| Authors: | Kohei Watanabe [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6519-5265>), Sanderson Conrad [ctb, cph] (C++ code for GMM and k-means), Curtin Ryan [ctb, cph] (C++ code for GMM and k-means) |
| Maintainer: | Kohei Watanabe <[email protected]> |
| License: | Apache License (>= 2.0) |
| Version: | 0.2.1 |
| Built: | 2026-10-12 06:37:06 UTC |
| Source: | https://github.com/koheiw/gmtm |
Convert a dictionary to a seed word matrix
as.seedwords(x, model, residual = 0, levels = 1)as.seedwords(x, model, residual = 0, levels = 1)
x |
a quanteda::dictionary of seed words. |
model |
a wordvector::textmodel_word2vec object. |
residual |
the number of unseeded topics. |
levels |
integers specifying the levels of entries in a hierarchical dictionary. |
Word vectors of the dictionary values within the same key are all averaged as a
single seed vector. If dictionary values are multi-word expressions, they are
matched against individual and compounded words in the model.
Unseeded topics are labeled "other", but it can be changed via options("GMTM.residual.name").
Returns a seed word matrix.
library(quanteda) library(wordvector) options(wordvector_threads = 2) corp <- head(wordvector::data_corpus_news2014, 1000) toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE) %>% tokens_remove(stopwords("en"), min_nchar = 2) wov <- textmodel_word2vec(toks, dim = 50) dict <- dictionary(list(eco = "econom*", sec = "securit*", spo = "sport*", pol = "politi*", cri = "crime")) seed <- as.seedwords(dict, wov, residual = 2)library(quanteda) library(wordvector) options(wordvector_threads = 2) corp <- head(wordvector::data_corpus_news2014, 1000) toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE) %>% tokens_remove(stopwords("en"), min_nchar = 2) wov <- textmodel_word2vec(toks, dim = 50) dict <- dictionary(list(eco = "econom*", sec = "securit*", spo = "sport*", pol = "politi*", cri = "crime")) seed <- as.seedwords(dict, wov, residual = 2)
Extract the probabilities for topics
## S3 method for class 'textmodel_gmm' probability(x, group = NULL, ...)## S3 method for class 'textmodel_gmm' probability(x, group = NULL, ...)
x |
a fitted model. |
group |
a factor to group documents and average their probability for
topics. Ignored if |
... |
not used. |
Returns the probabilities of topics as a matrix.
Extract lost likely topic terms from documents
terms(x, data, n = 10, filter = NULL, ...)terms(x, data, n = 10, filter = NULL, ...)
x |
a fitted model or a factor from |
data |
a quanteda::dfm or quanteda::tokens from which words are extracted for each topic. |
n |
the number of topic words. |
filter |
a character vector of words to be included in the output. |
... |
not used. |
To identify topic terms, data must be provided along with a fitted model because
the information about the frequency of individual words are lost in document vectors.
When x is a textmodel_gmm, data is weighted by the documents' probabilities
for topics to extract most likely terms. When x is textmodel_kmeans or a factor,
data is grouped by topic and weighted by TF-IDF to select the most distinctive
words for each topic (known as c-TF-IDF).
Returns a character matrix with the most distinctive words for each topic.
Perform topic analysis of document vectors using Gaussian mixture models.
textmodel_gmm( x, k = 10, model = NULL, seeds = NULL, omit = NULL, verbose = quanteda_options("verbose"), ... )textmodel_gmm( x, k = 10, model = NULL, seeds = NULL, omit = NULL, verbose = quanteda_options("verbose"), ... )
x |
a wordvector::textmodel_doc2vec or a dense matrix of document vectors in the rows. |
k |
the number of topics to identify. |
model |
a fitted model from which initial centroids are extracted. |
seeds |
a matrix created using as.seedwords. |
omit |
indices of singular values of |
verbose |
print the progress if |
... |
passed to the underlying function. |
Users can change the number of threads for the parallel computing via
options(GMTM.threads) or OMP_THREAD_LIMIT in the environmental
variable. To reproduce results, set options(GMTM.threads = 1) and call
set.seed() immediately before textmodel_gmm() or textmodel_kmeans().
On MacOS, only one thread is used regardless of GMTM.threads
because CRAN's toolchain for the platform does not support OpenMP.
omit is used to reduce the noise in the x by applying base::svd before
clustering. If it is not NULL, singular values corresponding to omit are
set to zero, removing their variance in x. See Chan et al. (2020)
https://doi.org/10.1080/19312458.2020.1812555 for the methodology.
The number of iterations in k-means (iter_km) and expectation maximization
(iter_em) stages can be set via ....
Returns a fitted textmodel_gmm object.
library(quanteda) library(wordvector) options(wordvector_threads = 2) corp <- head(wordvector::data_corpus_news2014, 1000) toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE) %>% tokens_remove(stopwords("en"), min_nchar = 2) wov <- textmodel_word2vec(toks, dim = 50) dov <- as.textmodel_doc2vec(dfm(toks), wov) gmm <- textmodel_gmm(dov, k = 10) table(topics(gmm))library(quanteda) library(wordvector) options(wordvector_threads = 2) corp <- head(wordvector::data_corpus_news2014, 1000) toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE) %>% tokens_remove(stopwords("en"), min_nchar = 2) wov <- textmodel_word2vec(toks, dim = 50) dov <- as.textmodel_doc2vec(dfm(toks), wov) gmm <- textmodel_gmm(dov, k = 10) table(topics(gmm))
Perform topic analysis of document vectors using k-means.
textmodel_kmeans( x, k = 10, model = NULL, seeds = NULL, verbose = quanteda_options("verbose"), ... )textmodel_kmeans( x, k = 10, model = NULL, seeds = NULL, verbose = quanteda_options("verbose"), ... )
x |
a wordvector::textmodel_doc2vec or a dense matrix of document vectors in the rows. |
k |
the number of topics to identify. |
model |
a fitted model from which initial centroids are extracted. |
seeds |
a matrix created using as.seedwords. |
verbose |
print the progress if |
... |
passed to the underlying function. |
Returns a fitted textmodel_kmeans object.
library(quanteda) library(wordvector) options(wordvector_threads = 2) corp <- head(wordvector::data_corpus_news2014, 1000) toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE) %>% tokens_remove(stopwords("en"), min_nchar = 2) wov <- textmodel_word2vec(toks, dim = 50) dov <- as.textmodel_doc2vec(dfm(toks), wov) km <- textmodel_kmeans(dov, k = 10) table(topics(km))library(quanteda) library(wordvector) options(wordvector_threads = 2) corp <- head(wordvector::data_corpus_news2014, 1000) toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, remove_numbers = TRUE) %>% tokens_remove(stopwords("en"), min_nchar = 2) wov <- textmodel_word2vec(toks, dim = 50) dov <- as.textmodel_doc2vec(dfm(toks), wov) km <- textmodel_kmeans(dov, k = 10) table(topics(km))
Extract the most likely topics of documents
topics(x, ...)topics(x, ...)
x |
a fitted model. |
... |
passed to |
Returns predicted topics as a vector.