-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta8tweets_tidy.Rmd
280 lines (211 loc) · 8.6 KB
/
delta8tweets_tidy.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
---
title: "delta8tweets"
author: "Drew Walker"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
bibliography: references.bib
---
# Tidy Adaptation
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#remotes::install_github("slu-openGIS/postmastr")
library(stm)
library(here)
library(postmastr)
library(textmineR)
library(tidyverse)
library(tidytext)
library(knitr) #Create nicely formatted output tables
library(kableExtra) #Create nicely formatted output tables
library(formattable) #For the color_tile function
library(lubridate)
library(academictwitteR)
library(tm)
library(tidygeocoder)
library(ggpubr)
library(topicmodels)
library(scales)
APIToken <- read_csv("apikeys.csv")
api_key <- as.character(APIToken[1,1])
api_secret_key <- as.character(APIToken[1,2])
bearer_token <- as.character(APIToken[1,3])
```
# Resources for topic modeling
Tidy Text Mining With R (Julia Silge) <https://www.tidytextmining.com/topicmodeling.html>
Predominantly used: has some older code that i had to fix, may try to convert to Julia's topicmodels package <https://towardsdatascience.com/beginners-guide-to-lda-topic-modelling-with-r-e57a5a8e7a25>
# Pull tweet full archive tweet data
- Add search by hashtag
```{r get-hashtag-delta8-tweets}
#
##Get tweets with Delta 8 thc or delta 8 hashtag
hashtag_delta8_tweets_jan_2020 <- get_all_tweets(
query = "#delta8",
start_tweets = "2020-01-01T00:00:00Z",
end_tweets = "2021-07-26T00:00:00Z",
bearer_token,data_path = "data/",
n = 100000)
users_hashtag_d8_tweets_jan_2020 <-
get_user_profile(unique(hashtag_delta8_tweets_jan_2020$author_id), bearer_token)
#
users_hashtag_d8_tweets_jan_2020 <- users_hashtag_d8_tweets_jan_2020 %>%
rename(author_id = id)
#
#
hashtag_delta8_tweets_and_user_info_jan_2020 <- left_join(hashtag_delta8_tweets_jan_2020, users_hashtag_d8_tweets_jan_2020, by = "author_id")
write_rds(hashtag_delta8_tweets_and_user_info_jan_2020, "hashtag_delta8_tweets_and_user_info_jan_2020.rds")
```
# Tweet data NLP and trend analysis
```{r delta8tweets,}
d8_tweets <- readRDS("hashtag_delta8_tweets_and_user_info_jan_2020.rds")
d8_tweets <- d8_tweets
```
# prepping for nlp
Decision was made to remove stopwords post hoc, due to inclusion of bigrams, and recent reseearch recommending for adhoc removeal instead. [@schofield2017]
- [@boon-itt2020] removed urls, emojis, special characters, retweets, hashtag symbols, hyperlinks.
- Also removed stopwords, lower cased, digits, words like delta8, delta, thc, http
```{r, preprocessing-tweets}
d8_tweets_text_id <- d8_tweets %>%
select(text,id)
#Token unnesting
#Remove @ and RT tweet notation
d8_tweets$text <- gsub("RT.*:", "", d8_tweets$text)
d8_tweets$text <- gsub("@.* ", "", d8_tweets$text)
# sub out digits , punctuation
#Should digits be included in case of delta 8, delta 10?
d8_tweets$text <- gsub('[[:punct:]]+', '', d8_tweets$text)
text_cleaning_tokens <- d8_tweets %>%
tidytext::unnest_tokens(word, text) %>%
left_join(d8_tweets_text_id, by = "id") %>%
mutate(raw_text = text)
#remove words? like
text_cleaning_tokens$word <- gsub('[[:digit:]]+', '', text_cleaning_tokens$word)
text_cleaning_tokens$word <- gsub('^https|^amp$|^delta$|^delta 8$|^thc$|^cbd$|^hemp$', '', text_cleaning_tokens$word)
#remove anything where word is only 1 character like a i d, remove stopwords
text_cleaning_tokens <- text_cleaning_tokens %>% filter(!(nchar(word) == 1))%>%
anti_join(stop_words)
#Stem/lemmatizer?
# https://blogs.cornell.edu/cornellnlp/2019/02/09/choose-your-words-wisely-for-topic-models/
# may not need to, is often done to save resources, or combine multiple words to mean same thing. May try to do as a sensitivity check
#remove commonly occurring words
#remove spaces
text_cleaning_tokens <- text_cleaning_tokens %>%
count(id,word) %>%
filter(word != "")
```
# Create document term matrix
```{r, create-dtm}
#create DTM
tweet_dtm <- text_cleaning_tokens %>%
cast_dtm(id,word,n)
```
# Running lda on up to 20 clusters, determining number of ks for analysis by highest coherence score
- We evaluated lda models using 1-20 clusters, and chose to conduct the analysis using 3 clusters due to the highest coherence score.
- We tried others inaugural_dfmas a sensitivity analysis
```{r, lda}
library(furrr)
plan(multiprocess)
models <- tibble(K = 2:15) %>%
mutate(topic_model = future_map(K, ~LDA(tweet_dtm,k = ., control = list(seed = 5849))))
current_time <- Sys.time()
st <- format(current_time,"%Y-%m-%d_%H_%M",)
rdsfilename <- paste0(here("summarysave"),st,".rds")
saveRDS(models, rdsfilename)
```
```{r, get-model-perplexity}
tibble(
k = models$K,
perplex = map_dbl(models$topic_model, perplexity)
) %>%
ggplot(aes(k, perplex)) +
geom_point() +
geom_line() +
labs(
title = "Evaluating LDA topic models",
subtitle = "Optimal number of topics (smaller is better)",
x = "Number of topics",
y = "Perplexity"
)
#Save ggplot below, change to classic theme
perplexityFilename <- paste0(here("perplexity"),st,".png")
ggsave(perplexityFilename)
```
```{r test-model}
topics <- 2:15
lda_and_summary <- function(topic_number){
best_fit_model <- LDA(tweet_dtm,k=topic_number, control = (list(seed = 5849)))
d8_twitter_topics_beta <- tidy(best_fit_model, matrix = "beta")
d8_twitter_topics_beta
d8_top_terms <- d8_twitter_topics_beta %>%
group_by(topic) %>%
slice_max(beta, n = 15) %>%
ungroup() %>%
arrange(topic, -beta)
top_terms_plot <- d8_top_terms %>%
mutate(term = reorder_within(term, beta, topic)) %>%
ggplot(aes(beta, term, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
scale_y_reordered() %>%
print()
current_time <- Sys.time()
st <- format(current_time,"%Y-%m-%d_%H_%M")
topicsFilename <- paste0(here("topics"),topic_number,st,".png")
ggsave(topicsFilename)
return(d8_top_terms)
}
topics_dfs <- topics %>%
as_tibble()
for (value in topics_dfs$value){
lda_and_summary(value)
}
```
"Alternatively, expert human judgment has been used to evaluate topic modeling outputs, especially when the modeling outputs are supposed to be used for human interpretation (Mimno et al., 2011, Mimno et al., 2011). Although human judgment is effective for judging the interpretability of topic modeling outputs, it is labor and time intensive (Baumer et al., 2017)."
- Meaning, if we can identify topics that highlight meaningful differences/features to identify texts. Can we use delphi method?
- Instructions: indicate a selected topic to most frequent word clusters that makes the most sense to substance abuse researcher/social media expert.
# Sentiment Analysis
```{r, nrc}
nrc <- get_sentiments("nrc") # get specific sentiment lexicons in a tidy format
nrc_words <- text_cleaning_tokens %>%
inner_join(nrc, by="word")
bar_words<- nrc_words %>%
group_by(sentiment) %>% # group by sentiment type
tally %>% # counts number of rows
arrange(desc(n)) %>%
mutate(percent = n/sum(n))
# arrange sentiments in descending order based on frequency
#get row percentage
nrc_bar_filename <- paste0(here("nrc_bar"),st,".png")
nrc_bar <- ggplot(bar_words, aes(x= fct_reorder(sentiment,n),y = n, fill = sentiment, label = percent)) +
geom_col(show.legend = FALSE,
width = 1)+
geom_text(aes(label = percent), hjust = .14)+
labs(x = "NRC Sentiment", y = "Frequency of Sentiment Words in Delta 8 Tweets")+
coord_flip()+
theme_classic()
nrc_bar
ggsave(nrc_bar_filename)
kable(bar_words)
sum(bar_words$n)
```
```{r, trends-over-time}
d8_tweets_by_date <- d8_tweets %>%
mutate(day = as.Date(created_at.x),
month = month(day),
year = year(day)) %>%
group_by(day) %>%
summarise(tweets_per_day = n())
tweets_per_day <- ggplot(d8_tweets_by_date, aes(day, tweets_per_day)) +
geom_point()+
geom_smooth(method = "loess", se = FALSE) +
labs(x = "January 1, 2020 to July 21, 2021", y = "Tweets per Day", title = "Frequency of Tweets mentioning #delta8")+
scale_x_date(date_breaks = "2 months", date_labels = "%b-%y")+
theme_classic()
tweets_per_day
```
# Explorations
1. Filter for "effects"
2. Maybe try to match with a vocabulary of drug effects? 1. Many of these topics are around different sites selling-- can we prove this by seeing topic:user ratios? 1. Anxiety, High, euphoria, psychoactive, real, depression, calm, wellness, anti(anxiety or depression)
3. legal (anything interesting we could do there?)
4. topic models conducted after filtering for words?
5. Group by authors-- aka concatenate all text data for each unique author (where authors are documents, instead of tweets) Not sure if this has been done
1. [@hong2010] pool tweets by author