-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-Topic Modeling.R
More file actions
292 lines (205 loc) · 8.86 KB
/
7-Topic Modeling.R
File metadata and controls
292 lines (205 loc) · 8.86 KB
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
################################################################################
# Amanda McGowin
# Thesis: An Analysis of Major Acquisition Reforms Through Text Mining and
# Grounded Theory Design
#
# 8. Topic Modeling
#
# Contains:
# Convert tidy-data frame to matrix (line 18),
# Estimate number of topics for LDA model parameter (k) (line 85),
# and
# Fit data to a topic model (line 174)
################################################################################
############################################
# convert tidy-text data frame to matrix #
############################################
# Document Term Matrix
# data_tb$NAME <- as.character(data_tb$NAME)
data_mx <- data_tb %>% # All Data
prep_analysis_word("TXT") %>%
count(NAME, word) %>%
cast_dtm(NAME, word, n)
data_mx_expert <- data_tb %>% # Compendium Data
prep_analysis_word("TXT") %>%
filter(CLASS1 == "Compendium") %>%
count(NAME, word) %>%
cast_dtm(NAME, word, n)
data_mx_expert <- data_mx_expert[apply(data_mx_expert, 1, sum)> 0, ]
data_mx_reform <- data_tb %>% # Reform Data
prep_analysis_word("TXT") %>%
filter(CLASS1 == "Reform")
data_mx_reform$NAME <- factor(data_mx_reform$NAME)
data_mx_reform <- data_mx_reform %>%
count(NAME, word) %>%
cast_dtm(NAME, word, n)
data_mx_reform <- data_mx_reform[apply(data_mx_reform, 1, sum)> 0, ]
data_mx_Gansler <- subset_tb %>% # Gansler Data
prep_analysis_word("TXT") %>%
filter(str_detect(NAME, "^Gansler"))
data_mx_Gansler$NAME <- factor(data_mx_Gansler$NAME)
data_mx_Gansler <- data_mx_Gansler %>%
count(NAME, word) %>%
cast_dtm(NAME, word, n)
data_mx_Gansler <- data_mx_Gansler[apply(data_mx_Gansler, 1, sum)> 0, ]
data_mx_WSARA <- subset_tb %>% # WSARA Subset Data
prep_analysis_word("TXT") %>%
filter(str_detect(NAME, "^WSARA"))
data_mx_WSARA$NAME <- factor(data_mx_WSARA$NAME)
data_mx_WSARA <- data_mx_WSARA %>%
count(NAME, word) %>%
cast_dtm(NAME, word, n)
data_mx_WSARA <- data_mx_WSARA[apply(data_mx_WSARA, 1, sum)> 0, ]
##################################
# Determining number of topics #
##################################
# -----------------------------------------------------------------------------
# estimate best fitting number of topics for LDA model parameter (k)
# -----------------------------------------------------------------------------
# ALL DATA
# (2, 100, by=1) = about 10-14 topics Runtime = 2 hr 10 mins (2-core)
# (2, 50, by=1) = about 8-14 topics Runtime = 30.74 mins
# (2, 30, by=1) = about 6-10 topics Runtime = 9.78 mins
result_all <- data_mx %>%
FindTopicsNumber(topics = seq(2, 30, by=1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010",
"Deveaud2014"),
method = "Gibbs",
control = list(seed = 1234),
mc.cores = 3L,
verbose = TRUE)
FindTopicsNumber_plot(result_all)
# Compendium DATA
# (2, 20, by=1) = about 5-8 topics Runtime = 2.735 mins
result_expert <- data_mx_expert %>%
FindTopicsNumber(topics = seq(2, 20, by=1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010",
"Deveaud2014"),
method = "Gibbs",
control = list(seed = 1234),
mc.cores = 3L,
verbose = TRUE)
FindTopicsNumber_plot(result_expert)
# Reform DATA
# (2, 20, by=1) = about 7-9 topics Runtime = 1.729 mins
result_reform <- data_mx_reform %>%
FindTopicsNumber(topics = seq(2, 20, by=1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010",
"Deveaud2014"),
method = "Gibbs",
control = list(seed = 1234),
mc.cores = 3L,
verbose = TRUE)
FindTopicsNumber_plot(result_reform)
# Gansler (For GT Comparison)
# (2, 20, by=1) = about 4-8 topics Runtime = 8.623 secs
result_Gansler <- data_mx_Gansler %>%
FindTopicsNumber(topics = seq(2, 20, by=1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010",
"Deveaud2014"),
method = "Gibbs",
control = list(seed = 1234),
mc.cores = 3L,
verbose = TRUE)
FindTopicsNumber_plot(result_Gansler)
# WSARA Subset (For GT Comparison)
# (2, 20, by=1) = about 4-7 topics Runtime = 6.784 secs
result_WSARA <- data_mx_WSARA %>%
FindTopicsNumber(topics = seq(2, 20, by=1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010",
"Deveaud2014"),
method = "Gibbs",
control = list(seed = 1234),
mc.cores = 3L,
verbose = TRUE)
FindTopicsNumber_plot(result_WSARA)
#####################
# Topic Modeling #
####################
# fit data to a topic model (number of topics, k=6-10)
lda_ALL <- LDA(data_mx, k = 10, control = list(seed = 1234))
# (number of topics, k=5-8)
lda_expert <- LDA(data_mx_expert, k = 8, control = list(seed = 1234))
# (number of topics, k=7-9)
lda_reform <- LDA(data_mx_reform, k = 9, control = list(seed = 1234))
# (number of topics, k=4-8)
lda_Gansler <- LDA(data_mx_Gansler, k = 8, control = list(seed = 1234))
# (number of topics, k=4-6)
lda_WSARA <- LDA(data_mx_WSARA, k = 6, control = list(seed = 1234))
# -----------------------------------------------------------------------------
# extract the "per-topic-per-word" probabilities (beta) form the LDA model
# computes probability of that term being generated from that topic
# top 10 terms that are most common within each topic
# This visualization lets us understand the two topics that were extracted from
# the articles
#data_topics <- tidy(data_lda, matrix = "beta")
# -----------------------------------------------------------------------------
# All Data Topics
lda_ALL %>% tidy(matrix = "beta") %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
ggplot(aes(drlib::reorder_within(term, beta, topic), beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
drlib::scale_x_reordered() +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
theme(axis.text.y = element_text(size = 15),
strip.text = element_text(size = 12))
# Compendium Data
lda_expert %>% tidy(matrix = "beta") %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
ggplot(aes(drlib::reorder_within(term, beta, topic), beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
drlib::scale_x_reordered() +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
theme(axis.text.y = element_text(size = 15),
strip.text = element_text(size = 12))
# Reform Data
lda_reform %>% tidy(matrix = "beta") %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
ggplot(aes(drlib::reorder_within(term, beta, topic), beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
drlib::scale_x_reordered() +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
theme(axis.text.y = element_text(size = 15),
strip.text = element_text(size = 12))
# Grounded Theory Comparison (Gansler)
lda_Gansler %>% tidy(matrix = "beta") %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
ggplot(aes(drlib::reorder_within(term, beta, topic), beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
drlib::scale_x_reordered() +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
theme(axis.text.y = element_text(size = 15),
strip.text = element_text(size = 12))
# Grounded Theory Comparison (WSARA)
lda_WSARA %>% tidy(matrix = "beta") %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
ggplot(aes(drlib::reorder_within(term, beta, topic), beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
drlib::scale_x_reordered() +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
theme(axis.text.y = element_text(size = 15),
strip.text = element_text(size = 12))
# # -----------------------------------------------------------------------------
# # extract the "per-document-per-topic" probabilities (gamma) form the LDA model
# # estimated proportion of words from that document generated from that topic
# # probability of a topic belonging to a document??
#
# data_documents <- tidy(data_lda, matrix = "gamma")
# # -----------------------------------------------------------------------------
#
# data_documents <- tidy(lda_ALL, matrix = "gamma")