-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.Rmd
More file actions
272 lines (212 loc) · 6.95 KB
/
analysis.Rmd
File metadata and controls
272 lines (212 loc) · 6.95 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
---
title: "Twitter Data Analysis"
author: "Marta Fattorel"
output: github_document
---
```{r message=FALSE}
library(rtweet)
library(ggplot2)
library(wordcloud)
library(tm)
library(topicmodels)
library(lubridate)
library(SentimentAnalysis)
library(quanteda)
library(ggpubr)
library(dplyr)
library(tidytext)
```
# RETRIEVE TWITTER DATA
```{r}
# consumer_key <- "xxx"
# consumer_secret <- "xxx"
# access_token <- "xxx"
# access_secret <- "xxx"
#
# token <- create_token(
# app = "xxx",
# consumer_key = consumer_key,
# consumer_secret = consumer_secret,
# access_token = access_token,
# access_secret = access_secret)
```
```{r}
# # retrieve 100,000 tweets from July 5 to July 12
# HK_tweets <- search_tweets(q = "Hong Kong OR #hongkongers OR #HKPolice", n = 100000,
# lang = "en",
# include_rts = FALSE,
# since = "2020-07-05",
# until = "2020-07-12",
# retryonratelimit = TRUE)
#
# HK_df <- data.frame(HK_tweets$text, HK_tweets$created_at)
#
# # select a random subset of 7,000 tweets
# samp <- sample(nrow(HK_df), 7000)
# HK_df <- HK_df[samp,]
#
# colnames(HK_df) <- c("Text", "Date")
# row.names(HK_df) <- NULL
# HK_df$Date <- as.Date(ymd_hms(HK_df$Date)) # remove time
# write.csv(HK_df, "HK_df.csv", quote = TRUE) # save the dataframe
```
# DATA PRE-PROCESSING - TEXT MINING
```{r}
HK_df <- read.csv("HongKong.csv", row.names = 1, stringsAsFactors = TRUE)
```
```{r}
# remove duplicates
HK_df <- HK_df[unique(HK_df$Text),]
#row.names(HK_df) <- NULL
# remove mentions
HK_df$Text <- gsub("@[[:alpha:]]*","", HK_df$Text)
# remove emoji in unicode
HK_df$Text <- gsub("[<].*[>]", "", HK_df$Text)
# remove tabs
HK_df$Text = gsub("[ |\t]{2,}", " ", HK_df$Text)
# leading blanks
HK_df$Text = gsub("^ ", "", HK_df$Text)
# lagging blankc
HK_df$Text = gsub(" $", "", HK_df$Text)
# general spaces
HK_df$Text = gsub(" +", " ", HK_df$Text)
# convert to basic ASCII text
HK_df$Text <- iconv(HK_df$Text, to = "ASCII", sub = " ")
```
```{r}
# build a corpus
text_corpus <- Corpus(VectorSource(HK_df$Text))
# convert to lower case
text_corpus <- tm_map(text_corpus, content_transformer(tolower))
# words to remove
text_corpus <- tm_map(text_corpus, removeWords,
c("will", "just", "can", "amp", "via", "also", "yet", "per", "hong", "kong", "s", "t", "get", "hk", "because", "hongkongers", "hkpolice", "hongkong"))
# stem words
text_corpus <- tm_map(text_corpus, stemDocument)
# remove stopwords
text_corpus <- tm_map(text_corpus, removeWords,
stopwords("english"))
# remove punctuation
text_corpus <- tm_map(text_corpus, removePunctuation)
# remove URLs
removeURL <- function(x) gsub("http[^[:space:]]*", "", x)
text_corpus <- tm_map(text_corpus, content_transformer(removeURL))
# remove extra whitespace
text_corpus <- tm_map(text_corpus, stripWhitespace)
# remove numbers
text_corpus <- tm_map(text_corpus, removeNumbers)
```
```{r}
# build a document term matrix
dtm <- TermDocumentMatrix(text_corpus)
matrix <- as.matrix(dtm)
words <- sort(rowSums(matrix),decreasing = TRUE)
df <- data.frame(word = names(words),freq = words)
```
```{r}
# plot the word cloud
set.seed(1234) # for reproducibility
wordcloud(words = df$word, freq = df$freq, min.freq = 1, max.words = 200, random.order = FALSE, rot.per = 0.35, colors = brewer.pal(12, "Paired"))
```
```{r}
# clean corpus back to the HK_df
text_df <- data.frame(text_clean = get("content", text_corpus),
stringsAsFactors = FALSE)
HK_df$Text <- text_df$text_clean
```
```{r}
# Generate Tokens
toks <- tokens(HK_df$Text)
```
```{r}
# plot the network of words co-occurrences
set.seed(30)
fcmat <- fcm(toks, context = "document", tri = FALSE)
feat <- names(topfeatures(fcmat, 30))
fcm_select(fcmat, pattern = feat) %>%
textplot_network(min_freq = 0.5)
```
# TOPIC MODELLING
```{r}
# select a random subset of 3,500 tweets
set.seed(100)
samp <- sample(nrow(HK_df), 3500)
corpus_sub <- Corpus(VectorSource(HK_df$Text[samp]))
dtm_sub = DocumentTermMatrix(corpus_sub)
doc.length = apply(dtm_sub, 1, sum) # find the sum of words in each document
dtm_sub = dtm_sub[doc.length > 0,] # remove all docs without words
```
```{r}
# Number of topics
k <- 3
# Run LDA using Gibbs sampling
ldaOut3 <-LDA(dtm_sub, k, method = "Gibbs", control =
list(nstart = 5, seed = list(2003, 5, 63, 100001, 765), best = TRUE,
burnin = 4000, iter = 2000, thin = 500))
```
```{r}
topics <- as.matrix(topics(ldaOut3))
terms <- as.matrix(terms(ldaOut3, 10))
topics_prob <- as.matrix(ldaOut3@gamma)
```
```{r}
# top 10 words within each topic according to beta (i.e. per-topic-per-word probability)
topics_beta <- tidy(ldaOut3, matrix = "beta")
top_terms_b <- topics_beta %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)
theme_set(theme_classic())
top_terms_b %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
labs(x = NULL, y = NULL) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
theme(axis.text.x = element_text(angle = 30, vjust = 0.5, size = 8)) +
coord_flip()
```
# SENTIMENT ANALYSIS
```{r}
HK_sentiment <- analyzeSentiment(HK_df$Text)
# keep only these columns
HK_sentiment <- dplyr::select(HK_sentiment,
SentimentGI,
WordCount)
# add to the dataset
HK_df <- cbind.data.frame(HK_df, HK_sentiment)
HK_df <- HK_df[!is.na(HK_df$SentimentGI),] # remove NA
```
```{r}
# plot the distribution of SentimentGI
theme_set(theme_classic())
ggplot(HK_df[, 3:4], aes(SentimentGI)) +
geom_histogram(binwidth = .05)
```
```{r}
# find the word-frequency in positive and negative tweets, without considering neutral ones
text_corpus_pos <- Corpus(VectorSource(HK_df[HK_df$SentimentGI > 0, "Text"]))
dtm.pos <- TermDocumentMatrix(text_corpus_pos)
mat.pos <- as.matrix(dtm.pos)
words.pos <- sort(rowSums(mat.pos), decreasing = TRUE)
text_corpus_neg <- Corpus(VectorSource(HK_df[HK_df$SentimentGI < 0, "Text"]))
dtm.neg <- TermDocumentMatrix(text_corpus_neg)
mat.neg <- as.matrix(dtm.neg)
words.neg <- sort(rowSums(mat.neg), decreasing = TRUE)
df_sent_pos <- data.frame(word = names(words.pos), freq = words.pos)
df_sent_neg <- data.frame(word = names(words.neg), freq = words.neg)
```
```{r}
# plot the most frequent words
theme_set(theme_classic())
df_sent_pos$word <- factor(df_sent_pos$word, levels = df_sent_pos$word)
g1 <- ggplot(df_sent_pos[1:20,], aes(reorder(word, freq), y=freq)) +
geom_bar(stat ='identity', width =.5, fill = "darkgreen") +
labs(x = NULL, y = NULL, title = "Positive tweets") + coord_flip()
df_sent_neg$word <- factor(df_sent_neg$word, levels = df_sent_neg$word)
g2 <- ggplot(df_sent_neg[1:20,], aes(reorder(word, freq), y=freq)) +
geom_bar(stat ='identity', width = .5, fill = "darkred") +
labs(x = NULL, y = NULL, title = "Negative tweets") + coord_flip()
ggarrange(g1, g2, ncol = 2, nrow = 1)
```