-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincivility_networks_scripts.R
More file actions
243 lines (186 loc) · 9.5 KB
/
incivility_networks_scripts.R
File metadata and controls
243 lines (186 loc) · 9.5 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
# Required Libraries #
library(tidyverse)
require(lubridate)
library(igraph)
library(ineq)
require(tidytext)
require(viridis)
library(network)
library(ggraph)
library(ggrepel)
### MP HANDLES ## - from other R script - so we can match them to the tweet mentions
mp_df <- read.csv("data/combined_MP_twitter_handles.csv", stringsAsFactors = F)
# summary(mp_df)
# Function to calculate the structural properties on the giant component of the RT network #
network_property_calc <- function(network){
largest_component <- components(network)$membership
largest_component_id <- which.max(table(largest_component))
network <- induced_subgraph(network, which(largest_component == largest_component_id))
degree_centrality <- degree(network, mode = "all")
closeness_centrality <- closeness(network, normalized = T)
betweenness_centrality <- betweenness(network, normalized = T) # by the n of vertex pairs
eigenvector_centrality <- eigen_centrality(network)$vector
Density <- edge_density(network)
Assortativity <- assortativity_degree(network)
Clustering_coeff <- transitivity(network, type = "localaverage")
Clustering_coeff_gini <- Gini(transitivity(network, type = "localaverage"))
Transitivity_global <- transitivity(network, type = "global")
Geodesic_mean <- mean_distance(network)
Geodesic_gini <- Gini(distances(network))
Geodesic_max <- diameter(network)
# Network-level metrics
Density <- edge_density(network, loops = FALSE)
Assortativity <- suppressWarnings(assortativity_degree(network))
Clustering_coeff <- suppressWarnings(transitivity(network, type = "localaverage"))
Clustering_coeff_gini <- suppressWarnings(Gini(transitivity(network, type = "local", isolates = "zero")))
Transitivity_global <- transitivity(network, type = "global")
Geodesic_mean <- suppressWarnings(mean_distance(network, directed = TRUE))
Geodesic_gini <- suppressWarnings(Gini(distances(network)))
Geodesic_max <- suppressWarnings(diameter(network, directed = TRUE))
# Output dataframe
result <- data.frame(
n_nodes = gorder(network),
n_edges = gsize(network),
Density,
avg_degree = mean(degree(network, mode = "all")),
avg_in_degree = mean(degree(network, mode = "in")),
avg_out_degree = mean(degree(network, mode = "out")),
Gini_Degree = ineq::Gini(degree_centrality),
Closeness = mean(closeness_centrality, na.rm = T),
Betweenness = mean(betweenness_centrality),
Eigenvector = mean(eigenvector_centrality),
Gini_Closeness = ineq::Gini(closeness_centrality),
Gini_Betweenness = ineq::Gini(betweenness_centrality),
Gini_Eigenvector = ineq::Gini(eigenvector_centrality),
Assortativity,
Clustering_coeff,
Clustering_coeff_gini,
Transitivity_global,
Geodesic_mean,
Geodesic_gini,
Geodesic_max
)
return(result)
}
## Creates RT networks from tweets mentions ##
retweet_networks <- function(data, month_name){
# Extracting the twitter mentions ##
Rtweets_data <- data %>%
mutate(mentions = str_extract_all(text, "@\\w+"),
text = str_replace_all(text, "[[:punct:]]", "")) %>%
unnest(mentions) %>%
select(-text) %>% # remove text as we don't use that variable
mutate(mentions = str_replace_all(mentions, "@", ""),
mentions_lc = str_to_lower(mentions),
politician = if_else(str_to_lower(mentions) %in% str_to_lower(mp_df$handles), 1, 0),
across(where(is.character), as.factor))
# We extract the mentions per tweet and the edge is the tweet itself but an edge attribute is
# the n of rtweets (size of edge) and label/civility (color)
# create user attribs
user_attribs <- data %>%
mutate(screen_name = str_replace_all(screen_name, "[^[:ascii:]]", ""),
screen_name = as.character(screen_name)) %>%
group_by(screen_name) %>%
summarise(
friendships = mean(friendships,na.rm = TRUE), #avg as they may have tweeted multiple times
uncivility_prop = sum(classification =="uncivil")/ n(), # calculates the proportion of uncivil out of all
rtweets = mean(rtweets,na.rm = TRUE),
politician = if_else(str_to_lower(screen_name) %in% str_to_lower(mp_df$handles), 1, 0),
.groups = 'drop'
) %>%
select(screen_name,uncivility_prop, friendships, rtweets,politician) %>%
ungroup() %>%
unique
# create the edge attribs for our tweeting users
edge_attributes <- Rtweets_data %>%
group_by(screen_name,mentions_lc) %>%
summarise(
mean_RTs = mean(rtweets, na.rm = TRUE),
classification = first(classification), # whether civil/uncivil
.groups = 'drop'
) %>%
mutate(screen_name = as.character(screen_name))%>%
rename(from = screen_name, to = mentions_lc)
# subset data for ONLY uncivil tweets
Rtweets_data_uncivil <- Rtweets_data %>%
filter(classification =="uncivil")
# initial edgelist
graph_edgelist <- Rtweets_data_uncivil %>%
select(from = screen_name, to = mentions_lc) %>%
mutate(across(where(is.factor), as.character))
# create the network from the data
month_network <- graph_from_data_frame(graph_edgelist, directed =TRUE)
#removes the loop and/or multiple edges from a graph
# month_network <- simplify(month_network)
# check which sources users are in the network
source_users <- intersect(user_attribs$screen_name, graph_edgelist[[1]])
# check which target users are in the network
target_users <- intersect(str_to_lower(user_attribs$screen_name), graph_edgelist[[2]])
# subsetting user attribs to actual nodes in network
user_attribs_filter <- user_attribs %>%
filter(screen_name %in% source_users | str_to_lower(screen_name) %in% target_users)
# assigning user attribs
labels1 <- setNames(user_attribs_filter$friendships, user_attribs_filter$screen_name)
labels2 <- setNames(user_attribs_filter$uncivility_prop, user_attribs_filter$screen_name)
labels3 <- setNames(user_attribs_filter$politician, user_attribs_filter$screen_name)
# Match and assign the attribute to node
V(month_network)$popularity <- labels1[match(V(month_network)$name, names(labels1))]
V(month_network)$uncivility_prop <- labels2[match(V(month_network)$name, names(labels2))]
V(month_network)$politician <- labels3[match(V(month_network)$name, names(labels3))]
V(month_network)$label <- ifelse(V(month_network)$politician == 1, V(month_network)$name, NA)
graph_edges_df <- as_data_frame(month_network, what = "edges")
#remove the dupli edges
edge_attributes_filter <- graph_edges_df %>%
left_join(edge_attributes, by = c("from", "to"))
# assign edge attribs
E(month_network)$RTs <- edge_attributes_filter$mean_RTs
E(month_network)$type <- edge_attributes_filter$classification
E(month_network)$type <- as.character(E(month_network)$type)
## network property calculations
property_summary <- network_property_calc(month_network)
property_summary <- property_summary %>%
mutate(month = month_name,
across(where(is.numeric), \(x) round(x, 3)))
# export network properties
write.csv(property_summary, paste0("uncivil_RT_structural_properties_", month_name, ".csv"), row.names = FALSE)
# data saving - convert into a save-able object
graph_edgelist <- as_edgelist(month_network)
# change col names
graph_edgelist %>%
as.data.frame() %>%
select("source" = "V1", "target"="V2") %>%
write.csv(paste0("uncivil_RT_edgelist_", month_name, ".csv"), row.names = FALSE)
#save csv
write_graph(month_network, file = paste0("uncivil_RT_graph_", month_name, ".graphml"), format = "graphml")
## creating a df of the node attribs (as it doesn't get saved otherwise)
vertex_attributes <- data.frame(
id = V(month_network)$name,
popularity = V(month_network)$popularity,
uncivility_prop = V(month_network)$uncivility_prop,
politician = V(month_network)$politician
)
# Exporting to CSV
write.csv(vertex_attributes, paste0("uncivil_RT_node_attributes", month_name, ".csv"), row.names = FALSE)
}
## Let's make some graphs
retweet_networks(tweets_jan,"January")
retweet_networks(tweets_feb,"February")
retweet_networks(tweets_march,"March")
retweet_networks(tweets_april,"April")
retweet_networks(tweets_may,"May")
retweet_networks(tweets_june,"June")
## Importing monthly network properties
tweets_jan_properties <- read.csv("outputs/RT networks/uncivil_RT_structural_properties_January.csv", stringsAsFactors = F)
tweets_feb_properties <- read.csv("outputs/RT networks/uncivil_RT_structural_properties_February.csv",header = T, stringsAsFactors = F)
tweets_march_properties <- read.csv("outputs/RT networks/uncivil_RT_structural_properties_March.csv",header = T, stringsAsFactors = F)
tweets_april_properties<- read.csv("outputs/RT networks/uncivil_RT_structural_properties_April.csv",header = T, stringsAsFactors = F)
tweets_may_properties <- read.csv("outputs/RT networks/uncivil_RT_structural_properties_May.csv",header = T, stringsAsFactors = F)
tweets_june_properties <- read.csv("outputs/RT networks/uncivil_RT_structural_properties_June.csv",header = T, stringsAsFactors = F)
# # combining them again
all_tweets_properties <- rbind(tweets_jan_properties,tweets_feb_properties,tweets_march_properties,
tweets_april_properties,tweets_may_properties,tweets_june_properties)
#Latex table of combined properties
properties_latex <- kable(all_tweets_properties, format = "latex", booktabs = TRUE)
properties_latex
# export data into csv
write.csv(all_tweets_properties, "Combined_monthly_RT_network_properties.csv", row.names = FALSE)