-
Notifications
You must be signed in to change notification settings - Fork 272
/
base-usa.r
219 lines (188 loc) · 9 KB
/
base-usa.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
library(rstan)
library(data.table)
library(lubridate)
library(gdata)
library(dplyr)
library(tidyr)
library(EnvStats)
library(scales)
library(stringr)
library(abind)
library(scales)
library(zoo)
library(matrixStats)
library(optparse)
source('usa/code/utils/read-data-usa.r')
source('usa/code/utils/process-covariates.r')
## to get latest data please run the scrape-required-data.r file or uncomment the line below
# system(paste0("Rscript usa/code/scrape-required-data.r"),intern=FALSE)
# 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
#Sys.setenv(DEBUG == "TRUE") # to run in debug mode
if(is.null(cmdoptions$options$debug)) {
DEBUG = Sys.getenv("DEBUG") == "TRUE"
} else {
DEBUG = cmdoptions$options$debug
}
#Sys.setenv(FULL = "TRUE") # to run in full mode
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-usa'
} else {
StanModel = cmdoptions$args[1]
}
args = cmdoptions$args
# if using rstudio change this, 1: pooling method, 2: mobility formula to use, debug and full are either commandline or sys variable
if(length(args) == 0) {
args = c('base-usa',
'~ -1 + averageMobility + I(transit * transit_use) + residential',
'~ 1 + averageMobility',
'~ -1 + I(transit * transit_use)'
)
}
StanModel = args[1]
cat(sprintf("Running:\nStanModel = %s\nFixed effects:%s\nRandom effects regional:%s\nRandom effects state:%s\nDebug: %s\n",
StanModel,args[2],args[3], args[4],DEBUG))
# Read JHU and NYT data
death_data <- read_death_data(source = "jhu", smooth = FALSE)
ny_data <- read_death_data(source = "nyt", smooth = FALSE)
ny_data <- ny_data[ny_data$code=='NY', ]
# NYT and JHU death data is different lengths
max_ny <- max(ny_data$date)
max_jhu <- max(death_data$date)
max_date <- min(max_ny, max_jhu)
death_data <- death_data[!death_data$code %in% c('NY'), ]
death_data <- bind_rows(death_data, ny_data)
death_data <- death_data[which(death_data$date <= max_date),]
# Choose states
states <- unique(death_data$code)
# Read ifr
ifr_by_state <- read_ifr_data()
# Read google mobility
mobility <- read_google_mobility()
# At times google has mobility na for some days in that cae you will need to impute those values
# else code will fail
# read predictions of future days from foursquare
# if you need predictions from foursquare please run file mobility-regression.r in
# the folder usa/code/utils/mobility-reg
google_pred <- read.csv('usa/data/google-mobility-forecast.csv', stringsAsFactors = FALSE)
google_pred$date <- as.Date(google_pred$date, format = '%Y-%m-%d')
google_pred$sub_region_2 <- ""
google_pred$country_region <- "United States"
google_pred$country_region_code <- "US"
colnames(google_pred)[colnames(google_pred) == 'state'] <- 'sub_region_1'
if (max(google_pred$date) > max(mobility$date)){
google_pred <- google_pred[google_pred$date > max(mobility$date),]
# reading mapping of states of csv
un<-unique(mobility$sub_region_1)
states_code = read.csv('usa/data/states.csv', stringsAsFactors = FALSE)
google_pred$code = "!!"
for(i in 1:length(un)){
google_pred$code[google_pred$sub_region_1==un[i]] = states_code$Abbreviation[states_code$State==un[i]]
}
mobility <- rbind(as.data.frame(mobility),as.data.frame(google_pred[,colnames(mobility)]))
}
max_date <- max(mobility$date)
death_data <- death_data[which(death_data$date <= max_date),]
# read interventions
interventions <- readRDS('usa/data/covariates.RDS')
# read interventions lifted date
interventions_lifted <- readRDS('usa/data/covariates_ended.RDS')
# Number of days to forecast
forecast <- 0
# Maximum number of days to simulate
num_days_sim <- (max(death_data$date) - min(death_data$date) + 1 + forecast)[[1]]
formula = as.formula(args[2])
formula_partial_regional = as.formula(args[3])
formula_partial_state = as.formula(args[4])
processed_data <- process_covariates(states = states,
mobility = mobility,
death_data = death_data ,
ifr_by_state = ifr_by_state,
num_days_sim = num_days_sim,
interventions = interventions,
interventions_lifted = interventions_lifted,
formula = formula, formula_partial_regional = formula_partial_regional,
formula_partial_state = formula_partial_state)
stan_data <- processed_data$stan_data
dates <- processed_data$dates
reported_deaths <- processed_data$reported_deaths
reported_cases <- processed_data$reported_cases
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
m <- stan_model(paste0('usa/code/stan-models/',StanModel,'.stan'))
JOBID = Sys.getenv("PBS_JOBID")
if(JOBID == "")
JOBID = as.character(abs(round(rnorm(1) * 1000000)))
print(sprintf("Jobid = %s",JOBID))
if(DEBUG) {
fit = sampling(m,data=stan_data,iter=40,warmup=20,chains=2)
} else if (FULL) {
fit = sampling(m,data=stan_data,iter=1800,warmup=1000,chains=5,thin=1,control = list(adapt_delta = 0.95, max_treedepth = 15))
} else {
fit = sampling(m,data=stan_data,iter=100,warmup=50,chains=4,thin=1,control = list(adapt_delta = 0.95, max_treedepth = 10))
}
covariate_data = list(interventions, mobility)
out <- rstan::extract(fit)
estimated_cases_raw <- out$prediction
estimated_deaths_raw <- out$E_deaths
estimated_deaths_cf <- out$E_deaths0
save(fit, dates, reported_cases, reported_deaths, states,
estimated_cases_raw, estimated_deaths_raw, estimated_deaths_cf,
formula, formula_partial_regional,formula_partial_state, stan_data,covariate_data, JOBID,
file=paste0('usa/results/',StanModel,'-',JOBID,'-stanfit.Rdata'))
# Makes three panel plots and rt plots for data
source("usa/code/plotting/make-plots.r")
make_plots_all(paste0('usa/results/', StanModel, '-', JOBID, '-stanfit.Rdata'),
last_date_data = max(dates[[1]]),
ext = ".pdf")
source("usa/code/plotting/infectiousness-plots.r")
source("usa/code/utils/calculate-infectiousness.r")
out <- rstan::extract(fit)
calculate_infectiousness(states,out,JOBID)
plot_infectiousness_regions(JOBID = JOBID, StanModel= StanModel, ext = ".pdf",
last_date_data = max(dates[[1]]),
individual = TRUE)
## These plots runn only in full mode and require a lot of other dependencies
if (FULL){
source("usa/code/plotting/make-rt-percentage-map.r")
make_rt_map(StanModel = StanModel, JOBID = JOBID, last_date_data = max(dates[[1]]), ext = ".pdf")
# Simlate scenarios
len_forecast <- 56
mobility_increases <- c(0, 20, 40)
mobility_vars = grep("averageMobility|transit|residential",labels(terms(formula)))
mobility_vars_partial_regional = grep("averageMobility|transit|residential",labels(terms(formula_partial_regional)))
mobility_vars_partial_state = grep("averageMobility|transit|residential",labels(terms(formula_partial_state)))
source("usa/code/simulate.r")
simulate_scenarios(JOBID = JOBID, StanModel = StanModel, len_forecast = len_forecast,
mobility_vars=mobility_vars,
simulate_code='usa/code/stan-models/simulate-usa.stan',
mobility_vars_partial_regional=mobility_vars_partial_regional,
mobility_vars_partial_state=mobility_vars_partial_state,
mobility_increases = mobility_increases, ext = ".pdf")
source("usa/code/plotting/make-scenario-plots.r")
make_scenario_comparison_plots_mobility_deaths(JOBID = JOBID, StanModel, len_forecast = len_forecast,
last_date_data = max(dates[[1]]) + len_forecast,
mobility_increases = mobility_increases, ext = '.pdf')
make_scenario_comparison_plots_mobility_cases(JOBID = JOBID, StanModel, len_forecast = len_forecast,
last_date_data = max(dates[[1]]) + len_forecast,
mobility_increases = mobility_increases, ext = '.pdf')
make_scenario_comparison_plots_mobility_rt(JOBID = JOBID, StanModel, len_forecast = len_forecast,
last_date_data = max(dates[[1]]) + len_forecast,
mobility_increase = mobility_increases, individual = TRUE, ext = ".pdf")
source("usa/code/utils/create-output-files.r")
create_output_files(JOBID, max(dates[[1]]))
}