-
Notifications
You must be signed in to change notification settings - Fork 9
/
calculations.Rmd
182 lines (160 loc) · 6.76 KB
/
calculations.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
---
title: "Untitled"
author: "Srikanth Aravamuthan"
date: "8/4/2020"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(
echo = FALSE,
# cache = TRUE,
message = FALSE,
warning = FALSE,
fig.width = 10
)
library(tidyverse)
library(plotly)
```
```{r}
initial.susceptible <- 1500
initial.infected <- 10
r0 <- 1.5
exogenous.shocks <- "Yes"
frequency.exogenous.shocks.per.day <- 7
new.infections.per.shock <- 10
days.incubation <- 3
time.to.recovery <- 14
per.asymptotics.advancing.to.symptoms <- 0.3
symptom.case.fatality.ratio <- 0.0005
frequency.screening <- "Weekly"
test.sensitivity <- 0.8
test.cost <- 25
test.specificity <- 0.98
time.to.return.fps.from.isolation <- 3
confirmatory.test.cost <- 100
```
```{r}
num.exogenous.shocks <- case_when(
exogenous.shocks == "Yes" ~ 1,
exogenous.shocks == "No" ~ 0
)
cycles.per.day <- 3
frequency.exogenous.shocks <- cycles.per.day*frequency.exogenous.shocks.per.day
cycles.per.test <- case_when(
frequency.screening == "Daily" ~ 1*cycles.per.day,
frequency.screening == "Every 2 Days" ~ 2*cycles.per.day,
frequency.screening == "Every 3 Days" ~ 3*cycles.per.day,
frequency.screening == "Weekly" ~ 7*cycles.per.day,
frequency.screening == "Every 2 Weeks" ~ 14*cycles.per.day,
frequency.screening == "Every 3 Weeks" ~ 21*cycles.per.day,
frequency.screening == "Every 4 Weeks" ~ 28*cycles.per.day,
frequency.screening == "Symptoms Only" ~ 99999999999
)
rho <- 1/(time.to.recovery*cycles.per.day)
sigma <- rho*(per.asymptotics.advancing.to.symptoms/(1-per.asymptotics.advancing.to.symptoms))
beta <- r0*(rho+sigma)
delta <- (symptom.case.fatality.ratio/(1-symptom.case.fatality.ratio))*rho
theta <- 1/(days.incubation*cycles.per.day)
mu <- 1/(cycles.per.day*time.to.return.fps.from.isolation)
```
```{r}
n.cycle <- 240
mat <- matrix(c(0,initial.susceptible,0,0,initial.infected,0,0,0,0), nrow = 1)
mat <- rbind(mat,
c(1,
max(0,mat[1,2]*(1-beta*(mat[1,5]/(mat[1,2]+mat[1,5]+mat[1,4])))+mat[1,3]*mu),
max(0,mat[1,3]*(1-mu)),
max(0,mat[1,4]*(1-theta)+ beta*(mat[1,2]*mat[1,5]/(mat[1,2]+mat[1,5]+mat[1,4]))),
max(0,mat[1,5]*(1-sigma-rho)+mat[1,4]*theta),
max(0,mat[1,6]*(1-delta-rho)+(mat[1,5]+mat[1,7])*sigma),
0,
max(0,mat[1,8]+(mat[1,5]+mat[1,6]+mat[1,7])*rho),
max(0,delta*mat[1,6]+mat[1,9]))
)
superspreader.event <- 0
superspreader.event <- c(superspreader.event,
(1:n.cycle %% frequency.exogenous.shocks == 0)*num.exogenous.shocks)
for(i in 2:n.cycle) {
mat <- rbind(mat,
c(i,
max(0,mat[i,2]*(1-beta*(mat[i,5]/(mat[i,2]+mat[i,5]+mat[i,4])))+mat[i,3]*mu-mat[i-1,2]*(1-test.specificity)/cycles.per.test-superspreader.event[i+1]*new.infections.per.shock),
max(0,mat[i,3]*(1-mu)+mat[i-1,2]*(1-test.specificity)/cycles.per.test),
max(0,mat[i,4]*(1-theta)+beta*(mat[i,2]*mat[i,5]/(mat[i,2]+mat[i,5]+mat[i,4]))+superspreader.event[i+1]*new.infections.per.shock),
max(0,mat[i,5]*(1-sigma-rho)+mat[i,4]*theta-mat[i-1,5]*test.sensitivity/cycles.per.test),
max(0,mat[i,6]*(1-delta-rho)+(mat[i,5]+mat[i,7])*sigma),
max(0,mat[i,7]*(1-sigma-rho)+mat[i-1,5]*test.sensitivity/cycles.per.test),
max(0,mat[i,8]+(mat[i,5]+mat[i,6]+mat[i,7])*rho),
max(0,delta*mat[i,6]+mat[i,9]))
)
}
mat <- cbind(mat, superspreader.event)
```
```{r}
names.df <- c("Cycle","Susceptible","FP","Exposed","Asympt","Symptoms","TP","Recovered","Dead","Superspreader Event")
df <-
mat %>%
as_tibble() %>%
rename_all(~names.df) %>%
mutate(`Persons Tested` = (lag(Susceptible,1,NA)+lag(Exposed,1,NA)+lag(Asympt,1,NA))/cycles.per.test,
`Total TPs` = lag(Asympt,2,NA)*test.sensitivity/cycles.per.test,
`Total FPs` = lag(Susceptible,2,NA)*(1-test.specificity)/cycles.per.test,
`Total TNs` = lag(Susceptible,2,NA)*test.specificity/cycles.per.test,
`Total FNs` = lag(Exposed,2,NA)+lag(Asympt,2,NA)*(1-test.sensitivity)/cycles.per.test) %>%
mutate(Day = Cycle/cycles.per.day,
`True Positive` = TP,
Symptoms = Symptoms,
`False Positive` = FP,
Total = TP+Symptoms+FP) %>%
mutate(`New Infections` = lag(Asympt,1,NA)*beta*lag(Susceptible,1,NA)/(lag(Susceptible,1,NA)+lag(Exposed,1,NA)+lag(Asympt,1,NA)),
`New Infections` = ifelse(Cycle>1,
`New Infections`+pmin(`Superspreader Event`*new.infections.per.shock,lag(Susceptible,1,NA)),
`New Infections`),
`New Infections` = ifelse(is.na(`New Infections`),0,`New Infections`),
`Cumulative Infections` = cumsum(`New Infections`),
`%Cumulative Infections` = `Cumulative Infections`/initial.susceptible)
```
```{r}
sum.stat <-
df %>%
slice(2:n()) %>%
summarize(`Total Persons Tested in 80 days` = sum(`Persons Tested`, na.rm = TRUE),
`Total Confirmatory Tests Performed` = sum(`Total TPs`, na.rm = TRUE) + sum(`Total FPs`, na.rm = TRUE),
`Average Isolation Unit Census` = mean(`Total`, na.rm = TRUE),
`Average %TP in Isolation` = 1-(mean(`False Positive`, na.rm = TRUE)/mean(`Total`, na.rm = TRUE)),
`Total testing cost` = `Total Persons Tested in 80 days`*test.cost+`Total Confirmatory Tests Performed`*confirmatory.test.cost,
`Total Infections` = last(`Cumulative Infections`))
```
```{r}
df %>%
select(Day, `True Positive`, Symptoms, `False Positive`) %>%
pivot_longer(`True Positive`:`False Positive`, names_to = "Group", values_to = "Value") %>%
mutate(Group = as.factor(Group),
Group = forcats::fct_relevel(Group, levels = c("True Positive", "Symptoms", "False Positive"))) %>%
group_by(Day) %>%
arrange(Group) %>%
mutate(`New Students` = sum(Value),
Students = cumsum(Value)) %>%
plot_ly(x = ~Day,
y = ~Students,
color = ~Group,
colors = "RdYlBu",
alpha = 0.7,
type = "scatter",
mode = "lines",
fill = 'tonexty',
text = ~paste0("</br>", Group,": ", round(Value,3),
"</br>Students: ", round(`New Students`,3),
"</br>", Group," (Percentage of Students): ",
"</br>", scales::percent(Value/`New Students`, accuracy = 0.1)),
hoverinfo = "text") %>%
layout(title = "Isolation Unit Occupancy") %>%
layout(yaxis = list(title = "Number of Students")) %>%
layout(autosize = TRUE,
margin = list(l = 75,
r = 75,
b = 75,
t = 75,
pad = 10)) %>%
config(displaylogo = FALSE)
```