-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweets_tutorial.Rmd
499 lines (408 loc) · 12.8 KB
/
tweets_tutorial.Rmd
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
---
title: "patents_tutorial"
author: "Prashant Garg"
date: "2023-04-26"
output: html_document
---
################################################
#
# 2023 ML Workshop
#
# Predicting, evaluating, and interpreting
#
#
################################################
install libraries (only once)
```{r}
# Un-comment and run these once, if you haven't installed them before
# install.packages("tidyverse") # useful everything
install.packages("ggrepel") # nice labels for plots
install.packages("glmnet") # supervised machine learning
install.packages("quanteda") # very useful text stuff
install.packages("textclean") # useful text stuff
install.packages("stm") # topic models
install.packages("textdata") # dictionaries
install.packages("syuzhet") # sentiment
install.packages("doc2concrete") # ngram model
install.packages("politeness") # dialogue acts
install.packages("spacyr") # grammar parsing
```
load libraries
```{r}
# Run these every time
library(tidyverse)
library(ggrepel)
library(glmnet)
library(quanteda)
library(textclean)
library(stm)
library(textdata)
library(syuzhet)
library(doc2concrete)
library(politeness)
library(spacyr)
```
Here I am loading separate R scripts that contain functions we will use.
make sure these are all in your Rstudio project
All in the main folder, along with this script!
```{r}
source("create_dfm.R") # our dfm function as a separate script
source("kendall_acc.R") # our accuracy function
source("vectorFunctions.R") # for word embeddings
```
load and explore data
```{r}
# patents data
tweets_data <- readRDS("tweets_data.RDS")
# First thing - check variables
names(tweets_data)
```
Calculate a 1-gram feature count matrix for the review data, with no dropped words
```{r}
dfm1<-create_dfm(tweets_data$text,
ngrams=1,
min.prop=0,
stop.words = FALSE)
dim(dfm1)
```
More than 42k ngrams! Too many
most common words - obvious
```{r}
sort(colMeans(dfm1),decreasing=TRUE)[1:20]
```
least common words
```{r}
sort(colMeans(dfm1))[1:20]
```
Let's put this on a plot
```{r}
ngram_counts<-data.frame(word=colnames(dfm1),
frequency=colMeans(dfm1),
f_rank=rank(-colMeans(dfm1),
ties.method="first"))
ngram_counts %>%
ggplot(aes(x=f_rank,y=frequency)) +
geom_point()
```
Let's write the words on the plot
```{r}
ngram_counts %>%
ggplot(aes(x=f_rank,y=frequency,label=word)) +
geom_text()
```
That's a mess... let's create a new column and pick only a few words to plot
```{r}
ngram_counts <- ngram_counts %>%
mutate(word_label=ifelse(f_rank%in%c(1:10, # top 10 words
sample(11:200,10), # ten random words from 11-100
sample(201:1000,20), # twenty random words from 101-1000
sample(1000:n(),20)), # twenty random words above 1000
word,""))
```
better, but still messy.
```{r}
ngram_counts %>%
ggplot(aes(x=f_rank,y=frequency,label=word_label)) +
geom_text()
```
Let's get rid of the rarest words and plot again
```{r}
ngram_counts %>%
filter(f_rank<1000) %>%
ggplot(aes(x=f_rank,y=frequency,label=word_label)) +
geom_text()
```
still some overlap.. we'll use the ggrepel package to space out the words
We'll also add the points for all the words, just to see
```{r}
ngram_counts %>%
filter(f_rank<1000) %>%
ggplot(aes(x=f_rank,y=frequency,label=word_label)) +
geom_point(color="red") +
geom_label_repel(max.overlaps=100,size=7,
nudge_x = 1,nudge_y = 1,
force=3) +
labs(x="Frequency Rank",y="Frequency per Tweet") +
theme_bw() +
theme(axis.title = element_text(size=24),
axis.text = element_text(size=24))
```
(optional) save the plot
```{r}
# ggsave("word_freq.png")
```
# prediction
Ok, let's build a model to predict whether tweet is clean or dirty!
First, let's look at our classification data
```{r}
table(tweets_data$label)
```
Let's only use 1-grams for now
```{r}
dfm3<-create_dfm(tweets_data$text,ngrams=1) %>%
convert(to="data.frame") %>%
select(-doc_id)
# Lots of words
dim(dfm3)
```
Most common words in clean and dirty tweets...
```{r}
sort(colMeans(dfm3[tweets_data$label==1,]),decreasing=T)[1:20]
```
lots of the same words!
```{r}
sort(colMeans(dfm3[tweets_data$label==0,]),decreasing=T)[1:20]
```
What we really care about is - does the presence of a word predict label?
```{r}
# A simple start - correlate each word with label
correlations<-dfm3 %>%
summarise_all(~round(cor(.,tweets_data$label),3)) %>%
unlist()
```
Ten lowest associations (dirty tweets)
```{r}
sort(correlations)[1:10]
```
Ten highest associations (clean tweets)
```{r}
rev(sort(correlations))[1:10]
```
note - same as:
```{r}
sort(correlations,decreasing=TRUE)[1:10]
```
Let's put this on a plot
```{r}
cor_set<-data.frame(correlation=correlations,
frequency=colMeans(dfm3),
word=colnames(dfm3)) %>%
# # let's group the points so we can add color
# mutate(colour=case_when(
# correlation>.02 ~ 1, # positive correlations (> +.02)
# correlation<(-.02) ~ 0, # negative correlations (< -.02)
# T ~ "neutral")) # black otherwise (i.e. if it is close to zero)
mutate(colour=case_when(
correlation>.02 ~ "high", # positive correlations (> +.02)
correlation<(-.02) ~ "low", # negative correlations (< -.02)
T ~ "none"))
head(cor_set)
```
Let's make a quick plot
```{r}
cor_set %>%
ggplot(aes(x=correlation,y=frequency,label=word,color=colour)) +
geom_point() +
geom_label_repel()+
theme_bw()
```
A few problems with this plot:
- The repel function is removing too many words
- We probably don't want to label the neutral words
- The points are all compressed at the bottom
- The colours are chosen automatically
- Axis labels are too small and don't make much sense
- We don't really need a legend here
- It would be nice to have a dark line indicating zero
- The other grid lines are not that useful
Here's a fixed version
```{r}
cor_set %>%
mutate(word=ifelse(abs(correlation)>.02,word,NA)) %>% # get rid of labels for all neutral words
ggplot(aes(x=correlation,y=frequency,label=word,color=colour)) +
scale_color_manual(breaks=c("low","none","high"),
values=c("brown4","gray","green3"))+ # manually assign colors to each category
geom_vline(xintercept=0)+ # Add dark line at zero
geom_point() +
geom_label_repel(max.overlaps=16,force=3)+ # tolerate more overlapping words and force them apart
scale_y_continuous(trans="log2", # log-transform the Y axis so it's not compressed at the bottom
breaks=c(.01,.05,.1,.2,.5,1,2,5))+ # manually set axis ticks
theme_bw() +
labs(x="Correlation with label",y="Uses per tweets")+ # Write in proper axis titles
theme(legend.position = "none", # get rid of legend
panel.grid = element_blank(), # get rid of grid lines
axis.title=element_text(size=20), # increase size of axis titles
axis.text=element_text(size=16)) # increase size of axis text
```
##################################################
As we will discuss, we are not interested in effects of individual words
Instead, we care more about how all the words perform as a class
To do this, we will use the cv.glmnet() function to build a model
First, we need to split the data into training and testing samples
```{r}
train_split=sample(1:nrow(tweets_data),round(nrow(tweets_data)/2))
length(train_split)
```
create our prediction variables
```{r}
dfm3<-create_dfm(tweets_data$text,ngrams=1) %>%
convert(to="data.frame") %>%
select(-doc_id)
trainX<-dfm3 %>%
slice(train_split) %>%
as.matrix() # we convert to matrix format for glmnet
trainY<-tweets_data %>%
slice(train_split) %>%
pull(label)
testX<-dfm3 %>%
slice(-train_split) %>%
as.matrix() # we convert to matrix format for glmnet
testY<-tweets_data %>%
slice(-train_split) %>%
pull(label)
```
Put training data into LASSO model (note - glmnet requires a matrix)
```{r}
lasso_mod<-cv.glmnet(x=trainX,y=trainY)
```
generate predictions for test data
```{r}
test_predict<-predict(lasso_mod,newx = testX)[,1]
```
Note that while the true answers are binary, the predictions are continuous
Always check these distributions!!
```{r}
# true values
hist(testY)
```
```{r}
# predicted values
hist(test_predict)
```
For now, let's just split the predictions in two, using the median
```{r}
test_predict_binary=ifelse(test_predict>median(test_predict),
1,0)
```
This should have the same values as testY
```{r}
hist(test_predict_binary)
```
and we can calculate accuracy from that
```{r}
round(100*mean(test_predict_binary==testY),3)
```
#######################################################
Let's get a little better at interpreting the model
instead of splitting the dfm we can split the data itself
```{r}
tweets_data_train<-tweets_data[train_split,]
tweets_data_test<-tweets_data[-train_split,]
tweets_data_dfm_train<-create_dfm(tweets_data_train$text,ngrams=1)
tweets_data_dfm_test<-create_dfm(tweets_data_test$text,ngrams=1) %>%
dfm_match(colnames(tweets_data_dfm_train))
```
```{r}
tweet_model <-glmnet::cv.glmnet(x=tweets_data_dfm_train %>%
as.matrix(),
y=tweets_data_train$label)
plot(tweet_model)
```
Interpret with a coefficient plot
This plots coefficients from the model, rather than raw correlations
any fewer features, and more focused on what the model is actually doing
```{r}
plotDat<-tweet_model %>%
coef() %>%
drop() %>%
as.data.frame() %>%
rownames_to_column(var = "ngram") %>%
rename(score=".") %>%
filter(score!=0 & ngram!="(Intercept)" & !is.na(score)) %>%
# add ngram frequencies for plotting
left_join(data.frame(ngram=colnames(tweets_data_dfm_train),
freq=colMeans(tweets_data_dfm_train)))
plotDat %>%
mutate_at(vars(score,freq),~round(.,3))
plotDat %>%
ggplot(aes(x=score,y=freq,label=ngram,color=score)) +
scale_color_gradient(low="blue",high="red")+
geom_vline(xintercept=0)+
geom_point() +
geom_label_repel(max.overlaps = 30,force = 6)+
scale_y_continuous(trans="log2",
breaks=c(.01,.05,.1,.2,.5,1,2,5))+
theme_bw() +
labs(x="Coefficient in Model",y="Uses per Tweet")+
theme(legend.position = "none",
axis.title=element_text(size=20),
axis.text=element_text(size=16))
```
# Evaluate Accuracy
we are going to use a function that calculates non-parametric correlations
this function reports percentage accuracy, where 50% is random guessing
it is calculated across all possible pairs of observations
this is the same function that is in your "kendall_acc.R" script
```{r}
kendall_acc<-function(x,y,percentage=TRUE){
kt=cor(x,y,method="kendall")
kt.acc=.5+kt/2
kt.se=sqrt((kt.acc*(1-kt.acc))/length(x))
report=data.frame(acc=kt.acc,
lower=kt.acc-1.96*kt.se,
upper=kt.acc+1.96*kt.se)
report = round(report,4)
if(percentage) report = report*100
return(report)
}
```
```{r}
test_ngram_predict<-predict(tweet_model,
newx = tweets_data_dfm_test %>%
as.matrix())[,1]
acc_ngram<-kendall_acc(tweets_data_test$label,test_ngram_predict)
acc_ngram
```
Find examples
```{r}
# store predictions in data, calculate accuracy
tweets_data_test<-tweets_data_test %>%
mutate(prediction=test_ngram_predict,
error=abs(label-prediction),
bias=label-prediction)
close_green<-tweets_data_test %>%
filter(label==1 & error<.5) %>%
select(text,label,prediction)
close_dirty<-tweets_data_test %>%
filter(label==0 & error<.5) %>%
select(text,label,prediction)
```
```{r}
# close_green
close_green %>%
slice(1:2) %>%
pull(text) # optionally, replace abstract with patent_title
```
```{r}
# close_dirty
close_dirty %>%
slice(1:2) %>%
pull(text) # optionally, replace abstract with patent_title
```
Error analysis - find biggest misses
```{r}
hist(tweets_data_test$prediction)
```
```{r}
hist(tweets_data_test$label)
```
```{r}
miss_green<-tweets_data_test %>%
arrange(bias) %>%
slice(1:10) %>%
select(text,label,prediction)
miss_dirty<-tweets_data_test %>%
arrange(-bias) %>%
slice(1:10) %>%
select(text,label,prediction)
```
```{r}
miss_dirty%>%
slice(1:2) %>%
pull(text)
```
```{r}
miss_green%>%
slice(1:2) %>%
pull(text)
```