-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffusion_exercise.qmd
49 lines (41 loc) · 1.24 KB
/
diffusion_exercise.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
---
title: "Diffusion Exercise"
---
Create a diffusion network model which simulates the diffusion of a rumor
throughout the UKfaculty population of size n = 81. Use set.seed(2223).
```{r}
set.seed(2223)
# First, load in the dataset
library(igraph)
data(UKfaculty, package="igraphdata")
n <- igraph::vcount(UKfaculty)
# Turning the network into an edgelist
library(epiworldR)
UKfaculty_vertex_data <- as_data_frame(UKfaculty, what = c("vertices"))
UKfaculty_matrix <- as.matrix(UKfaculty_vertex_data)
UKfaculty_edgelist <- as_edgelist(UKfaculty)
library(netplot)
nplot(UKfaculty)
# Simulate a 2 col numeric matrix and use for data in next function
# Creating the diffusion model
adopt_rumor <- ModelDiffNet(
"Rumor",
prevalence = .05,
prob_adopt = .1,
data = UKfaculty_matrix,
params = c(1, 4)
)
g <- matrix(runif(n ^ 2) < .01, nrow = n)
diag(g) <- FALSE
el <- which(g, arr.ind = TRUE) - 1L
# Simulating a population from smallworld - Use agents_from_edgelist
agents_from_edgelist(adopt_rumor,
source = el[,1],
target = el[,2],
size = n,
directed = TRUE)
# Running the model for 50 steps
run(adopt_rumor, 50)
# Plotting the model
plot(adopt_rumor)
```