使用glm进行情感预测

时间:2018-01-17 07:05:46

标签: r glm sentiment-analysis

我正在尝试使用glm来预测情绪并遇到以下问题

  train_data_df <- as.data.frame(as.matrix(train_data))
  log_model <- glm(sentiment ~ word_count, data = train_data_df,   family = binomial)
     > Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

输入数据结构&#34;情绪&#34;和&#34; word_count&#34;如下

> str(train_data$sentiment[1:2])
List of 2
 $ : num 1
 $ : num 1
> str(train_data$word_count[1:2])
List of 2
 $ :List of 1
  ..    $ :Classes 'term_frequency', 'integer'  Named int [1:24] 3 1 1 1 1 1  1 1 1 3 ...
      .. .. ..- attr(*, "names")= chr [1:24] "and" "bags" "came" "disappointed" ...
 $ :List of 1
  ..    $ :Classes 'term_frequency', 'integer'  Named int [1:22] 2 1 1 1 1 1 1 1 1 1 ...
     .. .. ..- attr(*, "names")= chr [1:22] "and" "anyone" "bed" "comfortable" ...



head(train_data_df[1,])
                   name
2 Planetwise Wipe Pouch
                                                                                                                                                          review
2 it came early and was not disappointed. i love planet wise bags and now my wipe holder. it keps my osocozy wipes moist and does not leak. highly recommend it.
  rating
2      5
                                                                                                                                                review_clean
2 it came early and was not disappointed i love planet wise bags and now my wipe holder it keps my osocozy wipes moist and does not leak highly recommend it
                                                              word_count sentiment
2 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1         1

提前感谢您帮助我

1 个答案:

答案 0 :(得分:0)

在您使用的R公式sentiment ~ word_count中,每一边应该是每行一个数字或因子(这是'x' must be atomic的含义)。对于您的word_count列,情况显然不是这样 - 看来,对于每一行,word_count是一个由几个整数值组成的列表(Have you called 'sort' on a list? - 嗯,确实你有)。

要确认这是您的问题的根源,您可以将word_count替换为其元素的总和;这应该使代码工作(当然,如果结果对于情绪预测具有任何实际价值,那么这是另一个故事,但这不是你在这里的实际问题......)