-
Notifications
You must be signed in to change notification settings - Fork 0
/
240113.Rmd
133 lines (108 loc) · 2.53 KB
/
240113.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
---
title: '240113'
author: "Oliver Cheng"
date: "2024-01-13"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(deSolve)
source("solver.R")
source("model.R")
source("simulation.R")
source("plots.R")
```
## Introduction
```{r}
parameters <- c(N = 100000, beta = 0.47857, sigma = 0.4, gamma = 0.17857)
state <- c(S = 99999, E = 1, I = 0, R = 0)
int <- 0.01
```
### Simulate
```{r}
simulation <- simulate_seir(
initial_value = state,
params = parameters,
start = 0,
end = 200
)
plot_SEIR(simulation, parameters, style="incidence")
```
```{r}
initial <- c("L" = 1, "F" = 0)
Individual <- function(t, s, p) {
with(as.list(c(s, p)), {
dL <- - p[["sigma"]] * s[["L"]]
dF <- p[["sigma"]] * s[["L"]] - p[["gamma"]] * s[["F"]]
list(c(dL, dF))
})
}
times <- seq(0, 50, by = int)
i <- data.frame(ode(y = initial, times = times, func = Individual, parms = parameters))
ggplot(i, aes(x=time)) +
geom_line(aes(y=L, colour="latent")) +
geom_line(aes(y=F, colour="infectious")) +
labs(title = "L and F over time after an infection",
x = "τ",
y = "p",
colour = "state")
```
Calculate the basic (intrinsic) generation time distribution
```{r}
total <- sum(i$F * int)
i$g_basic <- i$F / total
ggplot(i, aes(x=time, y=g_basic)) + geom_line()
```
Now, calculate the forward mean GT distribution.
```{r}
S.approx <- approxfun(simulation$overview$time, simulation$overview$S, yright=0)
F.approx <- approxfun(i$time, i$F, yright=0)
calc.gt.fwd <- function(s) {
numerator <- sum(
sapply(
seq(0, 50, by=0.1),
\(tau) {
tau * F.approx(tau) * S.approx(s+tau)
}
)
)
denominator <- sum(
sapply(
seq(0, 50, by=0.1),
\(tau) {
F.approx(tau) * S.approx(s+tau)
}
)
)
numerator / denominator
}
gt_c <- data.frame(time = seq(1, 200, by=0.1))
gt_c$fwd <- sapply(gt_c$time, \(x) calc.gt.fwd(x))
ggplot(gt_c, aes(x=time, y=fwd)) + geom_line()
```
And backwards mean
```{r}
{r}
Incidence.approx <- approxfun(simulation$overview$time, simulation$overview$incidence, yright=0, yleft=0)
calc.gt.back <- function(s) {
numerator <- sum(
sapply(
seq(0, 50, by=0.1),
\(tau) {
tau * F.approx(tau) * Incidence.approx(s-tau)
}
)
)
denominator <- sum(
sapply(
seq(0, 50, by=0.1),
\(tau) {
F.approx(tau) * Incidence.approx(s-tau)
}
)
)
numerator / denominator
}
gt_c$back <- sapply(gt_c$time, \(x) calc.gt.back(x))
ggplot(gt_c, aes(x=time, y=back)) + geom_line()
```