-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrumours.R
148 lines (131 loc) · 4.17 KB
/
rumours.R
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
library(igraph)
library(ggplot2)
library(data.table)
source("src/utils.R")
source("src/rumour_base.R")
source("src/rumour_dose.R")
if (Sys.info()["sysname"] == "Darwin") {
setwd("/Users/lorenzobarbiero/Documents/GitHub/conspiracy-networks/")
}
dl2 <- read.csv("data/fb-orgs/L2.csv", col.names = c("from", "to"))
gl2 <- graph_from_data_frame(dl2, directed = FALSE) |> simplify()
V(gl2)$name <- seq_len(vcount(gl2)) # normalize naming
##############
# COPENHAGEN #
##############
# phone calls
dc <- fread(
"data/copenhagen/calls.csv",
col.names = c("time", "from", "to", "duration")
)[duration > 0, .N, keyby = .(a = pmin(from, to), b = pmax(from, to))]
# sms
ds <- fread(
"data/copenhagen/sms.csv",
col.names = c("time", "from", "to")
)[, .N, keyby = .(a = pmin(from, to), b = pmax(from, to))]
# facebook friends
df <- fread("data/copenhagen/fb_friends.csv", col.names = c("a", "b"))
# big merge with custom weighting
# dcph <- merge(dc, merge(ds, df, all = TRUE), all = TRUE)[
# is.na(N.x), N.x := 0][is.na(N.y), N.y := 0][
# N.x + N.y > 0, weight := 1 + log(N.x + N.y)][
# N.x + N.y == 0, weight := 1][
# , c("N.x", "N.y") := NULL]
# without facebook
dcph <- merge(dc, ds, all = TRUE)[
is.na(N.x), N.x := 0][is.na(N.y), N.y := 0][
, .(a, b, weight = 1 + log(N.x + N.y))][
, weight := weight / max(weight)]
gcph <- graph_from_data_frame(dcph, directed = FALSE) |>
simplify() |>
get_lcc()
V(gcph)$name <- seq_len(vcount(gcph)) # normalize naming
is_simple(gcph)
clusters(gcph) # only one cc, good
k <- degree(gcph)
c <- closeness(gcph)
c_sc <- (c - min(c)) / (max(c) - min(c))
n_cols <- 20
V(gcph)$color <- viridis::plasma(n_cols + 1)[1 + floor(n_cols * c_sc)]
plot(
gcph, layout = layout_with_graphopt(gcph),
edge.width = 0.5 * E(gcph)$weight / min(E(gcph)$weight),
vertex.size = 2 * k^0.3, vertex.label = NA, vertex.frame.color = NA
)
# null model
ger <- sample_gnm(n = vcount(gcph), m = ecount(gcph))
res_er <- rumour_base(
graph = ger, n_iters = 100, inf_0 = 1,
p_skep = 0.5, spr_rate = 0.5, rec_rate = 0.5,
seed = FALSE, display = TRUE
)
res_cph <- rumour_dose(
graph = gcph, n_iters = 1e4, inf_0 = 1,
p_skep = 0.15, spr_rate = 0.7, rec_rate = 0.1, thresh = 5,
seed = FALSE, display = TRUE
)
gf <- make_full_graph(n = vcount(gcph))
res_f <- rumour_base(
graph = gf, n_iters = 100, inf_0 = 1,
p_skep = 0.2, spr_rate = 0.8, rec_rate = 0.1,
seed = FALSE, display = TRUE
)
k <- degree(gcph, mode = "out") |> unname()
cls <- c("sus", "rec")
k_sr <- rbindlist(lapply(cls, function(x) {
data.frame(class = x, k = k[res_cph[[x]]])
}))[, class := factor(class, cls)]
ggplot(k_sr, aes(k, fill = class)) +
geom_histogram(
aes(y = after_stat(density)),
binwidth = 1, alpha = 0.5,
position = "identity", boundary = 0
)
# g <- make_graph("Zachary")
g <- gcph
n_sim <- 1000
nodes <- V(g)
sims <- matrix(0, ncol = n_sim, nrow = vcount(g))
for (i in seq_len(n_sim)) {
res <- rumour_base(
graph = g, n_iters = 100, inf_0 = 1,
p_skep = 0.2, spr_rate = 0.8, rec_rate = 0.2,
seed = FALSE, display = FALSE
)
sims[nodes[res$sus], i] <- "s"
sims[nodes[res$inf], i] <- "i"
sims[nodes[res$rec], i] <- "r"
}
sir_freqs <- rbindlist(
apply(
sims, 1,
\(r) list(sus = sum(r == "s"), inf = sum(r == "i"), rec = sum(r == "r"))
)
)[, lapply(.SD, \(x) x / n_sim)]
# colour by frequency of final state = sus during n_sim runs
n_cols <- 20
freqs <- sir_freqs$sus
hist(freqs)
cols <- viridis::plasma(n_cols + 1)[
1 + floor(n_cols * (freqs - min(freqs)) / (max(freqs) - min(freqs)))]
plot(
g, layout = layout_with_graphopt(g),
vertex.color = cols, vertex.size = 2 * degree(g)^0.3,
vertex.label = NA, vertex.frame.color = NA
)
# colour by final state in sample run
res <- rumour_base(
graph = g, n_iters = 100, inf_0 = 1,
p_skep = 0.2, spr_rate = 0.8, rec_rate = 0.5,
seed = FALSE, display = TRUE
)
V(g)$state <- ifelse(res$sus, "sus", ifelse(res$rec, "rec", "inf"))
cols <- rep("darkgoldenrod1", vcount(gcph))
cols[res$inf] <- "firebrick"
cols[res$rec] <- "dodgerblue4"
cols[res$start] <- "black"
plot(
g, layout = layout_with_kk(g),
vertex.color = cols, vertex.size = 2 * sqrt(degree(g)),
vertex.label = NA, vertex.frame.color = NA
)