library(qdapRegex)
library(sentimentr)
library(syuzhet)
library(lubridate)
library(tidyverse)
library(tidytext)
library(forcats)
library(tokenizers)
library(widyr)
library(igraph)
library(ggraph)
library(topicmodels)
library(forcats)
library(scales)
library(stringr)
library(extrafont)
library(reldist)
library(grid)
library(gridExtra)
library(boot)
library(ggradar)
library(tidygraph)
# paleta
plots_palette <- c("#ad5d51", "grey55", "#2b559e", "#947240")
# orden comunidades
comunidad_order <- c("GOP", "Independent", "DNC", "Progressives")
# NECESITO SABER LA DISTRIBUCION DE TWEETS POR MIEMBRO DE UNA COMUNIDAD
# PARA SABER SI HAY UNA SITUACIÓN MUY DESEQUILIBRADA
# detectar emojis
# https://stackoverflow.com/questions/43359066/how-can-i-match-emoji-with-an-r-regex
library(remoji)
library(lexicon)
library(rtweet)
# Sys.getenv()
Sys.setenv(JAVA_HOME = "C:\\Program Files\\Java\\jre1.8.0_251")
library(tm)
library(qdap)
# funciones auxiliares
source(here::here("scripts", "funciones_auxiliares.R"))
Carga de datos. Estos datos han sido previamente procesados, sobre todo en lo que se refiere al texto de los tweets. Se han unificado los términos relacionados con el COVID-19, como “corona”, “coronavirus”, “pandemic” “covid2019”, etc.
Se ha recogido también información sobre los seguidores de cuentas para definir 4 comunidades
covid1 <- readRDS(here::here("datos_procesados", "formated_text_df_data", "covid_part1.rds"))
covid2 <- readRDS(here::here("datos_procesados", "formated_text_df_data", "covid_part2.rds"))
covid <- bind_rows(covid1, covid2) %>%
dplyr::distinct(status_id, .keep_all = TRUE) %>%
mutate(comunidad = fct_relevel(comunidad, c("GOP", "Independent", "DNC", "Progressives"))) %>%
mutate(text = str_replace_all(text, "(COVID|covid)19_?19(19|[0-9]{1,2})?s?|(COVID|covid)19pandemic|corona(19)?", "COVID19")) %>%
mutate(text = str_replace_all(text, "covid19covid19", "covid19"))
rm(covid1, covid2)
gc()
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 4539024 242.5 8423615 449.9 4562056 243.7
## Vcells 27816918 212.3 56883221 434.0 48985763 373.8
El número todal de tweets únicos recogidos, sin incluir retweets es de 524.843.
Los usuarios no asociados a nunguna comunidad son los que publican la mayoría de los tweets
covid %>%
count(comunidad) %>%
mutate(f = percent(n / sum(n), accuracy = 0.1))
## # A tibble: 4 x 3
## comunidad n f
## <fct> <int> <chr>
## 1 GOP 33512 6.4%
## 2 Independent 446721 85.1%
## 3 DNC 30694 5.8%
## 4 Progressives 13916 2.7%
Un reducido grupo de usuarios publica la mayor parte de los tweets.
# sentimiento
freq_tweets_usuario <- get_tweet_distribution(covid)
# Curva de lorenz
freq_tweets_usuario %>%
ggplot(aes(x = porc_usuarios_acum, y = porc_tweets_acum)) +
geom_line(aes(color = comunidad), size = 1.3) +
scale_color_manual(values = plots_palette) +
labs(x = "% acumulado de usuarios", y = "% Acumulado de tweets",
title = "Curva de Lorenz para el nº de usarios y el nº de tweets") +
scale_x_continuous(labels = scales::percent) +
scale_y_continuous(labels = scales::percent) +
theme_bw() +
theme(text = element_text(family = "Georgia", color = "grey45"),
legend.title = element_blank())
# gini
gini_comunidades <- freq_tweets_usuario %>%
group_by(comunidad) %>%
summarise(gini = gini(x = tweets, weights = porc_usuarios)) %>%
arrange(desc(gini))
Ahora en GOP, los temas son casiexclusivamente relaciondos con teorías conspirativas: #wwg1wga (donde va uno vamos todos, relacionado con #qanon), o #greatawakening (#también relacionado con #qanon). También hay un hashtag contra el governador del estado de New York, Andrew Cuomo, y otro relacionado con el medicamento hydroxychloroquine.
Tanto en el DNC como en el el movimiento progressista hay hashtags contra Trump, hay unos pocos partidos de campaña a favor del partido demócrata. En el caso del movimiento progresista, el hashtag #votebluenomatterwho podría ser una llamada para los miembros del movimiento progresista más reacios a Votar a Biden.
covid_tfidf_hashtags <- get_tfidf(covid_frequency %>% filter(comunidad!= "Independent"))
plot_tfidf(covid_tfidf_hashtags, cols = 3, colores = c(1, 3, 4)) +
labs(title = "Tópicos característicos de cada comunidad", y = "TF-IDF") +
theme(axis.title.x= element_blank(),
axis.text.y = element_text(size = 15),
legend.title = element_blank())
Utilizaremos el diccionario NRC para analizar los sentimientos que genera este evento en las diferentes comunidades
covid_nrc <- covid_unigram %>%
filter(comunidad != "Independent") %>%
inner_join(get_sentiments("nrc"), by = "word") %>%
filter(!sentiment %in% c("positive", "negative")) %>%
count(comunidad, sentiment, sort = T) %>%
mutate(sentiment = str_to_title(sentiment)) %>%
ungroup() %>%
group_by(comunidad) %>%
mutate(n = rescale(n)) %>%
pivot_wider(names_from = sentiment, values_from = n) %>%
select(comunidad, Joy, Trust, Fear, Surprise, Sadness, Disgust, Anger, Anticipation) %>%
ungroup() %>%
mutate(comunidad = fct_drop(comunidad))
ggradar(covid_nrc, group.colours = plots_palette[c(1, 3, 4)], legend.position = "top",
group.point.size = 4,
plot.title = "Sentimientos expresados hacia el tópico 'COVI19'",
legend.text.size = 11) +
theme(plot.title = element_text(size = 12, family = "Georgia", color = "grey55"),
axis.title = element_text(family = "Georgia"))
La puntuación es similar para todas las comunidades
Realizamos un test de diferencia de medias para comprobar si la mínima diferencia hallada es significativa.
library(boot)
pol_test_dif <- readRDS(here::here("datos_procesados", "formated_polarization_df_data", "polarization_testing", "covid_polarization.rds"))
pol_test_dif[-1]
## $IC
## 2.5% 97.5%
## DNC-GOP 0.03322876 0.04975325
## DNC-PRG -0.08141952 -0.06794638
##
## $resultado
## [1] "Diferencia significativa en medias"
pol_test_dif$data %>%
ggplot(aes(x = sentimiento)) +
geom_histogram(aes(group = comunidad, fill = comunidad), color = "white", alpha = 0.5, binwidth = 0.002, position = "identity") +
scale_fill_manual(values = plots_palette[c(1, 4)]) +
labs(x = "Average polarity score", title = "Replicaciones bootstrap de la media del scoring de polarización")