-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffs.R
86 lines (69 loc) · 2.21 KB
/
diffs.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
suppressPackageStartupMessages({
require(data.table)
require(RSQLite)
})
.debug <- "~/Dropbox/Covid-WHO-vax"
.args <- if (interactive()) sprintf(c(
"%s/inputs/config.sqlite", "%s/outputs/metrics_",
"%s/outputs/diffs.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")
fls <- grep("_\\d+\\.sqlite",
list.files(dirname(.args[2]), basename(.args[2]), full.names = TRUE),
value = TRUE
)
all <- rbindlist(lapply(fls, readDBtable))
#' TODO: currently specifying what's relevant by hand
baselinescns <- scn[strategy == "none", .(
id, nat_imm_dur_days, start_timing, seasonality
)]
responsescns <- scn[strategy != "none"]
responsescns[
baselinescns,
on = setdiff(colnames(baselinescns), "id"),
refid := i.id
]
baselineres <- all[scenarioId %in% unique(baselinescns$id)]
responseres <- all[!(scenarioId %in% unique(baselinescns$id))]
responseres[responsescns, on=.(scenarioId == id), refScnId := refid ]
responseres[
baselineres,
on=.(sampleId, age, simday, outcome, refScnId == scenarioId),
refval := i.value_numeric
]
responseres[order(simday),
rebase := value_numeric - value_numeric[1],
by = .(scenarioId, sampleId, age, outcome)
]
responseres[order(simday),
ref := refval - refval[1],
by = .(scenarioId, sampleId, age, outcome)
]
manip <- rbind(
responseres[!(outcome %in% c("cases_reported","foi","obs0")),.(
scenarioId, sampleId, simday, outcome,
age, ref, rebase
)],
responseres[!(outcome %in% c("cases_reported","foi","obs0")),.(
age = "all", ref = sum(ref), rebase = sum(rebase)
),by=.(scenarioId, sampleId, simday, outcome)]
)
manip[order(simday),
inc := c(rebase[1], diff(rebase)),
by = .(scenarioId, sampleId, age, outcome)
]
manip[,
cum.averted := ref - rebase
]
manip[,
cum.eff := fifelse(ref == rebase, 0, cum.averted/ref)
]
manip$ref <- manip$rebase <- NULL
saveRDS(manip, tail(.args, 1))