forked from iamdavecampbell/tibble_house_on_the_prairie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistical Language Models Workshop.Rmd
1042 lines (563 loc) · 31.9 KB
/
Statistical Language Models Workshop.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Statistical Language Modelling with R"
author:
name: "Dave Campbell, <br> Bank of Canada and Carleton University, <br> [linkedin.com/in/drdavecampbell/](https://www.linkedin.com/in/drdavecampbell/) <br> Twitter: @iamdavecampbell <br> GitHub: @iamdavecampbell"
date: "23/06/2022"
output:
ioslides_presentation:
widescreen: true
---
## Statistical Language Processing
Dave Campbell
#### All materials are on github:
[github.com/iamdavecampbell/tibble_house_on_the_prairie](https:github.com/iamdavecampbell/tibble_house_on_the_prairie)
Feel free to fork, star, download, correct typos,...
**We will run code live**
### Libraries:
```{r, eval = FALSE, message = FALSE}
# packages used
install.packages(c("tidyverse","tidytext",
"dplyr", "stringr",
"janitor","lubridate",
"qdap","tools"))
# packages mentioned:
install.packages(c("text2vec","textdata", "word2vec", "sentiment.ai"))
```
## Today
### Hands On, let's run together
- Sentiment Analysis from a lexicon
- Sentiment Analysis considering nearby words for negation, amplification, deamplification,...
### Hands-Waving
- Using Word Embeddings: basic concepts, plenty of potential, plenty of danger
### Hands-Off Research in Statistical Language Processing
- Is there a region effect for the language used to describe Canadian beer flavours?
## Load libraries:
```{r, eval = FALSE, message = FALSE}
library(tidyverse) ## of course we're using tidyverse
library(tidytext) ## some text tools, basic sentiment analysis
library(dplyr)
library(stringr) #handling strings
library(janitor) ## make tables look nice
library(lubridate) ## deal with dates
library(qdap) ## handle polarization for sentiment analysis
library(tools) ## to make one plot title look nice
library(text2vec)## will use if we get to word embeddings
library(textdata)## will use if we get to word embeddings
library(word2vec)## will use if we get to word embeddings
library(sentiment.ai) # for sentiment word embedding models
```
## R Version
Note that I am using R>4.1 which gives me access to the **|>** operator. For reproducibility this is the R version that I am using.
```{r, eval = TRUE}
version
```
## Basic workflow
- Decide what you want to do.
- Come up with a plan.
- Gather appropriate data
- Split into the units of observation. Sometimes these are documents, Tweets, speeches, or sentences.
- Tokenize the observations into subunits of interest, typically these are words.
- Analyze!
### Problem:
Is there a difference in the sentiment in Bank of Canada speeches given to the House of Commons Standing Committee on Finance? Let's compare 2018 vs 2022.
## Sentiment Analysis
### The goal is to infer the emotional meaning behind the authors words.
- In the 1980s Robert Plutchik created a classification framework for emotions based on 8 evolutionarily created emotions **{anger, disgust, sadness, surprise, fear, trust, joy, anticipation}**.
- Other emotions are combinations of these.
- Classifying text according to these emotions is challenging and often subjective.
- Instead people often use polarity **(positive, neutral, negative)**.
- Overall these tend to be easier and have higher annotator agreement.
## Sentiment Analysis Typical Strategies
### **Easy**
Use a lexicon of positive / negative words and count occurence within each article.
- This is quick and easy, but it is hard to get a lexicon that is really tuned to your goals and era.
- By default this ignores context
( _costs *fell*_ "+", _profits *fell*_ "-")
- And modifiers ( _my French comprehension is *not bad*_ "+", _my French spelling is *not good*_ "-").
## Sentiment Analysis Typical Strategies
### **Medium**
Modify a lexicon by editing some terms, looking for modifiers and/or merging terms that could be considered a single word "Data_Science".
- This requires some hands on effort and domain expertise. You'll need to read some of the text.
- This might involve searching for modifiers, such as *not* within a few words of *bad*.
- This is equivalent to manually adding some structure to the model.
- Note that pre-processing typically makes a lot of improvements but almost always introduces some undesirable effects.
## Sentiment Analysis Typical Strategies
### **Harder**
- Label some sentences as positive / negative and build a model to figure out what it is about the sentences that define their sentiment.
- Often start from something like a pre-trained *Word Embedding* model to convert the text to numbers then use a regression-type model (neural net or boosting?) to predict sentiment.
- Typically you need a lot of data.
- This is much better for paying attention to context, but costs more effort (labelling data and training the model).
- Relies on having a well-trained (possibly domain specific) *Word Embedding* model and making sure that the model isn't amplifying biases.
## Some Data
### Can we scrape it?
Take a look at using Bank of Canada speeches.
- The [https://www.bankofcanada.ca/robots.txt](https://www.bankofcanada.ca/robots.txt) file looks like we aren't disallowed.
- The other place to look for permission is in the webpage [terms and conditions](https://www.bankofcanada.ca/terms/).
We can use Bank of Canada speeches as long as we provide them free of charge (or otherwise obtain the Bank's permission) and attribute the Bank of Canada as the source. We must also **exercise due diligence in ensuring the accuracy of any content**.
## Subset to consider
- Speeches for the House of Commons Standing Committee on Finance in 2018 vs 2022 so far. Note that this is only 2 speeches per year.
- We could compare one speech from each, but in what follows we end up with low counts in some categories. We could get around this by combining sentiment categories, but taking more speeches also provides a snapshot of the economy.
---
### New Speeches
- \small [https://www.bankofcanada.ca/2022/04/opening-statement-250422/](https://www.bankofcanada.ca/2022/04/opening-statement-250422/)
- \small [https://www.bankofcanada.ca/2022/03/opening-statement-030322/](https://www.bankofcanada.ca/2022/03/opening-statement-030322/)
### Old Speeches
- \small [https://www.bankofcanada.ca/2018/10/opening-statement-october-30-2018/](https://www.bankofcanada.ca/2018/10/opening-statement-october-30-2018/)
- \small [https://www.bankofcanada.ca/2018/04/opening-statement-april-23-2018/](https://www.bankofcanada.ca/2018/04/opening-statement-april-23-2018/)
#### Want More?
Ask Me later about webscraping details if you like.
### Load the data
```{r,eval = FALSE, message = FALSE, warning = FALSE, results='hide'}
new_speeches = read_csv(file = "new_speeches.csv")
old_speeches = read_csv(file = "old_speeches.csv")
```
```{r,echo = FALSE, eval = FALSE, message = FALSE, warning = FALSE, results='hide'}
# alternative:
new_speeches = read_csv(file = "https://people.math.carleton.ca/~davecampbell/workshop/new_speeches.csv")
old_speeches = read_csv(file = "https://people.math.carleton.ca/~davecampbell/workshop/old_speeches.csv")
```
## Lexicons for Sentiment Analysis
- Lexicons are a list of words with an associated sentiment.
- Although the English language has a few hundred thousand words, typically sentiment lexicons have only a few thousand words.
- In most cases people convey information using a fairly small subset of language, so lexicons tend to do well most of the time.
- The idea is to match terms to a sentiment lexicon. These have been built for a specific purpose, but hopefully such a sentiment will be useful for us.
- The best is to build your own lexicon that suits your needs, but that's expensive and slow.
**There is no way of separating out data cleaning from data analysis.**
## Popular Lexicons
- AFINN from Finn Årup Nielsen,
- bing from Bing Liu and collaborators,
- Loughran-McDonald Financial dictionary,
- nrc from Saif Mohammad and Peter Turney at the National Research Council of Canada.
## AFINN
### Gives words a score between -5 and +5 rating its severity of positive or negative sentiment.
- Constructed by hand for 'microposts' and 'microblogs' such as Twitter.
```{r, eval = FALSE}
tidytext::get_sentiments("afinn")
get_sentiments("afinn") |>
ggplot(aes(x = value))+
geom_histogram()
```
## Bing
### Gives a binary value of positive or negative. Neutral words are not in the list.
- Constructed for feature based opinion mining.
- Bing et al, also have many related data products and lexicons.
```{r, eval = FALSE}
tidytext::get_sentiments("bing")
get_sentiments("bing") |>
ggplot(aes(x = sentiment, fill = sentiment))+
geom_bar()+
theme(axis.text.x = element_text(angle = 90,
vjust = 0.5, hjust=1))+
theme(legend.position = "none")
```
## Loughran-McDonald
Lexicon of negative, positive, uncertainty, litigious, strong modal, weak modal, and constraining terms from the Loughran-McDonald financial dictionary see [here](https://sraf.nd.edu/loughranmcdonald-master-dictionary/)
```{r, eval = FALSE}
## The format is a list:
## Convert it as a data.frame / tibble
LMlex = rbind(
cbind(SentimentAnalysis::DictionaryLM$positive, "positive"),
cbind(SentimentAnalysis::DictionaryLM$negative, "negative"),
cbind(SentimentAnalysis::DictionaryLM$uncertainty, "uncertainty"))
colnames(LMlex) = c("term", "polarity")
LMlex = LMlex |>as_tibble()
```
```{r, eval = FALSE}
LMlex |>
ggplot(aes(x = polarity, fill = polarity))+
geom_bar()+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
theme(legend.position = "none")
```
## NRC
### Puts each word into categories based on the evolutionarily created emotions.
- aka **EmoLex**
- From the documentation ""It was the first word-emotion association lexicon, with entries for eight basic emotions as well as positive and negative sentiment. It still remains the largest such lexicon. Prior work largely focused on positive and negative sentiment."
```{r, eval = FALSE}
tidytext::get_sentiments("nrc")
get_sentiments("nrc") |>
ggplot(aes(x = sentiment, fill = sentiment))+
geom_bar()+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
theme(legend.position = "none")
```
## Lexicons
Note that each lexicon has a different length and words might evolve in meaning over time.
- **Sick** used to be bad, then it was good, now it's so bad that we spent two years _staying the blazes home_.
## Sentiment
Back to sentiments, let's count the _nrc_ category occurences:
```{r, eval = FALSE}
Speeches = rbind(new_speeches,old_speeches) |>
mutate(date = year(date) )|> ## simplify date to just the year
unnest_tokens(output = word,input = speech_text,
token = "words") |>
inner_join(get_sentiments("nrc") )
#Count the occurrence with each speech
Sents = Speeches |> group_by(date) |> count(sentiment)
Sents |> summarise(sum(n))
Sents |> filter(!(sentiment =="positive"|sentiment=="negative")) |>
ggplot(aes(fill=sentiment, y=n, x=as.factor(date))) +
geom_bar(position="fill", stat="identity")+
xlab("date")
```
## Statistical Test
Consider the Null Hypothesis that economic speeches have same sentiment distribution between 2018 and 2022.
- The alternative is that there is an unspecified difference in distribution. This can be tested through a Chi-Square test for categorical distributions with $N_r$ rows and $N_c$ columns.
- With observed counts $O_{ij}$ in row $i$ and column $j$ of the table, the Expected counts are the data assuming the only difference is the total count;
\[E_{ij}= (\mbox{row i total*column j total}) / N_{total}\]
- This gives the test statistic:
\[X = \sum_{i=1}^{N_r}\sum_{j=1}^{N_c}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\sim \chi^2_{(N_r-1)*(N_c-1)}\]
## In Practice
- Remove the categories "positive" and "negative" since these are supersets of the other _nrc_ categories.
- The basic assumption is that these speeches are 'bags of words' that speeches are literally random samples of words rather than prose. Essentially the Bank of Canada has some sentiment distribution at a given time and the words used in speeches are a random sample thereof.
- This is really just illustrative of how to analyze two time points, but related tools could track extend to regression or time series tools and consider all speeches.
## In R:
Cross-tab table
T his counts the occurences, so we don't use the 'summed' table but instead use the tibble that we joined on the sentiments.
```{r, eval = FALSE}
MyTable = janitor::tabyl(
Speeches|>
filter(!(sentiment =="positive"|sentiment=="negative")) |>
group_by(date),
sentiment,date)
```
## Janitor Detour
The janitor library makes it easy to build tables that look nice and can format data with row or column percents and counts. As a side note it also can put in row / column / total percentages,
```{r, eval = FALSE}
MyTable |> adorn_totals("row") ## add a row for totals
## report column percentages rather than counts
MyTable |> adorn_percentages("col")
## report percentage of total and also counts
MyTable |> adorn_percentages("all")|> adorn_ns()
```
## The Statistical Test:
```{r, eval = FALSE}
#Chi-Square test:
chisq.test(MyTable)
```
### Note
when the counts within a table category are small, generally <5, the results may not be accurate.
- It might make more sense to combine a few sentiments for the purposes of the test.
- We got around this by using more speeches.
## Going further
Improving the lexicon by considering negation
Using a lexicon, consider the phrase:
- "This workshop is not too bad."
A lexicon based approach would see the word **bad** and consider the sentence to be negative, whereas **not bad** is actually pretty good all things considered.
## qdap
- Library **qdap** searches for the positive or negative **word** and then builds a window around it: **[word-4,word+2]**.
- Within that window it labels words as **neutral words**, **negators**, **amplifiers**, or **de-amplifiers**
### Word lists:
It's worthwhile to check out the word lists and make sure that they are suitable. Pre-processing choices impact analysis.
```{r, eval = FALSE, message = FALSE }
qdapDictionaries::amplification.words
qdapDictionaries::deamplification.words
qdapDictionaries::negation.words
```
## Let's try it out
Use a binary lexicon such as LMlex or bing
- Start by defining the basic polarity lexicon:
```{r, eval = FALSE}
## inputs are a vector of positives, vector of negatives,...
positives = LMlex |> filter(polarity =="positive") |> pull(term)
negatives = LMlex |> filter(polarity =="negative") |> pull(term)
## this is a great place to add in more terms...
negatives = c(negatives, "invasion", "war", "uncertainty",
"upheaval","anxious", "risk", "tensions","tension")
positives = c(positives, "solid", "reassuring")
## put them into the sentiment lookup hash table
## note that we could set the positive and negative weights:
polarity_frame = sentiment_frame(positives, negatives)
```
## qdap
- A polarized word starts with a score of $X_i=\pm 1$.
- The amplifier / deamplifier weight is by default $.8$.
- The number of negator words, $N_{Negator}$ flip the sign.
- The polarity score for a word in the positive / negative list depends on the nearby words:
\[P_{olarity}=\frac{(-1)^{N_{Negator}}(X_i + .8 * (A_{mplifiers} - D_{eamplifiers})) }{\sqrt{N_{WordsInSentence}}}\]
- Amplifier words are counted but could be weighted:
\[A_{mplifiers} = N_{Amplifiers}I\!\!I(N_{deamplifiers}=0)\]
\[D_{eamplifiers} = max(-1,(-1^{N_{Negators}}N_{Amplifiers} +N_{Deamplifiers}))\]
## Let's try it out
```{r, eval = FALSE}
## try a few sentences and extract the polarity:
polarity("This workshop is bad." ,
polarity.frame = polarity_frame)$all
polarity("This workshop is not great." ,
polarity.frame = polarity_frame)$all #negator
polarity("This workshop is seldom bad." ,
polarity.frame = polarity_frame)$all #seldom
polarity("This workshop is ok" ,
polarity.frame = polarity_frame)$all ## neutral
polarity("This workshop is hardly very good" ,
polarity.frame = polarity_frame)$all #deamplifier 'hardly', amplifier 'very'; (1+.8*(-(-1+1)))/sqrt(6)
polarity("This workshop is very seldom very good",
polarity.frame = polarity_frame)$all; #deamplifier 'seldom', amplifier 'very'x2; (1+.8*(0-(-2+1)))/sqrt(7)
polarity("This workshop is great" ,
polarity.frame = polarity_frame)$all
polarity("This workshop is really good" ,
polarity.frame = polarity_frame)$all # amplifier
polarity("This workshop is the absolute best" ,
polarity.frame = polarity_frame)$all
```
## Will this work on real world economic data?
### Looking at sentiment relative to certain topics
- split the speeches into sentences
- subset into sentences mentioning certain keywords here consider **inflation**
- obtain polarity scores around each topic area.
- t-test for differences between average polarity scores
## First let's do some cleaning.
```{r, eval = FALSE, message = FALSE, warning = FALSE, results='hide' }
#The data:
speeches_modified = rbind(new_speeches,old_speeches) |>
mutate(date = year(date) )|> ## simplify date to just the year
group_by(date)|> ## do these actions within the unique date values
unnest_tokens(output = paragraph,
input = speech_text,
token = "regex", pattern = "\n") |>## split into paragraphs
mutate(paragraph_count = row_number() )|> ## add a paragraph number
ungroup()
```
## We can run an automatic check for strange things,
- This gives suggestions about potential problems
```{r}
## check_text(speeches_modified[,"speech_text"])
```
### Here it suggests a few functions that could help.
- Replacing numbers with words, handling some symbols like '%' and "½", and dealing with unexpected ending to sentences.
- Alternatively check the spelling interactively as it skims through the text, but this is often too sensitive and flags jargon as typos:
```{r}
## check_spelling_interactive(speeches_modified$speech_text)
```
## Perform some basic cleaning steps as suggested from check_text.
```{r, eval = FALSE}
speeches_modified = speeches_modified |>
mutate(paragraph = replace_number(paragraph))|> ## replace numbers
mutate(paragraph = str_replace_all(paragraph,
pattern = "(\t)|(\n)|(\r)",
replacement = ""))|> ##spaces, breaks
mutate(paragraph = str_replace_all(paragraph,
pattern = "\\%",
replacement = " percent"))|> ## symbols
mutate(paragraph = str_replace_all(paragraph,
pattern = "½",
replacement = " a half")) |>
mutate(paragraph = str_replace_all(paragraph,
pattern = "¼",
replacement = " a quarter"))|>
mutate(paragraph = add_incomplete(paragraph))
## handing potentially incomplete sentences
```
## Sentiment around topics of interest.
- Consider the paragraphs mentioning inflation.
- What is the sentiment of those paragraphs?
- Is there a difference between 2018 and 2022 in sentiment when discussing inflation?
```{r, eval = FALSE}
speeches_economy = speeches_modified |>
filter(str_detect(paragraph, "inflation"))
## split paragraphs into sentences
speeches_economy_sentence = speeches_economy |>
unnest_tokens(input = paragraph, output = sentences,
token = stringr::str_split,
pattern = "\\.|\\?|\\!") |> ## split at punctuation.
filter(sentences != "") ## remove empty sentences
```
## Recycle the positive/negative sentiment frame from above
```{r, eval = FALSE}
## polarity_frame = sentiment_frame(positives, negatives)
speeches_economy_sentence = speeches_economy_sentence |>
mutate(polarity =
polarity(sentences, polarity.frame = polarity_frame)$all$polarity)
speeches_economy_sentence |>
filter(polarity!=0)|> ## remove the neutral filler stuff
ggplot(aes(x = polarity, colour = as.factor(date)))+
geom_density()+
ggtitle(tools::toTitleCase(
"Polarity of non-neutral sentences in paragraphs mentioning inflation"))
```
## T-test
We could perform a statistical test for differences in the average polarity of polarized sentences relating to inflation.
\[H_o:\mu_{2018}\leq\mu_{2022}\]
\[H_a:\mu_{2018}>\mu_{2022}\]
We should consider whether or not to include the neutral sentences. It's best to look at some of the neutral sentences.
```{r, eval = FALSE}
speeches_economy_sentence |>
filter(polarity==0) |>
select(sentences) |>
sample_n(10)## sample 10 random lines...
```
## T-test
Given whatever we decide, we can run the t-test to check if the mean polarity in sentences from paragraphs discussing inflation in 2018 is greater than it is now in 2022.
```{r, eval = FALSE}
t.test(polarity ~ date,
alternative = "greater",
var.equal = FALSE,
data = speeches_economy_sentence |>
mutate(date = as.factor(date))|>
filter(polarity!=0)#<-- consider this line carefully
)
```
## With More Effort We Can Do Even Better, But...
### ...we need more data.
- There are much better / more accurate tools for sentiment analysis. But language has a lot of specific nuances that require domain specific input.
### The meaning and sentiment behind words varies with context.
- Python might literally kill you because its a large hungry snake or it might figuratively kill you because you forget to start with index 0.
- Apples might be cheap or expensive depending on whether you buy one at a produce store or computer store.
## With More Effort We Can Do Even Better, But...
### You only have what you get, and you might not like it
With text it is often easy to get a whole lot of data.
- **good data >> big data**.
- With text data it's hard to know what you have, but its definitely noisy, and has a lot of caveats.
## Going Further
In text analysis = convert words into numbers, then we use standard tools.
- We could then get annotators to craft sentiment scores, then build a matrix, $\mathbf{X}$ with one row per sentence, and columns as presence / absence of individual words.
- This implies a __very very__ wide $\mathbf{X}$ matrix for fitting our sentiment scalar $\hat{\mathbf{Y}} = f(\mathbf{X}\beta)$, so we need a lot of data.
- In many cases we need to reduce the dimension of $\mathbf{X}$ to somehow handle equivalent words.
## Danger Zone: Word embeddings
### Fantastic Results, extreme potential for harm
Models like **Word2Vec, GloVE**, and **BERT** reduce the dimension of $\mathbf{X}$ from 300,000 ish unique English words, to 300ish numeric dimensions. Often we use a pre-trained word embedding model made for another purpose and hope it's good enough for us. Then we run our raw text through the word embedding as a dimension reduction tool and plug it into regression.
As statisticians know, it is usually best to reduce the dimension of $\mathbf{X}$ while modelling $\mathbf{Y}$ so that we end up with something optimal, but for computational reasons this is rarely done.
Reduced dimension models like BERT, GloVE, and Word2Vec are often called **word embeddings**. There is well known danger in blindly using them that has led to amplification of discrimination and human bias.
## Word Embedding Models
### Word2Vec
**Continuous Bag of Words**: Neural Net with one hidden layer predict a missing middle word from the context words.
- Input: 2xWindow size X 300,000 ish words
- Hidden (embedding) dimension: 300ish
- Output: 1 word from 300,000 words.
**Skip-Gram**: Neural Net with one hidden layer predicts the context words from the middle word.
- Input: 1 word from 300,000 ish words
- Hidden (embedding) dimension: 300ish,
- Output: 2xWindow size X 300,000ish words.
## Word2Vec
```{r, eval = FALSE}
library(word2vec)
model <- word2vec(x = Text_Goes_Here, type = "cbow", dim = 300, iter = 20)
# iter = passes through full corpus
```
### Or download a pre-trained model from [https://code.google.com/archive/p/word2vec/](https://code.google.com/archive/p/word2vec/)
Download (1.5GB) pre-trained vectors trained on part of Google News dataset (about 100 billion words) from the original paper: [GoogleNews-vectors-negative300.bin.gz](https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?usp=sharing)"
## GloVe
- Create a matrix with word of interest X context words.
- Entries are tallied co-occurences, weighted by distance of their occurence from word of interest.
- Define low dimensional word embeddings optimizing a weighted least squares reconstruction of the log co-occurence counts.
```{r, eval = FALSE}
# download via the library:
# textdata::embedding_glove6b(manual_download = FALSE)
# or
# download directly from "http://nlp.stanford.edu/data/glove.6B.zip"
# 822.2 MB download: 2010 Wikipedia dump with 1 billion tokens
# I have it downloaded in my current working directory, so it can be run like this
glove6b = textdata::embedding_glove6b(dimensions = 300, dir = getwd(),
manual_download=TRUE, return_path=TRUE)
glove6bMat = as.matrix(glove6b[,-1])
rownames(glove6bMat) = glove6b$token
colnames(glove6bMat) = colnames(glove6b[,-1])
# find nearby words in embedding space
Word_of_interest = glove6bMat["payment",]
word2vec::word2vec_similarity (x = Word_of_interest,
y = glove6bMat,
type = "cosine",
top_n = 20)
```
## Embeddings
<img src="images.001.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.002.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.003.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.005.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.006.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.007.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.008.jpeg" style="height: 500px"/>
## Embeddings
<img src="images.009.jpeg" style="height: 500px"/>
## Dangers
Biases we see in text are amplified in the embedding dimensions
- We can find words close to each other and do vector addition and subtraction!
- We might not like what we find.
### Prime Minister of Canada
- Kim Campbell was Prime Minister for 132 days in 1993, as a result "Prime Minister of Canada" is much closer to male pronouns than female ones.
### Racial and gender bias examples from general text are a lot worse.
## Going further
Sentiments
- Label the sentiment in a set of sentences.
- Fit a model to predict sentiment based on word embeddings.
Given an imprecise Bank speech quote, which year is it from?
- Split the speech sentences into test-train, use Year as $\mathbf{Y}$.
- Push sentences through embedding model and average over sentences, these become $\mathbf{X}$.
- Fit a (fancy?) regression model predicting $\hat{\mathbf{Y}}$ from $\mathbf{X}$.
- Run the imprecise quote through the model to estimate the year it was spoken.
## Installing a Pre-Trained Word Embedding model
Using a pre-trained word embedding language model...
It can be tricky to install. R needs to install and use models built in python and tensorflow
```{r, eval = FALSE}
library(sentiment.ai)
```
- I tried on Windows, Mac, and Linux and only found success on Linux if I first installed tensorflow and keras libraries and software:
```{r, eval = FALSE}
tensorflow::install_tensorflow()
keras::install_keras()
```
## Installing a Pre-Trained Word Embedding model Part 2
```{r, eval = FALSE}
# the first time you run it you need to do this:
# Load the package
library(sentiment.ai)
library(SentimentAnalysis)
library(sentimentr)
# the first time you set up the library you also need to do this:
# install_sentiment.ai()
#It installs python with numpy, tensorflow, tensorboard,...
# Also do this to do some pre-compiling
#init_sentiment.ai()
```
## Testing it out
```{r, eval = FALSE}
## try a few sentences and extract the polarity:
text2test = c("This workshop is bad.",
"This workshop is not great.",
"This workshop is seldom bad.",
"This workshop is ok",
"This workshop is hardly very good",
"This workshop is very seldom very good",
"This workshop is great",
"This workshop is really good",
"This workshop is the absolute best")
# Individually
sentiment_score(text2test)
polarity(text2test,
polarity.frame = polarity_frame)$all
# In a more comparable format:
cbind(polarity = polarity(text2test,
polarity.frame = polarity_frame)$all$polarity,
TFModel = sentiment_score(text2test) )