我正在尝试使用topicmodels
中的函数将术语(在给定文档中)的“共识”主题预测(beta)与文档本身(gamma)中最有可能预测的主题进行比较。
尽管在文档上使用groupby()
并在gamma上选择top_n()
可以很容易地从文档中提取出最可能预测的主题,但是在“ beta”估算中,唯一文档ID将在输出中被抑制,输出仅包含三列(topic
,term
,beta
)。这不允许人们从给定文档的术语中获得“共识”主题预测(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
的共识估计的主题进行比较就容易得多。
答案 0 :(得分:0)
如果我对您的理解正确,我相信您想要的功能是augment()
,该函数将返回一个表,其中每个原始文档术语对均包含一行,并与主题相关。
Sys.setlocale("LC_ALL","Chinese") # reset to simplified Chinese encoding as the text data is in Chinese
#> Warning in Sys.setlocale("LC_ALL", "Chinese"): OS reports request to set
#> locale to "Chinese" cannot be honored
#> [1] ""
library(foreign)
library(dplyr)
library(plyr)
#> -------------------------------------------------------------------------
#> You have loaded plyr after dplyr - this is likely to cause problems.
#> If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
#> library(plyr); library(dplyr)
#> -------------------------------------------------------------------------
#>
#> Attaching package: 'plyr'
#> The following objects are masked from 'package:dplyr':
#>
#> arrange, count, desc, failwith, id, mutate, rename, summarise,
#> summarize
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))
augment(lda_out, sample_dtm)
#> # A tibble: 18,676 x 4
#> document term count .topic
#> <chr> <chr> <dbl> <dbl>
#> 1 649 作揖 1 1
#> 2 649 拳头 1 1
#> 3 649 赞 1 1
#> 4 656 住 1 1
#> 5 656 小区 1 1
#> 6 656 没 1 1
#> 7 656 注意 2 1
#> 8 1916 中国 1 1
#> 9 1916 中国台湾 1 1
#> 10 1916 反对 1 1
#> # … with 18,666 more rows
由reprex package(v0.2.1)于2019-06-04创建
这将文档ID从LDA模型连接到主题。听起来您已经了解了这一点,但只需重申一下:
beta
矩阵是单词主题概率gamma
矩阵是文档主题概率