-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitterGender.Rmd
600 lines (482 loc) · 19.9 KB
/
twitterGender.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
---
title: "White men and black women: What Twitter says about gender"
author: "Peer Christensen"
date: "1/1/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=F, message=F, fig.width = 9, fig.height = 7,fig.pos = "center")
```
I've been wanting to try out the excellent rtweet package for some time now, and I recently came up with the idea to explore what Twitter users are tweeting about men and women these days. In this post, we'll look at gender pronouns and word frequency in the following analyses:
- Words co-occurring with the words 'men' and 'women'
- Hashtags co-occurring with 'men' and 'women'
- Bigrams ending in 'men' and 'women'
- Bigrams starting with the possessive pronouns 'his' and 'her'
- Tri- and quadrogram(?) constructions starting with "s/he is .." and "s/he is a/an .."
We won't cover how to download tweets with rtweet, but instead move right to the analyses.
First, we'll need to load a few other packages.
```{r}
library(tidyverse)
library(tidytext)
library(wordcloud)
library(tm)
library(widyr)
library(udpipe)
library(magrittr)
library(gridExtra)
```
## Men and Women
The first tweets that we'll explore all contain the word 'men' or 'women'.
```{r}
df <- read_csv("mwTweets.csv")
glimpse(df)
range(df$created_at)
range(df$created_at)[2]-range(df$created_at)[1]
```
As you can see, there's plenty of metadata surrounding the 214,741 tweets that I've downloaded from a roughly 20 hour period between December 17-18, 2018.
Next, we'll do some preprocessing where we remove some stopwords and create a 'gender' variable to categorize our tweets.
```{r}
df %<>%
select(text, screen_name) %>%
mutate(text = tolower(text)) %>%
mutate(gender = case_when(str_detect(text, "women") &
str_detect(text, " men") ~ "both",
str_detect(text, "women") ~ "women",
str_detect(text, "men") ~ "men")) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words[stop_words$lexicon=="SMART",]) %>%
mutate(word = removeWords(word,c(stopwords(),"t.co","https","amp","'s","’s"))) %>%
add_count(word) %>%
filter(n > 1, word != "", gender != "both") %>%
select(-n)
table(df$gender)
```
We see that the majority of tweets contain 'women' rather than 'men'.
Using the pairwise_count function from the widyr package, we can create new data frames with the words that most often co-occur with 'men' and 'women'.
```{r}
word_pairs_men <- df %>%
filter(gender == "men") %>%
pairwise_count(word, screen_name, sort = TRUE) %>%
filter(item1 == "men") %>%
top_n(20)
word_pairs_women <- df %>%
filter(gender == "women") %>%
pairwise_count(word, screen_name, sort = TRUE) %>%
filter(item1 == "women") %>%
top_n(20)
```
Let's put the data back together and plot the most common words.
```{r}
word_pairs <- rbind(word_pairs_men, word_pairs_women) %>%
mutate(order = rev(row_number()), item1 = factor(item1, levels = c("men", "women")))
word_pairs %>%
ggplot(aes(x = order, y = n, fill = item1)) +
geom_col(show.legend = FALSE) +
scale_x_continuous(breaks = word_pairs$order,
labels = word_pairs$item2,
expand = c(0,0)) +
facet_wrap(~item1, scales = "free") +
scale_fill_manual(values = c("steelblue", "indianred")) + coord_flip() + labs(x = "words") +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
strip.text.x = element_text(size=22, face="bold"),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank())
```
It seems that tweets tend to characterise people on the basis of skin colour.
A more direct way to explore this, will be to analyse two-word colocations, or bigrams, where 'men' or 'women' appear as the second word in the pair.
Let's first do the necessary preprocessing and create wordcloud starting with colocations with 'men' as the second word.
```{r}
men <- read_csv("mwTweets.csv") %>%
select(screen_name, text) %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(word2 == "men")
menCount <- men %>%
count(word1,word2) %>%
select(word1,n) %>%
arrange(desc(n)) %>%
anti_join(stop_words[stop_words$lexicon=="SMART",],by = c("word1" = "word"))
wordcloud(words = menCount$word1, freq = menCount$n, min.freq = 30, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(9,"Blues")[4:9])
```
Let's skip the code and do the same for colocations with 'women'.
```{r echo=F}
women <- read_csv("mwTweets.csv") %>%
select(screen_name, text) %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(word2 == "women")
womenCount <- women %>%
filter(word1!="amp",word1!="ii") %>%
count(word1,word2) %>%
select(word1,n) %>%
arrange(desc(n)) %>%
anti_join(stop_words[stop_words$lexicon=="SMART",],by = c("word1" = "word"))
wordcloud(words = womenCount$word1, freq = womenCount$n, min.freq = 30, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(9,"Reds")[4:9])
```
In order to make comparisons a little easier, we'll can plot frequencies more accurately with bar charts.
```{r}
menCountTop <- menCount %>%
filter(word1!="amp",word1!="ii") %>%
mutate(row = rev(row_number())) %>%
top_n(20,n)
menPlot <- menCountTop %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = menCountTop$row,
labels = menCountTop$word1,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"__ Men\"") +
scale_fill_gradient(low=brewer.pal(9,"Blues")[2],high=brewer.pal(9,"Blues")[9])
womenCountTop <- womenCount %>%
mutate(row = rev(row_number())) %>%
top_n(20,n)
womenPlot <- womenCountTop %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = womenCountTop$row,
labels = womenCountTop$word1,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.title.y = element_text(margin = margin(r = 40,l=40)),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"__ Women\"") +
scale_fill_gradient(low=brewer.pal(9,"Reds")[2],high=brewer.pal(9,"Reds")[9])
grid.arrange(menPlot,womenPlot, ncol = 2)
```
There are plenty of observations to make in these charts. They confirm that skin colour frequently precedes 'men' and 'women'. Interestingly, the relative frequency of 'black' and 'white' is reversed for the two genders, though I kind of suspected that 'white men' would be a prominent colocation.
We can also observe that the sexual orientation of men is highlighted, and that 'trans' appears more frequently before 'women'.
Lastly, let's compute and visualize the frequency of the most common hashtags co-occurring with 'men' and 'women' that also contain the forms 'men' and 'women'.
```{r}
df <- read_csv("mwTweets.csv") %>%
select(X1,screen_name,text) %>%
mutate(text = tolower(text))
remove_reg <- "&|<|>"
df <- df %>%
filter(!str_detect(text, "^RT")) %>%
mutate(text = str_remove_all(text, remove_reg)) %>%
unnest_tokens(hashtag, text, token = "tweets") %>%
filter(!hashtag %in% stop_words$word,
!hashtag %in% str_remove_all(stop_words$word, "'")) %>%
filter(str_detect(hashtag, "^#")) %>%
mutate(hashtag = str_remove(hashtag,"#")) %>%
filter(str_detect(hashtag,"men|women")) %>%
filter(hashtag != "men", hashtag != "women",hashtag != "mens", hashtag != "womens", !str_detect(hashtag,"ment"))
tags <- df %>%
group_by(hashtag) %>%
count() %>%
arrange(desc(n))
tags <- tags %>%
mutate(gender = case_when( str_detect(hashtag,"women") ~ "f",
!str_detect(hashtag,"women") ~ "m"))
menTags <- tags %>%
filter(gender == "m")
womenTags <- tags %>%
filter(gender == "f")
wordcloud(words = menTags$hashtag, freq = menTags$n, min.freq = 3, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(9,"Blues")[4:9])
wordcloud(words = womenTags$hashtag, freq = womenTags$n, min.freq = 3, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(9,"Reds")[4:9])
```
For whatever reason, the hashtags co-occurring with 'men' revolve around fashion, style and grooming.
By contrast, the hashtags co-occurring with 'women' reflect career choices (STEM, tech, business). More generally, the construction "women in X" appears to be highly productive and frequent.
## His and her bigrams
Next, we'll perform frequency analyses of words following the possessive pronouns starting with 'his'.
```{r}
his <- read_csv("hisTweets.csv") %>%
select(text) %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(word1 == "his")
his
```
We're also going to perform parts-of-speech tagging and narrow the lexical items down to nouns. To do this, we'll use the udpipe package.
```{r}
# udmodel <- udpipe_download_model(language = "english")
udmodel <- udpipe_load_model("english-ud-2.0-170801.udpipe")
include <- udpipe(x = his$word2,
object = udmodel)
include <- include %>%
select(token,upos) %>%
filter(upos =="NOUN") %>%
select(token)
his <- his %>%
filter(word2 %in% include$token)
his
```
Now, we can count and prepare a bar chart of the most frequent nouns following 'his'.
```{r}
hisCount <- his %>%
count(word1,word2) %>%
arrange(desc(n)) %>%
select(word2,n) %>%
mutate(row = rev(row_number()))
hisPlot <- hisCount %>%
top_n(20,n) %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = hisCount$row,
labels = hisCount$word2,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"His __\"") +
scale_fill_gradient(low=brewer.pal(9,"Blues")[2],high=brewer.pal(9,"Blues")[9])
```
```{r echo=F}
library(cldr)
her <- read_csv("herTweets.csv") %>%
select(screen_name, text) %>%
mutate(language = detectLanguage(text)$detectedLanguage) %>%
filter(language=="ENGLISH")
her <- her %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(word1 == "her")
#udmodel <- udpipe_download_model(language = "english")
udmodel <- udpipe_load_model("english-ud-2.0-170801.udpipe")
include <- udpipe(x = her$word2,
object = udmodel)
include <- include %>%
select(token,upos) %>%
filter(upos =="NOUN") %>%
select(token)
her <- her %>%
filter(word2 %in% include$token)
herCount <- her %>%
count(word1,word2) %>%
arrange(desc(n)) %>%
select(word2,n) %>%
mutate(row = rev(row_number()))
herPlot <- herCount %>%
top_n(20,n) %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width=.9) +
coord_flip() +
scale_x_continuous(
breaks = herCount$row,
labels = herCount$word2,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 16),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"Her __\"") +
scale_fill_gradient(low=brewer.pal(9,"Reds")[2],high=brewer.pal(9,"Reds")[9])
```
I've completed the same steps for nouns following 'her'. Let's plot and compare the results!
```{r}
grid.arrange(hisPlot,herPlot, ncol = 2)
```
Apparently, tweets about possessions and attributes are often concerned with family relations and body parts.
## "What is s/he?"
In this section, we'll examine the most frequent trigram constructions with the form "s/he is X"
We'll again use parts-of-speech tagging and only consider adjectives in the place of X.
```{r}
heTweets <- read_csv("heTweets.csv")
he <- heTweets %>%
select(screen_name, text) %>%
mutate(text = tolower(text)) %>%
mutate(text = str_replace(text, "he's", "he is")) %>%
unnest_tokens(trigram, text, token = "ngrams", n = 3) %>%
separate(trigram, c("word1", "word2","word3"), sep = " ") %>%
filter(word1 == "he", word2 == "is")
udmodel <- udpipe_load_model("english-ud-2.0-170801.udpipe")
include <- udpipe(x = he$word3,
object = udmodel)
include <- include %>%
select(token,upos) %>%
filter(upos == "ADJ") %>%
select(token)
he <- he %>%
filter(word3 %in% include$token)
heCount <- he %>%
count(word1,word2,word3) %>%
arrange(desc(n)) %>%
select(word3,n) %>%
mutate(row = rev(row_number()))
```
Just like above, the same steps has been completed for tweets with "she is X".
```{r echo = F}
sheTweets <- read_csv("sheTweets.csv")
she <- sheTweets %>%
select(screen_name, text) %>%
mutate(text = tolower(text)) %>%
mutate(text = str_replace(text, "she's", "she is")) %>%
unnest_tokens(trigram, text, token = "ngrams", n = 3) %>%
separate(trigram, c("word1", "word2","word3"), sep = " ") %>%
filter(word1 == "she", word2 == "is")
udmodel <- udpipe_load_model("english-ud-2.0-170801.udpipe")
include <- udpipe(x = she$word3,
object = udmodel)
include <- include %>%
select(token,upos) %>%
filter(upos == "ADJ") %>%
select(token)
she <- she %>%
filter(word3 %in% include$token)
sheCount <- she %>%
count(word1,word2,word3) %>%
arrange(desc(n)) %>%
select(word3,n) %>%
mutate(row = rev(row_number()))
```
We'll again prepare bar charts highlighting the most common words. The code is pretty much redundant, so let's skip that here.
```{r echo = F}
hePlot <- heCount %>%
top_n(20,n) %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = heCount$row,
labels = heCount$word3,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"He is __\"") +
scale_fill_gradient(low=brewer.pal(9,"Blues")[2],high=brewer.pal(9,"Blues")[9])
shePlot <- sheCount %>%
top_n(20,n) %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = sheCount$row,
labels = sheCount$word3,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"She is __\"") +
scale_fill_gradient(low=brewer.pal(9,"Reds")[2],high=brewer.pal(9,"Reds")[9])
grid.arrange(hePlot,shePlot,ncol=2)
```
The first question on my mind is *where did he go? Without looking at some tweets, I can't figure out why 'gone' should top the list for tweets about men. Conversely, it is perhaps not too surprising that tweets about women tend to focus on looks.
## X is what s/he is!
Finally, let's objectify both genders and find the most common quadrograms(?) starting with "s/he is a(n) .."
We'll limit results to nouns this time.
Since the code is largely redundant, let's just see what we get!
```{r echo = F}
he <- heTweets %>%
select(screen_name, text) %>%
mutate(text = tolower(text)) %>%
mutate(text = str_replace(text, "he's", "he is")) %>%
unnest_tokens(quadrogram, text, token = "ngrams", n = 4) %>%
separate(quadrogram, c("word1", "word2","word3", "word4"), sep = " ") %>%
filter(word1 == "he", word2 == "is", word3 == "a" | word3 == "an")
include <- udpipe(x = he$word4,
object = udmodel)
include <- include %>%
select(token,upos) %>%
filter(upos =="NOUN") %>%
select(token)
he <- he %>%
filter(word4 %in% include$token)
heCount <- he %>%
count(word1,word2,word3,word4) %>%
arrange(desc(n)) %>%
select(word4,n) %>%
mutate(row = rev(row_number()))
hePLot2 <- heCount %>%
top_n(20,n) %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = heCount$row,
labels = heCount$word4,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"He is a(n) __\"") +
scale_fill_gradient(low=brewer.pal(9,"Blues")[2],high=brewer.pal(9,"Blues")[9])
she <- sheTweets %>%
select(screen_name, text) %>%
mutate(text = tolower(text)) %>%
mutate(text = str_replace(text, "she's", "she is")) %>%
unnest_tokens(quadrogram, text, token = "ngrams", n = 4) %>%
separate(quadrogram, c("word1", "word2","word3", "word4"), sep = " ") %>%
filter(word1 == "she", word2 == "is", word3 == "a" | word3 == "an")
include <- udpipe(x = she$word4,
object = udmodel)
include <- include %>%
select(token,upos) %>%
filter(upos =="NOUN") %>%
select(token)
she <- she %>%
filter(word4 %in% include$token)
sheCount <- she %>%
count(word1,word2,word3,word4) %>%
arrange(desc(n)) %>%
select(word4,n) %>%
mutate(row = rev(row_number()))
shePlot2 <- sheCount %>%
top_n(20,n) %>%
ggplot(aes(row, n, fill = n)) +
geom_col(show.legend = FALSE,width = .9) +
coord_flip() +
scale_x_continuous(
breaks = sheCount$row,
labels = sheCount$word4,
expand = c(0,0)) +
theme_minimal() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()) +
ggtitle("\"She is a(n) __\"") +
scale_fill_gradient(low=brewer.pal(9,"Reds")[2],high=brewer.pal(9,"Reds")[9])
grid.arrange(hePLot2,shePlot2,ncol=2)
```
The words describing men are somewhat more negative than those describing women in tweets. However, I bet that, in most cases, the words 'traitor', 'idiot', 'baby', 'disgrace', 'racist' and 'coward' are used in reference to Donald Trump.
## Conclusion
It is important to keep in mind that the analyses presented here are based on tweets created during a fairly short time window (approx. 20 hours). It would therefore be interesting to compare the resulting word frequencies with those from a set of different tweets.
Nevertheless, we saw clear differences in how men and women were characterised in the downloaded tweets. It would of course be very useful to dig into the contexts in which men and women are mentioned. However, my main goal here was to explore what can be done with simple word frequency and n-gram analyses of specific linguistic constructions.