使用单个协变量为主题建模运行stm的问题

问题描述 投票:0回答:1

我正在尝试使用stm运行LDA主题建模分析,但是我的元数据有问题,它似乎工作正常,但我有一个协方差(Age),如本示例所示,未被读取。

我有一些推文(excel文件中的文档列),带有Age covariate(Young,Old)值。

这是我的数据http://www.mediafire.com/file/5eb9qe6gbg22o9i/dada.xlsx/file

library(stm)
library(readxl)
library(quanteda)
library(stringr)
library(tm)


data <-  read_xlsx("C:/dada.xlsx") 

#Remove URL's 
data$docu <- str_replace_all(data$docu, "https://t.co/[a-z,A-Z,0-9]*","")


data$docu <- gsub("@\\w+", " ", data$docu)  # Remove user names (all proper names if you're wise!)

data$docu <- iconv(data$docu, to = "ASCII", sub = " ")  # Convert to basic ASCII text to avoid silly characters
data$docu <- gsub("#\\w+", " ", data$docu)

data$docu <- gsub("http.+ |http.+$", " ", data$docu)  # Remove links

data$docu <- gsub("[[:punct:]]", " ", data$docu)  # Remove punctuation)

data$docu<-  gsub("[\r\n]", "", data$docu)

data$docu <- tolower(data$docu)



#Remove Stopwords. "SMART" is in reference to english stopwords from the SMART information retrieval system and stopwords from other European Languages.
data$docu <- tm::removeWords(x = data$docu, c(stopwords(kind = "SMART")))

data$docu <- gsub(" +", " ", data$docu) # General spaces (should just do all whitespaces no?)

myCorpus <- corpus(data$docu)
docvars(myCorpus, "Age") <- as.factor(data$Age)


processed <- textProcessor(data$docu, metadata = data)

out <- prepDocuments(processed$documents, processed$vocab, processed$meta, lower.thresh = 2)

out$documents
out$meta
levels(out$meta)

First_STM <- stm(documents = out$documents, vocab = out$vocab,
                 K = 4, prevalence =~ Age ,
                 max.em.its = 25, data = out$meta,
                 init.type = "LDA", verbose = FALSE)

如我在代码中所示,我试图将Age定义为因子,我认为这不是必需的,因为运行textProcessor可能已经足够了......但是当我运行levels(out$meta)时,我得到NULL值,所以当我运行stm以获得我得到的实际主题内存分配错误..

r topic-modeling
1个回答
2
投票

您将Age的元变量设置为此行中的因子

docvars(myCorpus, "Age") <- as.factor(data$Age)

但是你不要再使用myCorpus了。在接下来的步骤中,您将使用数据框data进行预处理。尝试将数据框中的Age定义为因子:

data$Age <- factor(data$Age)

然后在这之前使用它

processed <- textProcessor(data$docu, metadata = data)

out <- prepDocuments(processed$documents, processed$vocab, processed$meta, lower.thresh = 2)

然后你可以看看这样的水平:

levels(out$meta$Age)

我无法重现你的内存分配错误。 stm在我的机器上工作正常(Win 10 Pro,8GB Ram)。

© www.soinside.com 2019 - 2024. All rights reserved.