-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseline.R
58 lines (47 loc) · 1.56 KB
/
baseline.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
suppressPackageStartupMessages({
require(data.table)
require(RSQLite)
})
.debug <- "~/Dropbox/Covid-WHO-vax"
.args <- if (interactive()) sprintf(c(
"%s/inputs/config.sqlite", "%s/outputs/low/metrics_",
"%s/outputs/low/baseline.rds"
), .debug) else commandArgs(trailingOnly = TRUE)
if (!interactive()) warning(sprintf("invoked with args %s", paste(.args, collapse = " ")))
readDBtable <- function(fl, tbl = "metrics", drv = RSQLite::SQLite(), flags = SQLITE_RO) {
conn <- dbConnect(drv, fl, flags = flags)
res <- data.table(dbReadTable(conn, tbl))
dbDisconnect(conn)
res
}
scn <- readDBtable(.args[1], tbl = "scenario")
#' TODO: currently specifying what's relevant by hand
baselinescns <- scn[strategy == "none", .(
id, nat_imm_dur_days, start_timing, seasonality
)]
keepids <- baselinescns[,
sprintf("_(%s)\\.sqlite", paste(sprintf("%03g",id), collapse = "|"))
]
fls <- grep(keepids,
list.files(dirname(.args[2]), basename(.args[2]), full.names = TRUE),
value = TRUE
)
all <- rbindlist(lapply(fls, readDBtable))
all[order(simday),
rebase := value_numeric - value_numeric[1],
by = .(scenarioId, sampleId, age, outcome)
]
manip <- rbind(
all[!(outcome %in% c("cases_reported","foi","obs0")),.(
scenarioId, sampleId, simday, outcome,
age, rebase
)],
all[!(outcome %in% c("cases_reported","foi","obs0")),.(
age = "all", rebase = sum(rebase)
),by=.(scenarioId, sampleId, simday, outcome)]
)
manip[order(simday),
inc := c(rebase[1], diff(rebase)),
by = .(scenarioId, sampleId, age, outcome)
]
saveRDS(manip, tail(.args, 1))