欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

结构主题模型(二)复现

程序员文章站 2022-06-04 09:07:46
...

简介:整理在复现论文(stm An R Package for Structural Topic Models)代码过程中的注意事项和报错信息


复现代码

下载代码及数据1

文件名 解释
v091i02.R stm代码
poliblogs2008.csv 数据
results.rda 模型训练结束后,保存的变量文件

导包

stm包依赖R(≥ 3.5.0)2

install.packages("stm")

导入数据

导入论文作者提供的csv文件后,总是报各种类型的错误
问题一:read.table()[^R_fx_xlsx]

Warning messages: 1: In read.table(file = file, header = header, sep = sep, quote = quote,  :  invalid input found on input connection 'data_preprocessing_test1a.csv' 
2: In read.table(file = file, header = header, sep = sep, quote = quote,  :  incomplete final line found by readTableHeader on 'data_preprocessing_test1a.csv'

问题二:列名和列的数量不一致34

Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names

问题三:EOF with quoted string5

In scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  :   EOF within quoted string

问题四:对于中文文本,易因换行/n等其他分隔符使得程序对csv文件解析错误6

导入.xlsx./xls格式的数据就能避免和解决上述问题7

install.packages("readxl")
library(readxl)
data <- read_excel(path="data_preprocessing_test1a.xlsx", sheet="your_sheet_name", col_names= TRUE)

代码执行

问题一

system is computationally singular: reciprocal condition number = 1.06304e-19

存在矩阵不可逆,对于矩阵A而言,det(A)=/≈0都会使得A不可逆。

R中函数det()用于计算矩阵的行列式

matrix - System is computationally singular: reciprocal condition number in R - Stack Overflow

问题二

K=2 is equivalent to a unidimensional scaling model which you may prefer.

选择主题数时不宜选择等于或低于2的主题数,这样的话,结构主题模型就不能发挥较大的作用

问题三

文本数量和模型中的文档数量不匹配89

Error in findThoughts(out.stm, topics = 27, texts = corpus$documents$texts, : Number of provided texts and number of documents modeled do not match


参考文章:


  1. stm: An R Package for Structural Topic Models (jstatsoft.org) ↩︎

  2. CRAN - Package stm (r-project.org) ↩︎

  3. R Error more columns than column names - ProgrammingR ↩︎

  4. R语言“More Columns than Column Names” - 简书 (jianshu.com) ↩︎

  5. In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : EOF within quoted string_liuhuanheng的专栏-CSDN博客 ↩︎

  6. python去掉换行符“\n”的方法 - 编程语言 - 亿速云 (yisu.com) ↩︎

  7. CSV import problem on R - General - RStudio Community ↩︎

  8. R STM - 提供文本和建模不匹配 - VoidCC ↩︎

  9. R stm - Number of provided texts and number of documents modeled do not match - Stack Overflow ↩︎