简介:整理在复现论文(stm An R Package for Structural Topic Models)代码过程中的注意事项和报错信息
复现代码
下载代码及数据:stm: An R Package for Structural Topic Models (jstatsoft.org)
文件名 | 解释 |
---|
v091i02.R | stm代码 | poliblogs2008.csv | 数据 | results.rda | 模型训练结束后,保存的变量文件 |
导包
stm包依赖R(≥ 3.5.0)
install.packages("stm")
导入数据
导入论文作者提供的csv文件后,总是报各种类型的错误,具体错误类型如下: 问题一:read.table()
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'
问题二:列名和列的数量不一致
Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names
问题三:EOF with quoted string
In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : EOF within quoted string
问题四:对于中文文本,易因换行/n 等其他分隔符使得程序对csv文件解析错误
针对以上问题,建议使用.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不可逆。
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
以上解决方式并未解决博主的问题
参考文章:
|