-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path05_cta.qmd
212 lines (144 loc) · 3.92 KB
/
05_cta.qmd
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
---
title: "Computational Text Analysis"
subtitle: "SICSS, 2022"
author: Christopher Barrie
format:
revealjs:
chalkboard: true
editor: visual
---
## Manipulating text
```{r, eval = T, echo = T}
library(dplyr) #tidyverse package for wrangling data
library(quanteda) #quantitative text analysis package
library(tidytext) #package for 'tidy' manipulation of text data
library(topicmodels) #for topic modelling
library(ggplot2) #package for visualizing data
library(ggthemes) #to make plots nicer
library(stringi) #to generate random text
```
## Topic modelling
```{r, eval = T, echo = T}
#| code-line-numbers: "|1|2|4"
data("AssociatedPress",
package = "topicmodels")
ap_tidy <- tidy(AssociatedPress)
ap_tidy
```
## Topic modelling
```{r, eval = T, echo = T}
data("AssociatedPress",
package = "topicmodels")
str(AssociatedPress)
```
## Inspect topic terms
```{r, eval = T, echo = T}
lda_output <- LDA(AssociatedPress[1:100,], k = 10)
terms(lda_output, 10)
```
## Inspect βs
```{r, eval = T, echo = T}
lda_beta <- tidy(lda_output, matrix = "beta")
lda_beta %>%
arrange(-beta)
```
## Inspect γs
```{r, eval = T, echo = T}
lda_gamma <- tidy(lda_output, matrix = "gamma")
lda_gamma %>%
arrange(-gamma)
```
## Plot
```{r, eval = T, echo = T}
lda_beta %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta) %>%
mutate(term = reorder_within(term, beta, topic)) %>%
ggplot(aes(beta, term, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free", ncol = 4) +
scale_y_reordered() +
theme_tufte(base_family = "Helvetica")
```
## Plot
::: panel-tabset
### Code
```{r, echo = T, eval = T}
betaplot <- lda_beta %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta) %>%
mutate(term = reorder_within(term, beta, topic)) %>%
ggplot(aes(beta, term, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free", ncol = 4) +
scale_y_reordered() +
theme_tufte(base_family = "Helvetica")
```
### Plot
```{r, eval = T, echo = F}
betaplot
```
:::
## Word embedding
```{r, eval = T, echo = F}
library(Matrix) #for handling matrices
library(tidyverse)
library(irlba) # for SVD
library(umap) # for dimensionality reduction
load("data/pmi_svd.RData")
load("data/pmi_matrix.RData")
```
```{r, eval = F, echo = T}
library(Matrix) #for handling matrices
library(tidyverse)
library(irlba) # for SVD
library(umap) # for dimensionality reduction
```
## Data structure
- Word pair matrix with PMI (Pairwise mutual information)
- where PMI = log(P(x,y)/P(x)P(y))
- and P(x,y) is the probability of word x appearing within a six-word window of word y
- and P(x) is the probability of word x appearing in the whole corpus
- and P(y) is the probability of word y appearing in the whole corpus
## Data structure
```{r ,echo=F}
head(pmi_matrix[1:6, 1:6])
```
## Data structure
```{r ,echo=F}
glimpse(pmi_matrix)
```
## Singular value decomposition
```{r ,echo=F, eval = F}
pmi_svd <- irlba(pmi_matrix, 256, maxit = 500)
```
## Singular value decomposition
```{r ,echo=F, eval = F}
pmi_svd <- irlba(pmi_matrix, 256, maxit = 500)
```
## Singular value decomposition
```{r ,echo=T, eval = T}
word_vectors <- pmi_svd$u
rownames(word_vectors) <- rownames(pmi_matrix)
dim(word_vectors)
```
## Singular value decomposition
```{r ,echo=T, eval = T}
head(word_vectors[1:5, 1:5])
```
## Word embeddings with GloVe
After we've generated our term co-ocurrence matrix...
```{r, eval = F, echo = T}
DIM <- 300
ITERS <- 100
# ================================ set model parameters
# ================================
glove <- GlobalVectors$new(rank = DIM, x_max = 100, learning_rate = 0.05)
# ================================ fit model ================================
word_vectors_main <- glove$fit_transform(tcm, n_iter = ITERS, convergence_tol = 0.001,
n_threads = RcppParallel::defaultNumThreads())
```