/
CAS825_text_analysis_example.R
245 lines (201 loc) · 4.97 KB
/
CAS825_text_analysis_example.R
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
######### CAS 825 - COVID19 project
######### This code is developed to explore and analyze COVID19 Twitter data (Text data) in CAS 825 class
# install packages
#install.packages("RColorBrewer")
#install.packages("topicmodels")
#install.packages("twitteR")
#install.packages("tm")
#install.packages("wordcloud")
#install.packages("NLP")
#install.packages("ggplot2")
#install.packages("tidyverse")
#install.packages("tidytext")
# load packages
library(RColorBrewer)
library(topicmodels)
library(twitteR)
library(tm)
library(wordcloud)
library(NLP)
library(ggplot2)
library(tidyverse)
library(tidytext)
# setting working directory
setwd("/Users/Ralf/Desktop/sanguk")
# loading the data
covid_df <- read.csv("COVID19_sample.csv")
# check variables
names(covid_df)
# check first a few rows
head(covid_df)
# check data type
str(covid_df)
# select English text only
unique(covid_df$lang)
covid_df_english <- covid_df[which(covid_df$lang == "en"),]
# check if data includes only English texts
unique(covid_df_english$lang)
################# 1. Tokenizing Text
#covid_df$text <- iconv(covid_df$text, "UTF-8", "ASCII")
tweets_tokens <- covid_df_english %>%
unnest_tokens(word, text)
names(tweets_tokens)
tweets_tokens$word
# count tokens
tweets_tokens %>%
count(word) %>%
arrange(desc(n))
################# 2. Text Preprocessing (stopwords = words are not informative)
# stop_words are pre-loaded from a package
stop_words
# let's remove stopwords using anti_join function
tweets_rm_stop <- tweets_tokens %>%
anti_join(stop_words)
# count cleaned tokens
tweets_rm_stop %>%
count(word) %>%
arrange(desc(n))
# Word tokens seems cleaner than before, but there are still a few words that are not that informative
# let's remove those words by customizing our own word dictionary
# customizing stopwords
custom_stop_words <- tribble(
~word, ~lexicon,
"rt", "CUSTOM",
"https", "CUSTOM",
"t.co", "CUSTOM"
)
# integrate customized stopwords into the existing stopwords
stop_words2 <- stop_words %>%
bind_rows(custom_stop_words)
# let's remove those words using anti_join function again
tweets_rm_stop2 <- tweets_tokens %>%
anti_join(stop_words2)
# let's see if tokenized words look even cleaner
tweets_rm_stop2 %>%
count(word) %>%
arrange(desc(n))
################# 3. Visualization (word count, word cloud)
# 3-1) Visualize word counts
word_counts <- tweets_rm_stop2 %>%
count(word) %>%
arrange(desc(n))
word_counts
ggplot(word_counts, aes(x = word, y = n)
) +
geom_col()
# Using filtre function to plot only high freequencty words (n > 20)
word_counts2 <- tweets_rm_stop2 %>%
count(word) %>%
filter(n > 20) %>%
arrange(desc(n))
ggplot(word_counts2, aes(x = word, y = n)
) +
geom_col()
# flip x and y to make the graph more visible
ggplot(word_counts2, aes(x = word, y = n)
) +
geom_col() +
coord_flip() +
ggtitle("COVID19 Word Counts")
# ordering by the frequency number
word_counts2 <- tweets_rm_stop2 %>%
count(word) %>%
filter(n > 20) %>%
mutate(word2 = fct_reorder(word, n)) # reorder word according to n (frequency)
ggplot(word_counts2, aes(x = word2, y = n)
) +
geom_col() +
coord_flip() +
ggtitle("COVID19 Word Counts")
# 3-2) plot wordcloud
wordcloud(
words = word_counts$word,
freq = word_counts$n,
min.freq = 10,
max.word = 200,
random.order = FALSE,
rot.per = 0.15,
colors = brewer.pal(8, "Dark2")
)
################# 4. Topic Modeling
# 4-1) Generate document term matrix
# check the structure of document term matrix
names(tweets_rm_stop2)
tweets_rm_stop2 %>%
count(word, id) %>%
cast_dtm(id, word, n)
# Generate document term matrix
dtm1 <- tweets_rm_stop2 %>%
count(word, id) %>%
cast_dtm(id, word, n) %>%
as.matrix()
dtm1[1:5, 1:10]
dtm1[1:5, 10:20]
dtm1[1:5, 1000:1010]
# there are 1233776 elements. This is
464*2659
# 4-2) Apply LDA Topic Modeling
# Two topics
LDA_out <- LDA(
dtm1,
k = 2,
method = 'Gibbs',
control = list(seed =42)
)
# screening LDA output
LDA_out
glimpse(LDA_out)
# preperation for visualization
LDA_topics <- LDA_out %>%
tidy(matrix = 'beta')
LDA_topics %>%
arrange(desc(beta))
word_probs <- LDA_topics %>%
group_by(topic) %>%
top_n(15, beta) %>%
ungroup() %>%
mutate(term2 = fct_reorder(term, beta))
# Visualizing two topic model
ggplot(
word_probs,
aes(
term2,
beta,
fill = as.factor(topic)
)
) +
geom_col(show.legend = FALSE) +
facet_wrap(~topic, scales = "free") +
coord_flip()
# Four topics
LDA_out <- LDA(
dtm1,
k = 4,
method = 'Gibbs',
control = list(seed =42)
)
# screening LDA output
LDA_out
glimpse(LDA_out)
# preperation for visualization
LDA_topics <- LDA_out %>%
tidy(matrix = 'beta')
LDA_topics %>%
arrange(desc(beta))
word_probs <- LDA_topics %>%
group_by(topic) %>%
top_n(15, beta) %>%
ungroup() %>%
mutate(term2 = fct_reorder(term, beta))
# Visualizing four topic model
ggplot(
word_probs,
aes(
term2,
beta,
fill = as.factor(topic)
)
) +
geom_col(show.legend = FALSE) +
facet_wrap(~topic, scales = "free") +
coord_flip()