0

I am literally following the steps as presented in chapter 6 of the "Text Mining in R: a Tidy Approach" book. See: 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")

Gives me the following error in the terminal:

Error: No tidy method for objects of class LDA_VEM

What I should be getting, meanwhile is:

## # 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

Why am I seeing this error instead of the desired result?

Vasino
  • 3
  • 1
  • 2

2 Answers2

1

You are not the first person to run into this problem, but it is very difficult to reproduce. In fact, I personally have never been able to reproduce the error. However, I know it is a real problem because, well... If you'd like to see other people also struggling, check out here or here or here or here.

As best as I can tell, this is likely a bug in the topicmodels package, related to S4 registration. You can avoid running into this by not saving .RData between sessions and always starting from a fresh R session when you open up work.

Julia Silge
  • 10,848
  • 2
  • 40
  • 48
0

You need to first tidy the AssociatedPress data. Like this:

#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)

And then:

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

Which gives:

 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
NelsonGon
  • 13,015
  • 7
  • 27
  • 57