将 dfm 用于结构模型

Using a dfm for structural model

从这个过程中获得 dfm:

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)

如何将它用于这样的结构建模:

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

您需要使用函数quanteda::convert。该函数可以将不同包的dfm转换成不同的格式。请参阅 ?convert 了解所有选项。

请参阅下面的示例以了解您的示例的解决方案。

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.