/
slides5.Rpres
246 lines (179 loc) · 4.48 KB
/
slides5.Rpres
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<style>
.reveal .slides > sectionx {
top: -70%;
}
.reveal pre code.r {background-color: #ccF}
.section .reveal li {color:white}
.section .reveal em {font-weight: bold; font-style: "none"}
</style>
Text Analysis in R
========================================================
author: Wouter van Atteveldt
date: Session 5: Sentiment Analysis and Machine Learning
Course Overview
===
type:section
Thursday: Introduction to R
Friday: Corpus Analysis & Topic Modeling
Saturday:
+ *Sentiment Analysis*
+ Machine Learning
Sunday:
+ Basic visualization
+ Semantic Network Analysis
+ Graph Visualization
Sentiment Analysis
====
+ What is the tone of a text?
+ Techniques (e.g. Pang/Lee 2008, Liu 2012)
+ Manual coding
+ Dictionaries
+ Machine Learning
+ Crowdsourcing (Benoit ea 2015)
Sentiment Analysis: problems
====
<em>"The man who leaked cell-phone coverage of Saddam Hussein's execution was arrested"</em>
+ Language is subjective, ambiguous, creative
+ What does positive/negative mean?
+ e.g. Osgood ea 1957: evaluation, potency, activity
+ Who is positive/negative about what?
+ Sentiment Attribution
Sentiment Analysis resources
====
+ Lexicon (dictionary)
+ Annotated texts
+ Tools / models
Interlude: Downloading and Parsing files
===
type: section
Lexical Sentiment Analysis
===
+ Get list of positive / negative terms
+ Count occurrences in text
+ Summarize to sentiment score
+ Possible improvements
+ Word-window approach (tomorrow)
+ Deal with negation, intensification
Lexical Sentiment Analysis in R
===
+ Nothing new here!
+ Directly count words in DTM:
```{r, eval=F}
library(slam)
reviews$npos = row_sums(dtm[, colnames(dtm) %in% pos_words])
```
+ Count words in token list:
```{r, eval=F}
tokens$sent[tokens$lemma %in% pos_words] = 1
```
Interactive Session 5a
====
type: section
Lexical Sentiment Analysis
Course Overview
===
type:section
Thursday: Introduction to R
Friday: Corpus Analysis & Topic Modeling
Saturday:
+ Sentiment Analysis
+ *Machine Learning*
Sunday:
+ Basic visualization
+ Semantic Network Analysis
+ Graph Visualization
Machine Learning
====
+ Statistical Modeling
+ Dependent Variable: sentiment, topic, frame
+ Independent Variables: words
+ Focus: prediction rather than explanation
+ Millions of correlated independent variables
Text Classification
===
+ Each text has a 'class'
+ Training documents to fit model
+ Test documents to gauge accuracy
+ (or use cross-validation)
+ Choices:
+ What features?
+ Which model?
Text Classification: features
===
+ Features: independent variables
+ Basic approach: each word is a feature
+ Other options e.g.
+ Collocations (n-grams)
+ LDA Topics
+ Feature selection
Text Classification: models
===
+ Naive Bayes
+ Maximum Entropy
+ Support Vector Machines
+ Neural Networks
+ (deep learning)
Combining models
===
+ Ensemble Learning
+ Train multiple models
+ Decide by vote
+ Active Learning
+ Code limited amount of material
+ Train+test model
+ Code most difficult cases, repeat
Text Classification in R
===
+ Package RTextTools
+ Jurka et al, 2013
+ Based on DTM plus coded classes
+ Does learning, evaluation, prediction
Text Classification in R
===
(1) Create 'container' from DTM + coded classes
```{r, eval=F}
library(RTextToools)
c = create_container(dtm, classes,
trainSize=train, testSize=test, virgin=F)
```
(2) Train and test model
```{r, eval=F}
SVM <- train_model(c,"SVM")
SVM_CLASSIFY <- classify_model(c, SVM)
```
(3) Evaluate
```{r, eval=F}
analytics <- create_analytics(c, SVM_CLASSIFY)
```
Code new material
===
```{r, eval=F}
is_coded = !is.na(classes)
c = create_container(dtm, classes,
trainSize=is_coded, virgin=T)
SVM <- train_model(c,"SVM")
SVM_CLASSIFY <- classify_model(c, SVM)
analytics <- create_analytics(c, SVM_CLASSIFY)
head(analytics@document_summary)
```
Some links:
===
+ Burscher et al 2014: Framing with SVM's
+ Purpura & Wilkerson 2007: Active Learning for Agenda Coding
+ Some online resources:
+ http://www.r-bloggers.com/sentiment-analysis-on-donald-trump-using-r-and-tableau/
+ https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html#lexicon
+ http://datascienceplus.com/sentiment-analysis-with-machine-learning-in-r/
+ https://sites.google.com/site/miningtwitter/questions/sentiment/sentiment
Interactive Session 5b
====
type: section
Text Classification for Sentiment Analysis
Hands-on session 5
====
type: section
Break
Handouts:
+ Sentiment Analysis Resources
+ Lexcial Sentiment Analysis
+ Machine Learning