Search code examples
rquanteda

Using a dfm for structural model


Having a dfm from this process:

library(quanteda)
df <- data.frame(text = c("one text here", "one more here and there"))
toks_tweets <- tokens(df$text, remove_punct = TRUE)
dfmat_tweets <- dfm(toks_tweets,
    stem = FALSE,
    remove_punct = TRUE)

How is it possible to use it for the structural modeling like this:

library(stm)
fittedModel <- stm(documents = out$documents, vocab = out$vocab, K = 3, init.type = "Spectral")

Solution

  • You need to use the function quanteda::convert. This function can transform the dfm into different formats for different packages. See ?convert for all the options.

    See example below for the solution to your example.

    library(quanteda)
    df <- data.frame(text = c("one text here", "one more here and there"), stringsAsFactors = FALSE)
    toks_tweets <- tokens(df$text, remove_punct = TRUE)
    dfmat_tweets <- dfm(toks_tweets,
                        stem = FALSE,
                        remove_punct = TRUE)
    
    out <- convert(dfmat_tweets, to = "stm")  # convert to stm format
    
    library(stm)
    fittedModel <- stm(documents = out$documents, vocab = out$vocab, K = 3, init.type = "Spectral")
    
    fittedModel
    # A topic model with 3 topics, 2 documents and a 6 word dictionary.