forked from ImperialCollegeLondon/covid19model
-
Notifications
You must be signed in to change notification settings - Fork 2
/
base_general.r
294 lines (251 loc) · 11.5 KB
/
base_general.r
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
library(rstan)
library(data.table)
library(lubridate)
library(gdata)
library(dplyr)
library(tidyr)
library(EnvStats)
library(optparse)
countries <- c(
"Denmark",
"Italy",
"Germany",
"Spain",
"United_Kingdom",
"France",
"Norway",
"Belgium",
"Austria",
"Sweden",
"Switzerland",
"Greece",
"Portugal",
"Netherlands"
)
# Commandline options and parsing
parser <- OptionParser()
parser <- add_option(parser, c("-D", "--debug"), action="store_true",
help="Perform a debug run of the model")
parser <- add_option(parser, c("-F", "--full"), action="store_true",
help="Perform a full run of the model")
cmdoptions <- parse_args(parser, args = commandArgs(trailingOnly = TRUE), positional_arguments = TRUE)
# Default run parameters for the model
if(is.null(cmdoptions$options$debug)) {
DEBUG = Sys.getenv("DEBUG") == "TRUE"
} else {
DEBUG = cmdoptions$options$debug
}
if(is.null(cmdoptions$options$full)) {
FULL = Sys.getenv("FULL") == "TRUE"
} else {
FULL = cmdoptions$options$full
}
if(DEBUG && FULL) {
stop("Setting both debug and full run modes at once is invalid")
}
if(length(cmdoptions$args) == 0) {
StanModel = 'base'
} else {
StanModel = cmdoptions$args[1]
}
print(sprintf("Running %s",StanModel))
if(DEBUG) {
print("Running in DEBUG mode")
} else if (FULL) {
print("Running in FULL mode")
}
## Reading all data
d=readRDS('data/COVID-19-up-to-date.rds')
## Ensure that output directories exist
dir.create("results/", showWarnings = FALSE, recursive = TRUE)
dir.create("figures/", showWarnings = FALSE, recursive = TRUE)
dir.create("web/", showWarnings = FALSE, recursive = TRUE)
## get IFR and population from same file
ifr.by.country = read.csv("data/popt_ifr.csv")
ifr.by.country$country = as.character(ifr.by.country[,2])
ifr.by.country$country[ifr.by.country$country == "United Kingdom"] = "United_Kingdom"
serial.interval = read.csv("data/serial_interval.csv")
covariates = read.csv('data/interventions.csv', stringsAsFactors = FALSE)
names_covariates = c('Schools + Universities','Self-isolating if ill', 'Public events', 'Lockdown', 'Social distancing encouraged')
covariates <- covariates %>%
filter((Type %in% names_covariates))
covariates <- covariates[,c(1,2,4)]
covariates <- spread(covariates, Type, Date.effective)
names(covariates) <- c('Country','lockdown', 'public_events', 'schools_universities','self_isolating_if_ill', 'social_distancing_encouraged')
covariates <- covariates[c('Country','schools_universities', 'self_isolating_if_ill', 'public_events', 'lockdown', 'social_distancing_encouraged')]
covariates$schools_universities <- as.Date(covariates$schools_universities, format = "%d.%m.%Y")
covariates$lockdown <- as.Date(covariates$lockdown, format = "%d.%m.%Y")
covariates$public_events <- as.Date(covariates$public_events, format = "%d.%m.%Y")
covariates$self_isolating_if_ill <- as.Date(covariates$self_isolating_if_ill, format = "%d.%m.%Y")
covariates$social_distancing_encouraged <- as.Date(covariates$social_distancing_encouraged, format = "%d.%m.%Y")
## using covariates as dates we want
covariates$schools_universities[covariates$schools_universities > covariates$lockdown] <- covariates$lockdown[covariates$schools_universities > covariates$lockdown]
covariates$public_events[covariates$public_events > covariates$lockdown] <- covariates$lockdown[covariates$public_events > covariates$lockdown]
covariates$social_distancing_encouraged[covariates$social_distancing_encouraged > covariates$lockdown] <- covariates$lockdown[covariates$social_distancing_encouraged > covariates$lockdown]
covariates$self_isolating_if_ill[covariates$self_isolating_if_ill > covariates$lockdown] <- covariates$lockdown[covariates$self_isolating_if_ill > covariates$lockdown]
forecast = 0
N2 = 90 # increase if you need more forecast
dates = list()
reported_cases = list()
stan_data = list(M=length(countries),N=NULL,covariate1=NULL,covariate2=NULL,covariate3=NULL,covariate4=NULL,covariate5=NULL,covariate6=NULL,deaths=NULL,f=NULL,
N0=6,cases=NULL,SI=serial.interval$fit[1:N2],
EpidemicStart = NULL, pop = NULL) # N0 = 6 to make it consistent with Rayleigh
deaths_by_country = list()
# various distributions required for modeling
mean1 = 5.1; cv1 = 0.86; # infection to onset
mean2 = 18.8; cv2 = 0.45 # onset to death
x1 = rgammaAlt(1e7,mean1,cv1) # infection-to-onset distribution
x2 = rgammaAlt(1e7,mean2,cv2) # onset-to-death distribution
ecdf.saved = ecdf(x1+x2)
for(Country in countries) {
IFR=ifr.by.country$ifr[ifr.by.country$country == Country]
covariates1 <- covariates[covariates$Country == Country, c(2,3,4,5,6)]
d1_pop = ifr.by.country[ifr.by.country$country==Country,]
d1=d[d$Countries.and.territories==Country,c(1,5,6,7)]
d1$date = as.Date(d1$DateRep,format='%d/%m/%Y')
d1$t = decimal_date(d1$date)
d1=d1[order(d1$t),]
date_min <- dmy('31/12/2019')
if (as.Date(d1$DateRep[1], format='%d/%m/%Y') > as.Date(date_min, format='%d/%m/%Y')){
print(paste(Country,'In padding'))
pad_days <- as.Date(d1$DateRep[1], format='%d/%m/%Y') - date_min
pad_dates <- date_min + days(1:pad_days[[1]]-1)
padded_data <- data.frame("Countries.and.territories" = rep(Country, pad_days),
"DateRep" = format(pad_dates, '%d/%m/%Y'),
"t" = decimal_date(as.Date(pad_dates,format='%d/%m/%Y')),
"date" = as.Date(pad_dates,format='%d/%m/%Y'),
"Cases" = as.integer(rep(0, pad_days)),
"Deaths" = as.integer(rep(0, pad_days)),
stringsAsFactors=F)
d1 <- bind_rows(padded_data, d1)
}
index = which(d1$Cases>0)[1]
index1 = which(cumsum(d1$Deaths)>=10)[1] # also 5
index2 = index1-30
print(sprintf("First non-zero cases is on day %d, and 30 days before 10 deaths is day %d",index,index2))
d1=d1[index2:nrow(d1),]
stan_data$EpidemicStart = c(stan_data$EpidemicStart,index1+1-index2)
stan_data$pop = c(stan_data$pop, d1_pop$popt)
for (ii in 1:ncol(covariates1)) {
covariate = names(covariates1)[ii]
d1[covariate] <- (as.Date(d1$DateRep, format='%d/%m/%Y') >= as.Date(covariates1[1,covariate]))*1 # should this be > or >=?
}
dates[[Country]] = d1$date
# hazard estimation
N = length(d1$Cases)
print(sprintf("%s has %d days of data",Country,N))
forecast = N2 - N
if(forecast < 0) {
print(sprintf("%s: %d", Country, N))
print("ERROR!!!! increasing N2")
N2 = N
forecast = N2 - N
}
# IFR is the overall probability of dying given infection
convolution = function(u) (IFR * ecdf.saved(u))
f = rep(0,N2) # f is the probability of dying on day i given infection
f[1] = (convolution(1.5) - convolution(0))
for(i in 2:N2) {
f[i] = (convolution(i+.5) - convolution(i-.5))
}
reported_cases[[Country]] = as.vector(as.numeric(d1$Cases))
deaths=c(as.vector(as.numeric(d1$Deaths)),rep(-1,forecast))
cases=c(as.vector(as.numeric(d1$Cases)),rep(-1,forecast))
deaths_by_country[[Country]] = as.vector(as.numeric(d1$Deaths))
covariates2 <- as.data.frame(d1[, colnames(covariates1)])
# x=1:(N+forecast)
covariates2[N:(N+forecast),] <- covariates2[N,]
## append data
stan_data$N = c(stan_data$N,N)
# stan_data$x = cbind(stan_data$x,x)
stan_data$covariate1 = cbind(stan_data$covariate1,covariates2[,1])
stan_data$covariate2 = cbind(stan_data$covariate2,covariates2[,2])
stan_data$covariate3 = cbind(stan_data$covariate3,covariates2[,3])
stan_data$covariate4 = cbind(stan_data$covariate4,covariates2[,4])
stan_data$covariate5 = cbind(stan_data$covariate5,covariates2[,4])
stan_data$covariate6 = cbind(stan_data$covariate6,covariates2[,5])
stan_data$f = cbind(stan_data$f,f)
stan_data$deaths = cbind(stan_data$deaths,deaths)
stan_data$cases = cbind(stan_data$cases,cases)
stan_data$N2=N2
stan_data$x=1:N2
if(length(stan_data$N) == 1) {
stan_data$N = as.array(stan_data$N)
}
}
# create the `any intervention` covariate
stan_data$covariate4 = 1*as.data.frame((stan_data$covariate1+
stan_data$covariate2+
stan_data$covariate3+
stan_data$covariate5+
stan_data$covariate6) >= 1)
if(DEBUG) {
for(i in 1:length(countries)) {
write.csv(
data.frame(date=dates[[i]],
`school closure`=stan_data$covariate1[1:stan_data$N[i],i],
`self isolating if ill`=stan_data$covariate2[1:stan_data$N[i],i],
`public events`=stan_data$covariate3[1:stan_data$N[i],i],
`government makes any intervention`=stan_data$covariate4[1:stan_data$N[i],i],
`lockdown`=stan_data$covariate5[1:stan_data$N[i],i],
`social distancing encouraged`=stan_data$covariate6[1:stan_data$N[i],i]),
file=sprintf("results/%s-check-dates.csv",countries[i]),row.names=F)
}
}
# Combine covariates to an array of design matrices X
stan_data$P <- 6
stopifnot(dim(stan_data$covariate1) == c(stan_data$N2, stan_data$M))
stan_data$X <- array(c(stan_data$covariate1,
stan_data$covariate2,
stan_data$covariate3,
unname(as.matrix(stan_data$covariate4)),
stan_data$covariate5,
stan_data$covariate6),
dim = c( stan_data$N2 , stan_data$M , stan_data$P ))
stan_data$X <- aperm(stan_data$X, c(2,1,3))
stopifnot(all(stan_data$covariate1 == t(stan_data$X[,,1])))
stan_data$covariate1 <-
stan_data$covariate2 <-
stan_data$covariate3 <-
stan_data$covariate4 <-
stan_data$covariate5 <-
stan_data$covariate6 <- NULL
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
m = stan_model(paste0('stan-models/',StanModel,'.stan'))
if(DEBUG) {
fit = sampling(m,data=stan_data,iter=40, warmup=20, chains=2, seed = 4711)
} else if (FULL) {
fit = sampling(m,data=stan_data,iter=4000,warmup=2000,chains=4,thin=4,control = list(adapt_delta = 0.95, max_treedepth = 10))
} else {
fit = sampling(m,data=stan_data,iter=200,warmup=100,chains=4,thin=4,control = list(adapt_delta = 0.95, max_treedepth = 10))
}
out = rstan::extract(fit)
prediction = out$prediction
estimated.deaths = out$E_deaths
estimated.deaths.cf = out$E_deaths0
JOBID = Sys.getenv("PBS_JOBID")
if(JOBID == "")
JOBID = as.character(abs(round(rnorm(1) * 1000000)))
print(sprintf("Jobid = %s",JOBID))
save.image(paste0('results/',StanModel,'-',JOBID,'.Rdata'))
save(fit,prediction,dates,reported_cases,deaths_by_country,countries,estimated.deaths,estimated.deaths.cf,out,covariates,file=paste0('results/',StanModel,'-',JOBID,'-stanfit.Rdata'))
library(bayesplot)
filename <- paste0(StanModel,'-',JOBID)
system(paste0("Rscript covariate-size-effects.r ", filename,'-stanfit.Rdata'))
mu = (as.matrix(out$mu))
colnames(mu) = countries
g = (mcmc_intervals(mu,prob = .9))
ggsave(sprintf("results/%s-mu.png",filename),g,width=4,height=6)
tmp = lapply(1:length(countries), function(i) (out$Rt_adj[,stan_data$N[i],i]))
Rt_adj = do.call(cbind,tmp)
colnames(Rt_adj) = countries
g = (mcmc_intervals(Rt_adj,prob = .9))
ggsave(sprintf("results/%s-final-rt.png",filename),g,width=4,height=6)
system(paste0("Rscript plot-3-panel.r ", filename,'-stanfit.Rdata'))
system(paste0("Rscript plot-forecast.r ",filename,'-stanfit.Rdata'))
system(paste0("Rscript make-table.r results/",filename,'-stanfit.Rdata'))
verify_result <- system(paste0("Rscript web-verify-output.r ", filename,'.Rdata'),intern=FALSE)
if(verify_result != 0){
stop("Verification of web output failed!")
}