基于STM模型计算Reddit仇恨言论分类对应主题占比的技术咨询
Reddit仇恨言论STM主题概率计算方案
前提:已完成STM模型拟合,且所有待分析帖子都完成了仇恨言论类别标注。
两类结果实现代码
# 加载依赖包 library(stm) library(dplyr) # 标注数据示例(修正原示例的语法错误) post_data <- data.frame( content = c("i dislike asian", "i dislike black"), anti_asian = c(TRUE, FALSE), anti_black = c(FALSE, TRUE) ) # 提取STM模型输出的帖子-主题归属概率矩阵 # theta矩阵每行对应1条帖子,每列对应1个主题,值为该帖子属于对应主题的概率 theta_matrix <- fitted_stm_model$theta # 为每条帖子匹配概率最高的主导归属主题 post_data$dominant_topic <- apply(theta_matrix, 1, which.max) # ---------------------- # 第一类结果:各主题下不同仇恨言论占比 # ---------------------- topic_hate_ratio <- post_data %>% group_by(dominant_topic) %>% summarise( post_count = n(), 反亚裔占比 = round(sum(anti_asian)/post_count * 100, 2), 反黑人占比 = round(sum(anti_black)/post_count * 100, 2) # 存在其他仇恨类别可按上述格式补充 ) # 输出第一类结果 View(topic_hate_ratio) # ---------------------- # 第二类结果:各仇恨言论在全部主题中的整体占比 # ---------------------- global_hate_ratio <- post_data %>% summarise( total_post = n(), 反亚裔整体占比 = round(sum(anti_asian)/total_post * 100, 2), 反黑人整体占比 = round(sum(anti_black)/total_post * 100, 2) # 存在其他仇恨类别可按上述格式补充 ) # 输出第二类结果 View(global_hate_ratio)
注:代码中的
fitted_stm_model需替换为你自己拟合完成的STM模型对象名。如果需要按主题概率加权计算占比而非取主导主题统计,只需将每条帖子对应主题的theta值作为权重,乘以对应仇恨标签的0/1值后再分组求和即可。
内容的提问来源于stack exchange,提问作者Reshmi Kumpakha
相关产品推荐
相关产品推荐

