Error: No tidy method for objects of class LDA_VEM§

Error: No tidy method for objects of class LDA_VEM§

我严格按照 "Text Mining in R: a Tidy Approach" 书第 6 章中介绍的步骤进行操作。参见:https://www.tidytextmining.com/topicmodeling.html

#import libraries
library(topicmodels)
library(tidytext)

#access dataset
data("AssociatedPress")

# set a seed so that the output of the model is predictable
ap_lda <- LDA(AssociatedPress, k = 2, control = list(seed = 1234))

#tidy model object
ap_topics <- tidy(ap_lda, matrix = "beta")

在终端中给我以下错误:

Error: No tidy method for objects of class LDA_VEM

同时我应该得到的是:

## # A tibble: 20,946 x 3
##    topic term           beta
##    <int> <chr>         <dbl>
##  1     1 aaron      1.69e-12
##  2     2 aaron      3.90e- 5
##  3     1 abandon    2.65e- 5
##  4     2 abandon    3.99e- 5
##  5     1 abandoned  1.39e- 4
##  6     2 abandoned  5.88e- 5
##  7     1 abandoning 2.45e-33
##  8     2 abandoning 2.34e- 5
##  9     1 abbott     2.13e- 6
## 10     2 abbott     2.97e- 5
## # ... with 20,936 more rows

为什么我看到的是这个错误,而不是我想要的结果?

您需要先整理一下美联社的数据。像这样:

#if(!require("topicmodels")) install.packages("topicmodels")
#install.packages("topicmodels")
library(topicmodels)
data("AssociatedPress",package="topicmodels")
AssociatedPress
#Getting the Terms
terms<-Terms(AssociatedPress)
head(terms)
#tidyig with the tidy function
ap_tidy<-tidy(AssociatedPress)

然后:

ap_lda<-LDA(AssociatedPress,k=2,control=list(seed=1234))
ap_topics<-tidy(ap_lda,matrix="beta")
head(ap_topics)

给出:

 topic term          beta
  <int> <chr>        <dbl>
1     1 aaron     1.69e-12
2     2 aaron     3.90e- 5
3     1 abandon   2.65e- 5
4     2 abandon   3.99e- 5
5     1 abandoned 1.39e- 4
6     2 abandoned 5.88e- 5

您不是第一个 运行 遇到此问题的人,但很难重现。事实上,我个人从未能够重现该错误。但是,我知道这是一个真正的问题,因为,好吧...如果您希望看到其他人也在苦苦挣扎,请查看 here or here or here or here

据我所知,这可能是 topicmodels 包中与 S4 注册相关的错误。您可以通过在会话和 always starting from a fresh R session when you open up work.

之间不保存 .RData 来避免 运行ning 进入此状态