The tidytext book has examples with a tidier for topicmodels:
library(tidyverse)
library(tidytext)
library(topicmodels)
library(broom)
year_word_counts <- ti
Adding to the very helpful answer provided by Julia Silge:
I too believe that the interaction between loading .Rdata and the topicmodels package is the culprit here. But you can still work with your saved workspace:
I was able to eliminate the problem by starting with a fresh restart of RStudio, loading the topicmodels package and then loading the .Rdata. Done in this sequence, the error message disappears. Loading first the data and then the package does not work.
One more word on workspaces: In the case of LDA, using these along with your RScripts is really the only way I could figure out to work efficiently. Depending on the parameters and the size of the corpus, fitting an LDA-model may take several hours. It is crucial to be able to save the model fit to then do further analyses down the road.
Deleting .Rhistory and .RData led to correct behaviour.
Wow, that is extremely mysterious to me. I am not able to reproduce that error. I installed to all the same versions/etc as you, except that I am on MacOS instead of Windows. I do have tests for the LDA tidiers that run and pass on Windows on Appveyor, so I would expect this to work.
The code you have should work without loading broom, for what it's worth.
library(tidyverse)
library(tidytext)
library(topicmodels)
year_word_counts <- tibble(year = c("2007", "2008", "2009"),
word = c("dog", "cat", "chicken"),
n = c(1753L, 1157L, 1057L))
animal_dtm <- cast_dtm(data = year_word_counts, document = year, term = word, value = n)
animal_lda <- LDA(animal_dtm, k = 5, control = list( seed = 1234))
class(animal_lda)
#> [1] "LDA_VEM"
#> attr(,"package")
#> [1] "topicmodels"
tidy(animal_lda, matrix = "beta")
#> # A tibble: 15 x 3
#> topic term beta
#> <int> <chr> <dbl>
#> 1 1 dog 0.0000000000000000000000000000000000000000000372
#> 2 2 dog 0.0000000000000000000000000000000000000000000372
#> 3 3 dog 0.0000000000000000000000000000000000000000000372
#> 4 4 dog 1.00
#> 5 5 dog 0.0000000000000000000000000000000000000000000372
#> 6 1 cat 0.0000000000000000000000000000000000000000000372
#> 7 2 cat 0.0000000000000000000000000000000000000000000372
#> 8 3 cat 0.0000000000000000000000000000000000000000000372
#> 9 4 cat 0.0000000000000000000000000000000000000000000372
#> 10 5 cat 1.00
#> 11 1 chicken 0.0000000000000000000000000000000000000000000372
#> 12 2 chicken 0.0000000000000000000000000000000000000000000372
#> 13 3 chicken 1.00
#> 14 4 chicken 0.0000000000000000000000000000000000000000000372
#> 15 5 chicken 0.0000000000000000000000000000000000000000000372
Created on 2018-02-14 by the reprex package (v0.2.0).
What happens if you load library(methods)
as well?
I had the same issue when I loaded the LDA I had saved. Finally, for no apparent reasons when I restarted the R session I worked again.