-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.qmd
156 lines (119 loc) · 3.78 KB
/
examples.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
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
---
title: Examples
---
This R package includes several popular epidemiological models, including SIS ([wiki](https://en.wikipedia.org/w/index.php?title=Compartmental_models_in_epidemiology&oldid=1155757336#Variations_on_the_basic_SIR_model)), SIR ([wiki](https://en.wikipedia.org/w/index.php?title=Compartmental_models_in_epidemiology&oldid=1155757336#The_SIR_model)), and SEIR ([wiki](https://en.wikipedia.org/w/index.php?title=Compartmental_models_in_epidemiology&oldid=1155757336#The_SEIR_model)) using either a fully connected graph (similar to a compartmental model) or a user-defined network. Here are some examples:
## SIR Model Using a Random Graph
This Susceptible-Infected-Recovered model features a population of 100,000 agents simulated in a small-world network. Each agent is connected to ten other agents. One percent of the population has the virus, with a 70% chance of transmission. Infected individuals recover at a 0.3 rate:
```{r}
library(epiworldR)
## Creating a SIR model
sir <- ModelSIR(
name = "COVID-19",
prevalence = .01,
transmission_rate = .7,
recovery_rate = .3
) |>
# Adding a Small world population
agents_smallworld(n = 100000, k = 10, d = FALSE, p = .01) |>
# Running the model for 50 days
run(ndays = 50, seed = 1912)
sir
```
Visualizing the outputs
```{r}
plot(sir)
```
## SEIR Model With a Fully Connected Graph
```{r}
model_seirconn <- ModelSEIRCONN(
name = "COVID-19",
prevalence = 0.01,
n = 10000,
contact_rate = 4,
incubation_days = 7,
transmission_rate = 0.6,
recovery_rate = 0.5
)
set.seed(132)
run(model_seirconn, ndays = 100)
model_seirconn
```
Computing some key statistics: plotting and reproductive number ([wiki](https://en.wikipedia.org/w/index.php?title=Basic_reproduction_number&oldid=1155282807))
```{r}
plot(model_seirconn)
repnum <- get_reproductive_number(model_seirconn)
plot(repnum, type = "b")
```
## SIR Logit
```{r}
set.seed(2223)
n <- 100000
X <- cbind(
Intercept = 1,
Female = sample.int(2, n, replace = TRUE) - 1
)
coef_infect <- c(.1, -2, 2)
coef_recover <- rnorm(2)
model_logit <- ModelSIRLogit(
"covid2",
data = X,
coefs_infect = coef_infect,
coefs_recover = coef_recover,
coef_infect_cols = 1L:ncol(X),
coef_recover_cols = 1L:ncol(X),
prob_infection = .8,
recovery_rate = .3,
prevalence = .01
)
agents_smallworld(model_logit, n, 8, FALSE, .01)
run(model_logit, 50)
plot(model_logit)
## Females are supposed to be more likely to become infected
rn <- get_reproductive_number(model_logit)
(table(
X[, "Female"],
(1:n %in% rn$source)
) |> prop.table())[,2]
## Looking into the agents
get_agents(model_logit)
```
## Transmission Network
```{r}
sir <- ModelSIR(
name = "COVID-19",
prevalence = .01,
transmission_rate = .5,
recovery_rate = .5
) |>
# Adding a Small world population
agents_smallworld(n = 500, k = 10, d = FALSE, p = .01) |>
# Running the model for 50 days
run(ndays = 50, seed = 1912)
## Transmission network
net <- get_transmissions(sir)
## Plotting
library(netplot)
library(igraph)
x <- graph_from_edgelist(as.matrix(net[,2:3]) + 1)
nplot(x, edge.curvature = 0, edge.color = "gray", skip.vertex=TRUE)
```
## Multiple Simulations
```{r}
model_sir <- ModelSIRCONN(
name = "COVID-19",
prevalence = 0.01,
n = 1000,
contact_rate = 2,
transmission_rate = 0.9,
recovery_rate = 0.1
)
## Generating a saver
saver <- make_saver("total_hist", "reproductive")
## Running and printing
run_multiple(model_sir, ndays = 100, nsims = 50, saver = saver, nthread = 2)
## Retrieving the results
ans <- run_multiple_get_results(model_sir)
head(ans$total_hist)
head(ans$reproductive)
plot(ans$reproductive)
```