我不是 100% 确定您所说的“错误”是什么意思。我做了一个快速测试,看看posterior 是否适用于新数据。首先,我使用AssociatedPress 数据集的所有文档运行一个模型:
library(topicmodels)
data("AssociatedPress")
ap_lda <- LDA(AssociatedPress, k = 5, control = list(seed = 1234))
根据您的问题,我怀疑您在这里查看每个文档最可能的主题。为了保持可比性,我基于一些整洁的包构建了自己的方法来在这里找到它们:
library(tidytext)
library(dplyr)
library(tidyr)
ap_documents <- tidy(ap_lda, matrix = "gamma")
ap_documents %>%
group_by(document) %>%
top_n(1, gamma) %>% # keep only most likely topic
arrange(document)
# A tibble: 2,246 x 3
# Groups: document [2,246]
document topic gamma
<int> <int> <dbl>
1 1 4 0.999
2 2 2 0.529
3 3 4 0.999
4 4 4 0.518
5 5 4 0.995
6 6 2 0.971
7 7 1 0.728
8 8 2 0.941
9 9 4 0.477
10 10 5 0.500
# ... with 2,236 more rows
现在我再次运行相同的 LDA,但保留前 10 个文档:
AssociatedPress_train <- AssociatedPress[11:nrow(AssociatedPress), ]
AssociatedPress_test <- AssociatedPress[1:10, ]
ap_lda <- LDA(AssociatedPress_train, k = 5, control = list(seed = 1234))
我使用posterior 获取每个文档的 gamma 值,并再次保留最有可能的值:
posterior(object = ap_lda, newdata = AssociatedPress_test)$topics %>%
as_tibble() %>%
mutate(document = seq_len(nrow(.))) %>%
gather(topic, gamma, -document) %>%
group_by(document) %>%
top_n(1, gamma) %>% # keep only most probable topic
arrange(document)
# A tibble: 10 x 3
# Groups: document [10]
document topic gamma
<int> <chr> <dbl>
1 1 4 0.898
2 2 2 0.497
3 3 4 0.896
4 4 4 0.468
5 5 4 0.870
6 6 2 0.754
7 7 1 0.509
8 8 2 0.913
9 9 4 0.476
10 10 2 0.399
除了文档 10 之外的所有文档都具有与以前相同的最可能主题。所以一切似乎都很好!所以我看不出你的代码有什么直接的问题。
我没有测试过的一件事是,如果训练集和测试集的 DTM 具有不同的列,会发生什么情况。我怀疑这会是个问题。
这里有一个简单的例子来说明你如何处理这个问题:
text1 <- tibble(doc = 1, word = LETTERS[1:10])
text2 <- tibble(doc = 1, word = LETTERS[2:11])
dtm1 <- text1 %>%
count(doc, word) %>%
arrange(word) %>%
cast_dtm(doc, word, n)
dtm2 <- text2 %>%
count(doc, word) %>%
arrange(word) %>%
cast_dtm(doc, word, n)
all.equal(dtm1$dimnames$Terms, dtm2$dimnames$Terms)
[1] "10 string mismatches"
我制作了两个 DTM,其中第二个有一个额外的术语,而另一个缺少一个术语。因此,dimnames 是不同的。我们可以通过将 DTM 恢复为整洁的格式来使它们相等,删除多余的术语并在再次转换 DTM 之前添加缺失的术语:
dtm2_clean <- tidy(dtm2) %>%
filter(term %in% dtm1$dimnames$Terms) %>%
rbind(tibble(document = 1,
term = dtm1$dimnames$Terms, # adding term but no counts
count = 0)) %>%
arrange(term) %>%
cast_dtm(document, term, count)
all.equal(dtm1$dimnames$Terms, dtm2_clean$dimnames$Terms)
[1] TRUE
您现在可以将其用作后验的新数据。