-
Notifications
You must be signed in to change notification settings - Fork 0
/
240109.Rmd
234 lines (187 loc) · 5.6 KB
/
240109.Rmd
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
---
title: "SEIR modelling"
output:
html_document: default
pdf_document: default
date: "2024-01-08"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("ggplot2")
library("sdprisk")
source("solver.R")
source("model.R")
source("simulation.R")
source("plots.R")
```
## Modelling COVID-19 using a stochastic differential equation
### Equations
The SEIR model arises from the following assumptions:
$$
\begin{equation}
S(t) + E(t) + I(t) + R(t) = N\quad \forall t \\
S \xrightarrow{\text{exposure rate }\beta} E \xrightarrow{\text{infection rate }\sigma} I \xrightarrow{\text{removal rate }\gamma}R
\end{equation}
$$
Consequently, we can determine the rate of transition and model this using a Poisson distribution.
$$
\begin{align}
S \rightarrow E &\sim \text{Pois}\left( \frac{\beta}{N}S(t)I(t) \right) \\
E \rightarrow I &\sim \text{Pois}\left( \sigma E(t)\right) \\
I \rightarrow R &\sim \text{Pois}\left( \gamma I(t)\right)
\end{align}
$$
#### Estimating parameters for COVID-19
I approximated the values for $\beta$, $\sigma$ and $\gamma$ using the following:
- Latent period of 2.5 days
- Infectious period of 5.6 days
- $R_0$ of 2.68
This resulted in the following parameters: $$
\begin{align}
\beta &= 0.47857 \\
\sigma &= 0.4 \\
\gamma &= 0.17857
\end{align}
$$
### Solving the equations
```{r covid_default_model}
parameters <- c(N = 10000, beta = 0.47857, sigma = 0.4, gamma = 0.17857)
state <- c(S = 9999, E = 1, I = 0, R = 0)
out.default_solution <- simulate_seir(
initial_value = state,
params = parameters,
start = 0,
end = 100
)
plot_SEIR(out.default_solution, parameters, style="incidence")
```
### Generating multiple solutions (and a deterministic one) and comparing
```{r multi-plot}
out.deterministic <- simulate_seir(
initial_value = state,
params = parameters,
start = 0,
end = 100,
stochastic = FALSE
)
out.stochastic <- replicate(100, simulate_seir(
initial_value = state,
params = parameters,
start = 0,
end = 100,
stochastic = TRUE,
simulate = FALSE
), simplify = FALSE)
plot_SEIR_incidences(out.deterministic, out.stochastic, parameters)
```
### Simulating using a real population
```{r}
parameters <- c(N = 100000, beta = 0.47857, sigma = 0.4, gamma = 0.17857)
state <- c(S = 99999, E = 1, I = 0, R = 0)
out <- simulate_seir(
initial_value = state,
params = parameters,
start = 0,
end = 200
)
plot_SEIR(out, parameters, "combined")
plot_SEIR(out, parameters, "exploded")
# the histogram of events should exactly match the line plot
plot_SEIR(out, parameters, "incidence") +
geom_histogram(data=out$events, aes(x=time), binwidth=1, alpha=0.5)
```
#### Sanity check our results to verify things are working properly
```{r}
results <- table(unlist(out$pop$.state))
results
unlist(tail(out$overview, n=1))
```
#### Generation time distribution
```{r}
# for each event, find the time when the original infector got infected
# original infection times
events <- out$events
events$infector_exposure_time <- out$pop[events$infector, ]$E
events$gt <- events$time - events$infector_exposure_time
# normalise the generation time frequencies, to graph a histogram
gt.freq <- data.frame(table(events$gt)) # frequency table
colnames(gt.freq) <- c("time", "freq") # fix column names
gt.freq$time <- as.numeric(gt.freq$time)
gt.freq$norm <- gt.freq$freq / sum(gt.freq$freq)# normalise
# graph the hypoexponential
hypoexp <- data.frame(time = seq(1, 60, by=0.1))
hypoexp$density <- dhypoexp(hypoexp$time, rate=c(parameters[["sigma"]], parameters[["gamma"]]))
# plot
ggplot() +
geom_line(data=gt.freq, aes(x=time, y=norm, colour="simulation")) +
geom_line(data=hypoexp, aes(x=time, y=density, colour="expected")) +
#geom_line(data=out, aes(x=time, y=g, colour="de")) +
labs(
title="generation time frequency",
x="generation time",
y="proportion",
colour="type"
)
```
#### Forwards and backwards generation time
The backwards GI is also known as the "period GI", and the forwards GI is also known as the "cohort GI"
```{r}
gt.back <- do.call(
data.frame,
aggregate(
gt ~ time, data = events, FUN=function(x) {
c(
mean = mean(x),
q1 = quantile(x, 0.25),
q3 = quantile(x, 0.75),
median = median(x),
sd = sd(x),
size = length(x)
)
}
)
)
colnames(gt.back) <- c("time", "mean", "q1", "q3", "median", "sd", "size")
```
And forward generation:
```{r}
gt.fwd <- do.call(
data.frame,
aggregate(
gt ~ infector_exposure_time, data = events, FUN=function(x) {
c(
mean = mean(x),
q1 = quantile(x, 0.25),
q3 = quantile(x, 0.75),
median = median(x),
sd = sd(x),
size = length(x)
)
}
)
)
colnames(gt.fwd) <- c("time", "mean", "q1", "q3", "median", "sd", "size")
```
Graphing the results
```{r}
ggplot(gt.back, aes(x=time, y=mean)) +
geom_line() +
geom_errorbar(aes(ymin=mean - sd, ymax=mean + sd), width=.1,
position=position_dodge()) +
labs(title="mean backwards generation time")
ggplot() +
geom_line(data=gt.back, aes(x=time, y=mean, colour="simulation")) +
geom_line(data=gt_c, aes(x=time, y=back, colour="estimate")) +
labs(title="mean backwards generation time")
```
```{r}
ggplot(gt.fwd, aes(x=time, y=mean)) +
geom_line() +
#geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.1,
# position=position_dodge()) +
labs(title="mean forwards generation time")
ggplot() +
geom_line(data=gt_c, aes(x=time, y=fwd, colour="estimate")) +
geom_line(data=gt.fwd, aes(x=time, y=mean, colour="simulation")) +
labs(title="mean forwards generation time")
```