Using tidytext and broom but not finding tidier for LDA_VEM

后端 未结 4 2004
春和景丽
春和景丽 2021-01-22 21:16

The tidytext book has examples with a tidier for topicmodels:

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

year_word_counts <- ti         


        
4条回答
  •  悲&欢浪女
    2021-01-22 22:00

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

提交回复
热议问题