请容我先说明一下,我对这个领域非常陌生,正在完成一个学位课程的项目。我有一个.csv数据集,是从Pubmed和Embase数据库检索到的文献记录。数据集共有1034行,包含多个字段,然而,我只想从摘要字段创建话题模型,而且某些记录没有摘要。我进行了一些处理(去除停用词、标点符号等),并成功地制作了出现次数超过200次的单词条形图,并按排名创建了一个常见术语列表,还可以通过选定的单词运行词汇联想。因此,看起来R确实在摘要字段中看到了单词本身。然而,当我尝试使用topicmodels软件包创建话题模型时,问题就来了。以下是我正使用的代码部分。
然而,我得到的主题输出如下。
有什么想法,可能是什么问题?
#including 1st 3 lines for reference
options(header = FALSE, stringsAsFactors = FALSE, FileEncoding =
"latin1")
records <- read.csv("Combined.csv")
AbstractCorpus <- Corpus(VectorSource(records$Abstract))
AbstractTDM <- TermDocumentMatrix(AbstractCorpus)
library(topicmodels)
library(lda)
lda <- LDA(AbstractTDM, k = 8)
(term <- terms(lda, 6))
term <- (apply(term, MARGIN = 2, paste, collapse = ","))
然而,我得到的主题输出如下。
Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 Topic 6 Topic 7 Topic 8
[1,] "499" "733" "390" "833" "17" "413" "719" "392"
[2,] "484" "655" "808" "412" "550" "881" "721" "61"
[3,] "857" "299" "878" "909" "15" "258" "47" "164"
[4,] "491" "672" "313" "1028" "126" "55" "375" "987"
[5,] "734" "430" "405" "102" "13" "193" "83" "588"
[6,] "403" "52" "489" "10" "598" "52" "933" "980"
为什么我在这里看到的不是单词而是数字?
此外,下面的代码基本上是我从主题模型的R PDF中获取的,它确实为我生成了值,但主题仍然是数字而不是单词,这对我来说毫无意义。
#using information from topicmodels paper
library(tm)
library(topicmodels)
library(lda)
AbstractTM <- list(VEM = LDA(AbstractTDM, k = 10, control = list(seed =
505)), VEM_fixed = LDA(AbstractTDM, k = 10, control = list(estimate.alpha
= FALSE, seed = 505)), Gibbs = LDA(AbstractTDM, k = 10, method = "Gibbs",
Control = list(seed = 505, burnin = 100, thin = 10, iter = 100)), CTM =
CTM(AbstractTDM, k = 10, control = list(seed = 505, var = list(tol =
10^-4), em = list(tol = 10^-3))))
#To compare the fitted models we first investigate the α values of the
models fitted with VEM and α estimated and with VEM and α fixed
sapply(AbstractTM[1:2], slot, "alpha")
#Find entropy
sapply(AbstractTM, function(x)mean(apply(posterior(x)$topics, 1,
function(z) - sum(z * log(z)))))
#Find estimated topics and terms
Topic <- topics(AbstractTM[["VEM"]], 1)
Topic
#find 5 most frequent terms for each topic
Terms <- terms(AbstractTM[["VEM"]], 5)
Terms[,1:5]
有什么想法,可能是什么问题?
DocumentTermMatrix()
而不是TermDocumentMatrix()
? - Kara Woodput(head(records$Abstract, 10))
的输出,或者产生相同问题的玩具数据集? - Kara Wooabstracts <- records$Abstract[records$Abstract != ""]
,然后创建语料库和 DTM。 - Kara Woo