我正在尝试使用来自topicmodels
. groupby()
虽然使用over document 并在 gamma 上选择很容易从文档中提取最有可能的预测主题top_n()
,但在“beta”估计中,唯一的文档 id 将在输出中被抑制,输出仅包含三列 ( topic
, term
, beta
) . 这不允许人们从给定文档的术语中获得“共识”主题预测(测试版)。
以我自己的数据为例:
Sys.setlocale("LC_ALL","Chinese") # reset to simplified Chinese encoding as the text data is in Chinese
library(foreign)
library(dplyr)
library(plyr)
library(tidyverse)
library(tidytext)
library(tm)
library(topicmodels)
sample_dtm <- readRDS(gzcon(url("https://www.dropbox.com/s/gznqlncd9psx3wz/sample_dtm.rds?dl=1")))
lda_out <- LDA(sample_dtm, k = 2, control = list(seed = 1234))
word_topics <- tidy(lda_out, matrix = "beta")
head(word_topics, n = 4)
# A tibble: 6 x 3
topic term beta
<int> <chr> <dbl>
1 1 费解 8.49e- 4
2 2 费解 1.15e- 9
3 1 上 2.92e- 3
document_gamma <- tidy(lda_out, matrix = "gamma")
head(document_gamma, n = 4)
# A tibble: 6 x 3
document topic gamma
<chr> <int> <dbl>
1 1203232 1 0.00374
2 529660 1 0.0329
3 738921 1 0.00138
4 963374 1 0.302
无论如何我可以从lda
输出中恢复文档 id 并与beta
估计(word_topics
存储为data.frame
对象)结合起来吗?beta
这样从 的共识与的共识中比较估计的主题会容易得多gamma
。