我如何测量使用 R 中的 textmineR 包制作的 LDA 模型的困惑分数?

How do i measure perplexity scores on a LDA model made with the textmineR package in R?

我用R语言做了一个LDA主题模型,使用的是textmineR包,如下所示。

## get textmineR dtm
dtm2 <- CreateDtm(doc_vec = dat2$fulltext, # character vector of documents
                 ngram_window = c(1, 2), 
                 doc_names = dat2$names,
                 stopword_vec = c(stopwords::stopwords("da"), custom_stopwords),
                 lower = T, # lowercase - this is the default value
                 remove_punctuation = T, # punctuation - this is the default
                 remove_numbers = T, # numbers - this is the default
                 verbose = T,
                 cpus = 4)



dtm2 <- dtm2[, colSums(dtm2) > 2]
dtm2 <- dtm2[, str_length(colnames(dtm2)) > 2]


############################################################
## RUN & EXAMINE TOPIC MODEL
############################################################

# Draw quasi-random sample from the pc
set.seed(34838)

model2 <- FitLdaModel(dtm = dtm2, 
                     k = 8,
                     iterations = 500,
                     burnin = 200,
                     alpha = 0.1,
                     beta = 0.05,
                     optimize_alpha = TRUE,
                     calc_likelihood = TRUE,
                     calc_coherence = TRUE,
                     calc_r2 = TRUE,
                     cpus = 4) 

那么问题是: 1. 我应该应用哪个函数来获取 textmineR 包中的困惑分数?我好像找不到。
2. 如何衡量不同主题数 (k) 的复杂性得分?

正如所问:除非您自己明确编程,否则无法使用 textmineR 计算困惑度。 TBH,我从来没有见过你无法通过可能性和连贯性获得的困惑价值,所以我没有实现它。

但是,text2vec 包确实有一个实现。见下面的例子:

library(textmineR)

# model ships with textmineR as example
m <- nih_sample_topic_model

# dtm ships with textmineR as example
d <- nih_sample_dtm

# get perplexity
p <- text2vec::perplexity(X = d, 
                          topic_word_distribution = m$phi, 
                          doc_topic_distribution = m$theta)