概述
简介:整理在复现论文(stm An R Package for Structural Topic Models
)代码过程中的注意事项和报错信息
结构主题模型专题文章:结构主题模型(一)stm包工作流
复现代码
下载代码及数据:stm: An R Package for Structural Topic Models (jstatsoft.org)
文件名 | 解释 |
---|---|
v091i02.R | stm代码 |
poliblogs2008.csv | 数据 |
results.rda | 模型训练结束后,保存的变量文件 |
导包
stm包依赖R(≥ 3.5.0)1
install.packages("stm")
导入数据
导入论文作者提供的csv文件后,总是报各种类型的错误,具体错误类型如下:
问题一:read.table()2
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'
问题二:列名和列的数量不一致3 4
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
格式的数据
- 先使用python将csv转excel
import pandas as pd
csv_file = pd.read_csv('data.csv', low_memory=False, encoding='utf-8')
csv_file.to_excel('data.xlsx', index=False, encoding='utf-8')
- R导入excel数据
install.packages("readxl")
library(readxl)
data <- read_excel(path="data.xlsx", sheet="your_sheet_name", col_names= TRUE)
代码执行
问题一
system is computationally singular: reciprocal condition number = 1.06304e-19
可能的原因:存在矩阵不可逆,对于矩阵A而言,det(A)=/≈0都会使得A不可逆。7
R中函数det()用于计算矩阵的行列式
问题二
K=2 is equivalent to a unidimensional scaling model which you may prefer.
选择主题数时不宜选择等于或低于2的主题数,这样的话,结构主题模型就不能发挥较大的作用
问题三
文本数量和模型中的文档数量不匹配
Error in findThoughts(out.stm, topics = 27, texts = corpus$documents$texts, : Number of provided texts and number of documents modeled do not match
可能的解决方法(未解决博主的问题):
- R STM - 提供文本和建模不匹配 - VoidCC
- R stm - Number of provided texts and number of documents modeled do not match - Stack Overflow
参考文章:
CRAN - Package stm (r-project.org) ↩︎
CSV import problem on R - General - RStudio Community ↩︎
R Error more columns than column names - ProgrammingR ↩︎
R语言“More Columns than Column Names” - 简书 (jianshu.com) ↩︎
In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : EOF within quoted string_liuhuanheng的专栏-CSDN博客 ↩︎
python去掉换行符“n”的方法 - 编程语言 - 亿速云 (yisu.com) ↩︎
matrix - System is computationally singular: reciprocal condition number in R - Stack Overflow ↩︎
最后
以上就是文静雪碧为你收集整理的结构主题模型(二)复现的全部内容,希望文章能够帮你解决结构主题模型(二)复现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复