结构主题模型(二)复现
简介:整理在复现论文(stm An R Package for Structural Topic Models)代码过程中的注意事项和报错信息
结构主题模型专题文章:
复现代码
下载代码及数据:
导包
stm包依赖R(≥ 3.5.0)
install.packages("stm")
导入数据
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
可能的解决方法(未解决博主的问题):