dgrtwo / tidy-text-mining

Manuscript of the book "Tidy Text Mining with R" by Julia Silge and David Robinson
http://tidytextmining.com
Other
1.31k stars 806 forks source link

tidy() error: cannot coerce class to a data.frame #34

Closed angoodkind closed 7 years ago

angoodkind commented 7 years ago

Loving this tutorial. Everything works fine until 6.1.2: When I run ap_documents <- tidy(ap_lda, matrix = "gamma") I get:

Error in as.data.frame.default(x) : 
  cannot coerce class "structure("LDA_VEM", package = "topicmodels")" to a data.frame
In addition: Warning message:
In tidy.default(ap_lda, matrix = "gamma") :
  No method for tidying an S3 object of class LDA_VEM , using as.data.frame

Full code:

library(topicmodels)
library(tidytext)
library(tm)
library(ggplot2)
library(tidyr)
library(dplyr)
library(broom)

data("AssociatedPress")
ap_lda <- LDA(AssociatedPress, k = 2, control = list(seed = 1234))
ap_documents <- tidy(ap_lda, matrix = "gamma")
juliasilge commented 7 years ago

Hmmmmm, I am not able to reproduce this error. :confused:

library(tidyverse)
library(tidytext)
library(broom)
library(topicmodels)

data("AssociatedPress", package = "topicmodels") 
AssociatedPress
#> <<DocumentTermMatrix (documents: 2246, terms: 10473)>>
#> Non-/sparse entries: 302031/23220327
#> Sparsity           : 99%
#> Maximal term length: 18
#> Weighting          : term frequency (tf)

ap_lda <- LDA(AssociatedPress, k = 2, control = list(seed = 1234)) 
ap_lda
#> A LDA_VEM topic model with 2 topics.

ap_documents <- tidy(ap_lda, matrix = "gamma")
ap_documents
#> # A tibble: 4,492 x 3
#>    document topic        gamma
#>       <int> <int>        <dbl>
#>  1        1     1 0.2480616686
#>  2        2     1 0.3615485445
#>  3        3     1 0.5265844180
#>  4        4     1 0.3566530023
#>  5        5     1 0.1812766762
#>  6        6     1 0.0005883388
#>  7        7     1 0.7734215655
#>  8        8     1 0.0044516994
#>  9        9     1 0.9669915139
#> 10       10     1 0.1468904793
#> # ... with 4,482 more rows

Do you have updated versions of these packages and R?

angoodkind commented 7 years ago

Ugh, I restarted R and the error went away. Sorry about this. Love the tutorial.