-
Notifications
You must be signed in to change notification settings - Fork 1
/
part2a.qmd
79 lines (59 loc) · 1.9 KB
/
part2a.qmd
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
---
title: "Part 2a: Social Network Analysis"
---
## Network Diffusion
epiworldR also supports diffusion networks to simulate the spread of ideas or rumors throughout a population. The below example demonstrates this capability.
#### Example
Goal: Create a diffusion network model that simulates a rumor's spread throughout the `UKfaculty` network of size n = 81. We start by loading the corresponding R packages and data.
```{r, include = FALSE}
library(igraph) # For manipulating networks
library(netplot) # For plotting
```
We now load the `UKfaculty` data and visualize the network. The file `part2a.rda` has the network, the vertex attributes as a numeric matrix, and the edge list as a numeric matrix.
```{r}
# Loading the data for the section
load("part2a.rda")
# Visualizing the Network
# library(netplot)
nplot(UKfaculty)
# First few lines of the vertex attributes
head(UKfaculty_vertex)
# First few lines of the edge list
head(UKfaculty_edgelist)
```
To simulate the rumor, we use the `ModelDiffNet` function:
```{r}
# Creating the diffusion model
library(epiworldR)
adopt_rumor <- ModelDiffNet(
name = "The dean is leaving!",
prevalence = 2/vcount(UKfaculty), # Two adopter
prob_adopt = .1,
data = UKfaculty_vertex,
params = c(0, 5)
)
# Reading in the network
agents_from_edgelist(
adopt_rumor,
size = vcount(UKfaculty),
source = UKfaculty_edgelist[,1] - 1L,
target = UKfaculty_edgelist[,2] - 1L,
directed = TRUE
)
# Running the model for 50 steps
run(adopt_rumor, 100);adopt_rumor
# Plotting the model
plot(adopt_rumor)
```
We can extract the adoption network:
```{r}
transmissions <- get_transmissions(adopt_rumor)
# Turning it into a network via igraph
tnet <- graph_from_data_frame(
transmissions[, c(2, 3)] + 1, # To start from 1 (not 0)
directed = TRUE,
vertices = cbind(id = 1:81, UKfaculty_vertex)
)
# Adding back vertex attributes
nplot(tnet)
```