ldatuning 包可以帮助您找到主题模型的数量。请参阅下面的代码。注意不要运行完整的关联新闻数据集。这可能会运行几个小时。
为了调整,使用了几个指标。您可以在带有 ldatuning的小插图的参考资料中阅读这些内容。
library(ldatuning)
library(topicmodels)
data("AssociatedPress", package="topicmodels")
my_dtm <- AssociatedPress[1:20,]
result <- FindTopicsNumber(
my_dtm,
topics = seq(from = 2, to = 10, by = 1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010", "Deveaud2014"),
method = "Gibbs",
control = list(seed = 4242),
mc.cores = 3L,
verbose = TRUE
)
fit models... done.
calculate metrics:
Griffiths2004... done.
CaoJuan2009... done.
Arun2010... done.
Deveaud2014... done.
result
topics Griffiths2004 CaoJuan2009 Arun2010 Deveaud2014
1 10 -29769.77 0.2049923 32.15563 0.3651554
2 9 -29679.41 0.1913860 32.07003 0.4018582
3 8 -29682.97 0.1619718 32.45093 0.4407269
4 7 -29617.64 0.1556135 33.58472 0.4908904
5 6 -29632.34 0.1247883 33.04505 0.5502962
6 5 -29634.21 0.1201017 34.07776 0.6244967
7 4 -29685.18 0.1134287 35.96230 0.7129967
8 3 -29864.36 0.1070237 38.18795 0.8194811
9 2 -30216.09 0.1040786 42.01731 0.9678864
FindTopicsNumber_plot(result)

基于 griffiths 的 5 个主题将是一个不错的选择。作者:Devaud 2,Arun 9。因此,让我们在此之上运行一组不同的主题。我也添加了 3 个,但阅读了每个指标。
no_topics <- c(2, 3, 5, 9)
lda_list <- lapply(no_topics, function(k) LDA(k = k,
x = my_dtm,
method = "Gibbs",
control = control_list_gibbs
)
)
names(lda_list) <- paste0("no_", no_topics)
lda_list
$no_2
A LDA_Gibbs topic model with 2 topics.
$no_3
A LDA_Gibbs topic model with 3 topics.
$no_5
A LDA_Gibbs topic model with 5 topics.
$no_9
A LDA_Gibbs topic model with 9 topics.
在此之后,将检查 lda 结果以查看其中是否有任何好处。
有关此主题的深入概述,您可以阅读此博文。作者使用 purrr、tidytext、dplyr 和 ggplot2 来研究数据集。
这是一篇关于将交叉验证与 ldatuning 和 topicmodels 一起使用的博客文章。