0

This code shows an error : Error: No tidy method for objects of class LDA_Gibbs

I don't understand why.....whenever i try to do test_lda_td2 <- tidy(test_lda2) this shows error. Need some solutions

library(NLP)
library(tm)
library(tidytext)
library(stringr)
library(tidyr)
library(topicmodels)
library(tidyverse)

mywords<- read.csv("mystop.csv",header =F,sep = ",")
mywords <- as.character(mywords)
mywords <- c(mywords, stopwords())

filelist = list.files(pattern = ".*.txt")
files <- lapply(filelist,readLines)

(docs <- VCorpus(VectorSource(files)))

docs <-tm_map(docs,content_transformer(tolower))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, removeNumbers)
docs <- tm_map(docs, removeWords, mywords)

docs <- tm_map(docs, removeWords,stopwords("english"))
docs <- tm_map(docs, stripWhitespace)

toSpace <- content_transformer(function(x, pattern) { return (gsub(pattern, 
" " , x))})
docs <- tm_map(docs, toSpace, "-")
docs <- tm_map(docs, toSpace, "’")
docs <- tm_map(docs, toSpace, "‘")
docs <- tm_map(docs, toSpace, "•")
docs <- tm_map(docs, toSpace, "”")
docs <- tm_map(docs, toSpace, "“")

#...............
dtm <- DocumentTermMatrix(docs)
rownames(dtm) <- filelist
freq <- colSums(as.matrix(dtm))
length(freq)
ord <- order(freq,decreasing=TRUE)

d <- data.frame(word = names(freq),freq=freq[ord])

new_dtm<-dtm

burnin <- 4000
iter <- 2000
thin <- 500
seed <-list(2003,5,63,100001,765)
nstart <- 5
best <- TRUE
k <- 3
test_lda2 <-LDA(new_dtm,k, method="Gibbs",
control=list(nstart=nstart, seed = seed, best=best,
burnin = burnin, iter = iter, thin=thin))

test_lda_td2 <- tidy(test_lda2) <---Error

lda_top_terms2 <- test_lda_td2 %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)

lda_top_terms2 %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_bar(stat = "identity", show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
coord_flip()
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Mir Satra
  • 1
  • 1
  • 3
  • Welcome to SO! You might get more of a response if you are able to reduce your code and provide a more reproducible example. For the former, scrolling pages of code can be a deterrent, especially when it seems clear that most of it is not required to reproduce the problem/error. Good refs for how to ask a good and small reproducible question: https://stackoverflow.com/questions/5963269/, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Sep 12 '18 at 08:14
  • Have you actually read the error? There are no tidy methods for `topicmodels::LDA`. Look at the broom [available methods vignette](https://cran.r-project.org/web/packages/broom/vignettes/available-methods.html) to see which methods broom can tidy. – phiver Sep 12 '18 at 10:16
  • Thanks for your help @phiver – Mir Satra Sep 12 '18 at 17:52

1 Answers1

0

There may be a function with same name in multiple packages. And, the latest one you have called in "library()" takes precedence. Suggest to try calling the function with the package name and check if you still get the error.

.()

For example :- tidyr.tydy()