# ===== Run all 4 washout scenarios =====
run_scenario <- function(windows_dates, label) {
# Survival quarter ITS
surv_res <- purrr::pmap_dfr(
as.list(as.data.frame(windows_dates, stringsAsFactors = FALSE)),
function(Update, start_pre, wash_start, wash_end, cut, end_post) {
d <- build_its_quarter(quarterly_data, start_pre, wash_start, wash_end, cut, end_post)
message(sprintf("[SURV-Quarter | %s | %s] site-quarters=%d (pre=%d, post=%d)",
label, Update, nrow(d), sum(d$post == 0), sum(d$post == 1)))
analyze_its_quarter(d) %>% mutate(Update = Update, .before = 1)
}
) %>%
mutate(
Scenario = label,
`Risk Difference (pp)` = round(`Risk Difference (pp)`, 2),
`RD p` = signif(`RD p`, 3),
`Odds Ratio` = round(`Odds Ratio`, 3),
`OR p` = signif(`OR p`, 3)
) %>%
dplyr::select(
Scenario, Update, Metric,
`Risk Difference (pp)`, `RD 95% CI`, `RD p`,
`Odds Ratio`, `OR 95% CI`, `OR p`
) %>%
arrange(Update, match(Metric, c("Immediate change", "Slope difference")))
# Epi monthly ITS
epi_res <- purrr::pmap_dfr(
as.list(as.data.frame(windows_dates, stringsAsFactors = FALSE)),
function(Update, start_pre, wash_start, wash_end, cut, end_post) {
d <- build_its_month(epi_monthly, start_pre, wash_start, wash_end, cut, end_post)
message(sprintf("[EPI-Month | %s | %s] site-months=%d (pre=%d, post=%d)",
label, Update, nrow(d), sum(d$post == 0), sum(d$post == 1)))
analyze_its_epi(d) %>% mutate(Update = Update, .before = 1)
}
) %>%
mutate(
Scenario = label,
Estimate = round(Estimate, 2),
p_value = signif(p_value, 3)
) %>%
dplyr::select(Scenario, Update, Metric, Estimate, `95% CI`, p_value) %>%
arrange(Update, match(Metric, c("Immediate change", "Slope difference")))
list(survival_quarter = surv_res, epi_month = epi_res)
}
scenario_list <- list(
"No washout" = windows_0,
"Washout 6 mo" = windows_6,
"Washout 12 mo" = windows_12,
"Washout 18 mo" = windows_18
)
all_results <- purrr::imap(scenario_list, run_scenario)
[SURV-Quarter | No washout | 2005] site-quarters=884 (pre=241, post=643)
[SURV-Quarter | No washout | 2010] site-quarters=1250 (pre=643, post=607)
[SURV-Quarter | No washout | 2015] site-quarters=1422 (pre=607, post=815)
[SURV-Quarter | No washout | 2020] site-quarters=1308 (pre=815, post=493)
[EPI-Month | No washout | 2005] site-months=862 (pre=184, post=678)
[EPI-Month | No washout | 2010] site-months=1324 (pre=678, post=646)
[EPI-Month | No washout | 2015] site-months=1610 (pre=646, post=964)
[EPI-Month | No washout | 2020] site-months=1589 (pre=964, post=625)
[SURV-Quarter | Washout 6 mo | 2005] site-quarters=780 (pre=186, post=594)
[SURV-Quarter | Washout 6 mo | 2010] site-quarters=1190 (pre=649, post=541)
[SURV-Quarter | Washout 6 mo | 2015] site-quarters=1328 (pre=590, post=738)
[SURV-Quarter | Washout 6 mo | 2020] site-quarters=1297 (pre=804, post=493)
[EPI-Month | Washout 6 mo | 2005] site-months=761 (pre=136, post=625)
[EPI-Month | Washout 6 mo | 2010] site-months=1244 (pre=673, post=571)
[EPI-Month | Washout 6 mo | 2015] site-months=1506 (pre=624, post=882)
[EPI-Month | Washout 6 mo | 2020] site-months=1582 (pre=957, post=625)
[SURV-Quarter | Washout 12 mo | 2005] site-quarters=698 (pre=159, post=539)
[SURV-Quarter | Washout 12 mo | 2010] site-quarters=1109 (pre=621, post=488)
[SURV-Quarter | Washout 12 mo | 2015] site-quarters=1253 (pre=592, post=661)
[SURV-Quarter | Washout 12 mo | 2020] site-quarters=1273 (pre=780, post=493)
[EPI-Month | Washout 12 mo | 2005] site-months=671 (pre=117, post=554)
[EPI-Month | Washout 12 mo | 2010] site-months=1125 (pre=621, post=504)
[EPI-Month | Washout 12 mo | 2015] site-months=1406 (pre=628, post=778)
[EPI-Month | Washout 12 mo | 2020] site-months=1545 (pre=920, post=625)
[SURV-Quarter | Washout 18 mo | 2005] site-quarters=595 (pre=132, post=463)
[SURV-Quarter | Washout 18 mo | 2010] site-quarters=993 (pre=572, post=421)
[SURV-Quarter | Washout 18 mo | 2015] site-quarters=1174 (pre=601, post=573)
[SURV-Quarter | Washout 18 mo | 2020] site-quarters=1252 (pre=759, post=493)
[EPI-Month | Washout 18 mo | 2005] site-months=572 (pre=98, post=474)
[EPI-Month | Washout 18 mo | 2010] site-months=993 (pre=560, post=433)
[EPI-Month | Washout 18 mo | 2015] site-months=1320 (pre=637, post=683)
[EPI-Month | Washout 18 mo | 2020] site-months=1521 (pre=896, post=625)
# ===== Combined survival table across all scenarios =====
survival_all <- bind_rows(lapply(all_results, `[[`, "survival_quarter")) %>%
arrange(
factor(Scenario, levels = c("No washout", "Washout 6 mo", "Washout 12 mo", "Washout 18 mo")),
Update,
match(Metric, c("Immediate change", "Slope difference"))
)
print(survival_all, n = Inf, width = Inf)
# A tibble: 32 × 9
Scenario Update Metric `Risk Difference (pp)` `RD 95% CI`
<chr> <chr> <chr> <dbl> <chr>
1 No washout 2005 Immediate change 9.27 (2.47, 16.06)
2 No washout 2005 Slope difference 0.96 (-0.65, 2.58)
3 No washout 2010 Immediate change 0.72 (-4.50, 5.94)
4 No washout 2010 Slope difference 1.22 (-0.33, 2.76)
5 No washout 2015 Immediate change 5.57 (1.12, 10.01)
6 No washout 2015 Slope difference 0.13 (-1.25, 1.51)
7 No washout 2020 Immediate change -1.5 (-7.00, 3.99)
8 No washout 2020 Slope difference 1 (-2.45, 4.45)
9 Washout 6 mo 2005 Immediate change 10.5 (1.89, 19.04)
10 Washout 6 mo 2005 Slope difference 0.49 (-1.36, 2.34)
11 Washout 6 mo 2010 Immediate change 1.01 (-4.44, 6.45)
12 Washout 6 mo 2010 Slope difference 1.64 (-0.19, 3.48)
13 Washout 6 mo 2015 Immediate change 5.4 (0.73, 10.08)
14 Washout 6 mo 2015 Slope difference 0.39 (-1.20, 1.98)
15 Washout 6 mo 2020 Immediate change -1.41 (-6.92, 4.10)
16 Washout 6 mo 2020 Slope difference 1.16 (-2.29, 4.60)
17 Washout 12 mo 2005 Immediate change 9.01 (-0.25, 18.28)
18 Washout 12 mo 2005 Slope difference 0.8 (-1.29, 2.90)
19 Washout 12 mo 2010 Immediate change 2.19 (-3.54, 7.92)
20 Washout 12 mo 2010 Slope difference 1.27 (-0.87, 3.42)
21 Washout 12 mo 2015 Immediate change 5.38 (0.49, 10.27)
22 Washout 12 mo 2015 Slope difference 1.08 (-0.76, 2.93)
23 Washout 12 mo 2020 Immediate change -1.71 (-7.22, 3.80)
24 Washout 12 mo 2020 Slope difference 1.26 (-2.19, 4.70)
25 Washout 18 mo 2005 Immediate change 10.8 (0.85, 20.77)
26 Washout 18 mo 2005 Slope difference -0.71 (-3.29, 1.87)
27 Washout 18 mo 2010 Immediate change 2.16 (-3.89, 8.21)
28 Washout 18 mo 2010 Slope difference 1.72 (-0.86, 4.30)
29 Washout 18 mo 2015 Immediate change 4.75 (-0.48, 9.98)
30 Washout 18 mo 2015 Slope difference 1.51 (-0.74, 3.75)
31 Washout 18 mo 2020 Immediate change -0.88 (-6.46, 4.70)
32 Washout 18 mo 2020 Slope difference 1.12 (-2.36, 4.59)
`RD p` `Odds Ratio` `OR 95% CI` `OR p`
<dbl> <dbl> <chr> <dbl>
1 0.00752 1.55 (1.17, 2.06) 0.00256
2 0.243 1.04 (0.98, 1.11) 0.181
3 0.786 1.02 (0.84, 1.24) 0.852
4 0.123 1.06 (1.00, 1.12) 0.056
5 0.0142 1.27 (1.07, 1.50) 0.00678
6 0.856 1.01 (0.95, 1.06) 0.834
7 0.592 0.938 (0.76, 1.16) 0.552
8 0.569 1.04 (0.91, 1.19) 0.537
9 0.0167 1.64 (1.13, 2.36) 0.00894
10 0.603 1.03 (0.95, 1.10) 0.491
11 0.717 1.03 (0.84, 1.27) 0.787
12 0.0788 1.08 (1.01, 1.16) 0.0343
13 0.0235 1.26 (1.05, 1.51) 0.0118
14 0.63 1.02 (0.96, 1.08) 0.586
15 0.616 0.941 (0.76, 1.16) 0.575
16 0.511 1.05 (0.92, 1.20) 0.474
17 0.0565 1.51 (1.01, 2.24) 0.043
18 0.453 1.04 (0.96, 1.13) 0.313
19 0.453 1.08 (0.87, 1.34) 0.491
20 0.244 1.06 (0.98, 1.15) 0.145
21 0.0312 1.26 (1.04, 1.52) 0.017
22 0.249 1.05 (0.98, 1.12) 0.204
23 0.543 0.929 (0.75, 1.15) 0.502
24 0.474 1.05 (0.92, 1.21) 0.436
25 0.0333 1.65 (1.07, 2.55) 0.0241
26 0.59 0.977 (0.88, 1.08) 0.662
27 0.484 1.07 (0.84, 1.36) 0.568
28 0.192 1.09 (0.98, 1.20) 0.11
29 0.0748 1.23 (1.00, 1.51) 0.0449
30 0.189 1.07 (0.98, 1.16) 0.143
31 0.758 0.962 (0.78, 1.19) 0.722
32 0.53 1.05 (0.92, 1.20) 0.489
# Optional prettier HTML table
gt::gt(survival_all)
| No washout |
2005 |
Immediate change |
9.27 |
(2.47, 16.06) |
0.00752 |
1.551 |
(1.17, 2.06) |
0.00256 |
| No washout |
2005 |
Slope difference |
0.96 |
(-0.65, 2.58) |
0.24300 |
1.045 |
(0.98, 1.11) |
0.18100 |
| No washout |
2010 |
Immediate change |
0.72 |
(-4.50, 5.94) |
0.78600 |
1.019 |
(0.84, 1.24) |
0.85200 |
| No washout |
2010 |
Slope difference |
1.22 |
(-0.33, 2.76) |
0.12300 |
1.058 |
(1.00, 1.12) |
0.05600 |
| No washout |
2015 |
Immediate change |
5.57 |
(1.12, 10.01) |
0.01420 |
1.266 |
(1.07, 1.50) |
0.00678 |
| No washout |
2015 |
Slope difference |
0.13 |
(-1.25, 1.51) |
0.85600 |
1.006 |
(0.95, 1.06) |
0.83400 |
| No washout |
2020 |
Immediate change |
-1.50 |
(-7.00, 3.99) |
0.59200 |
0.938 |
(0.76, 1.16) |
0.55200 |
| No washout |
2020 |
Slope difference |
1.00 |
(-2.45, 4.45) |
0.56900 |
1.043 |
(0.91, 1.19) |
0.53700 |
| Washout 6 mo |
2005 |
Immediate change |
10.47 |
(1.89, 19.04) |
0.01670 |
1.635 |
(1.13, 2.36) |
0.00894 |
| Washout 6 mo |
2005 |
Slope difference |
0.49 |
(-1.36, 2.34) |
0.60300 |
1.026 |
(0.95, 1.10) |
0.49100 |
| Washout 6 mo |
2010 |
Immediate change |
1.01 |
(-4.44, 6.45) |
0.71700 |
1.029 |
(0.84, 1.27) |
0.78700 |
| Washout 6 mo |
2010 |
Slope difference |
1.64 |
(-0.19, 3.48) |
0.07880 |
1.078 |
(1.01, 1.16) |
0.03430 |
| Washout 6 mo |
2015 |
Immediate change |
5.40 |
(0.73, 10.08) |
0.02350 |
1.260 |
(1.05, 1.51) |
0.01180 |
| Washout 6 mo |
2015 |
Slope difference |
0.39 |
(-1.20, 1.98) |
0.63000 |
1.017 |
(0.96, 1.08) |
0.58600 |
| Washout 6 mo |
2020 |
Immediate change |
-1.41 |
(-6.92, 4.10) |
0.61600 |
0.941 |
(0.76, 1.16) |
0.57500 |
| Washout 6 mo |
2020 |
Slope difference |
1.16 |
(-2.29, 4.60) |
0.51100 |
1.050 |
(0.92, 1.20) |
0.47400 |
| Washout 12 mo |
2005 |
Immediate change |
9.01 |
(-0.25, 18.28) |
0.05650 |
1.508 |
(1.01, 2.24) |
0.04300 |
| Washout 12 mo |
2005 |
Slope difference |
0.80 |
(-1.29, 2.90) |
0.45300 |
1.044 |
(0.96, 1.13) |
0.31300 |
| Washout 12 mo |
2010 |
Immediate change |
2.19 |
(-3.54, 7.92) |
0.45300 |
1.080 |
(0.87, 1.34) |
0.49100 |
| Washout 12 mo |
2010 |
Slope difference |
1.27 |
(-0.87, 3.42) |
0.24400 |
1.062 |
(0.98, 1.15) |
0.14500 |
| Washout 12 mo |
2015 |
Immediate change |
5.38 |
(0.49, 10.27) |
0.03120 |
1.260 |
(1.04, 1.52) |
0.01700 |
| Washout 12 mo |
2015 |
Slope difference |
1.08 |
(-0.76, 2.93) |
0.24900 |
1.047 |
(0.98, 1.12) |
0.20400 |
| Washout 12 mo |
2020 |
Immediate change |
-1.71 |
(-7.22, 3.80) |
0.54300 |
0.929 |
(0.75, 1.15) |
0.50200 |
| Washout 12 mo |
2020 |
Slope difference |
1.26 |
(-2.19, 4.70) |
0.47400 |
1.054 |
(0.92, 1.21) |
0.43600 |
| Washout 18 mo |
2005 |
Immediate change |
10.81 |
(0.85, 20.77) |
0.03330 |
1.652 |
(1.07, 2.55) |
0.02410 |
| Washout 18 mo |
2005 |
Slope difference |
-0.71 |
(-3.29, 1.87) |
0.59000 |
0.977 |
(0.88, 1.08) |
0.66200 |
| Washout 18 mo |
2010 |
Immediate change |
2.16 |
(-3.89, 8.21) |
0.48400 |
1.072 |
(0.84, 1.36) |
0.56800 |
| Washout 18 mo |
2010 |
Slope difference |
1.72 |
(-0.86, 4.30) |
0.19200 |
1.086 |
(0.98, 1.20) |
0.11000 |
| Washout 18 mo |
2015 |
Immediate change |
4.75 |
(-0.48, 9.98) |
0.07480 |
1.230 |
(1.00, 1.51) |
0.04490 |
| Washout 18 mo |
2015 |
Slope difference |
1.51 |
(-0.74, 3.75) |
0.18900 |
1.067 |
(0.98, 1.16) |
0.14300 |
| Washout 18 mo |
2020 |
Immediate change |
-0.88 |
(-6.46, 4.70) |
0.75800 |
0.962 |
(0.78, 1.19) |
0.72200 |
| Washout 18 mo |
2020 |
Slope difference |
1.12 |
(-2.36, 4.59) |
0.53000 |
1.048 |
(0.92, 1.20) |
0.48900 |
# ===== Combined epi table across all scenarios =====
epi_all <- bind_rows(lapply(all_results, `[[`, "epi_month")) %>%
arrange(
factor(Scenario, levels = c("No washout", "Washout 6 mo", "Washout 12 mo", "Washout 18 mo")),
Update,
match(Metric, c("Immediate change", "Slope difference"))
)
print(epi_all, n = Inf, width = Inf)
# A tibble: 32 × 6
Scenario Update Metric Estimate `95% CI` p_value
<chr> <chr> <chr> <dbl> <chr> <dbl>
1 No washout 2005 Immediate change -0.67 (-1.50, 0.17) 0.116
2 No washout 2005 Slope difference -0.11 (-0.31, 0.10) 0.306
3 No washout 2010 Immediate change -0.13 (-0.69, 0.43) 0.653
4 No washout 2010 Slope difference -0.14 (-0.32, 0.03) 0.102
5 No washout 2015 Immediate change -0.11 (-0.47, 0.26) 0.571
6 No washout 2015 Slope difference 0.07 (-0.04, 0.18) 0.239
7 No washout 2020 Immediate change 0.16 (-0.26, 0.59) 0.443
8 No washout 2020 Slope difference -0.06 (-0.31, 0.20) 0.658
9 Washout 6 mo 2005 Immediate change -1.24 (-2.25, -0.24) 0.015
10 Washout 6 mo 2005 Slope difference -0.04 (-0.28, 0.20) 0.751
11 Washout 6 mo 2010 Immediate change -0.21 (-0.82, 0.39) 0.485
12 Washout 6 mo 2010 Slope difference -0.14 (-0.35, 0.07) 0.192
13 Washout 6 mo 2015 Immediate change -0.17 (-0.54, 0.19) 0.356
14 Washout 6 mo 2015 Slope difference 0.09 (-0.04, 0.21) 0.165
15 Washout 6 mo 2020 Immediate change 0.2 (-0.18, 0.59) 0.303
16 Washout 6 mo 2020 Slope difference -0.03 (-0.26, 0.20) 0.802
17 Washout 12 mo 2005 Immediate change -1.45 (-2.54, -0.35) 0.00972
18 Washout 12 mo 2005 Slope difference -0.07 (-0.36, 0.22) 0.62
19 Washout 12 mo 2010 Immediate change -0.18 (-0.82, 0.47) 0.595
20 Washout 12 mo 2010 Slope difference -0.16 (-0.41, 0.09) 0.218
21 Washout 12 mo 2015 Immediate change -0.29 (-0.69, 0.11) 0.154
22 Washout 12 mo 2015 Slope difference 0.1 (-0.05, 0.25) 0.189
23 Washout 12 mo 2020 Immediate change 0.22 (-0.16, 0.61) 0.255
24 Washout 12 mo 2020 Slope difference -0.02 (-0.25, 0.21) 0.864
25 Washout 18 mo 2005 Immediate change -1.57 (-2.81, -0.33) 0.013
26 Washout 18 mo 2005 Slope difference 0.08 (-0.30, 0.45) 0.69
27 Washout 18 mo 2010 Immediate change -0.36 (-1.09, 0.36) 0.33
28 Washout 18 mo 2010 Slope difference -0.11 (-0.44, 0.22) 0.511
29 Washout 18 mo 2015 Immediate change -0.17 (-0.58, 0.24) 0.414
30 Washout 18 mo 2015 Slope difference -0.04 (-0.22, 0.14) 0.646
31 Washout 18 mo 2020 Immediate change 0.3 (-0.06, 0.67) 0.0999
32 Washout 18 mo 2020 Slope difference -0.02 (-0.23, 0.20) 0.866
# Optional prettier HTML table
gt::gt(epi_all)
| No washout |
2005 |
Immediate change |
-0.67 |
(-1.50, 0.17) |
0.11600 |
| No washout |
2005 |
Slope difference |
-0.11 |
(-0.31, 0.10) |
0.30600 |
| No washout |
2010 |
Immediate change |
-0.13 |
(-0.69, 0.43) |
0.65300 |
| No washout |
2010 |
Slope difference |
-0.14 |
(-0.32, 0.03) |
0.10200 |
| No washout |
2015 |
Immediate change |
-0.11 |
(-0.47, 0.26) |
0.57100 |
| No washout |
2015 |
Slope difference |
0.07 |
(-0.04, 0.18) |
0.23900 |
| No washout |
2020 |
Immediate change |
0.16 |
(-0.26, 0.59) |
0.44300 |
| No washout |
2020 |
Slope difference |
-0.06 |
(-0.31, 0.20) |
0.65800 |
| Washout 6 mo |
2005 |
Immediate change |
-1.24 |
(-2.25, -0.24) |
0.01500 |
| Washout 6 mo |
2005 |
Slope difference |
-0.04 |
(-0.28, 0.20) |
0.75100 |
| Washout 6 mo |
2010 |
Immediate change |
-0.21 |
(-0.82, 0.39) |
0.48500 |
| Washout 6 mo |
2010 |
Slope difference |
-0.14 |
(-0.35, 0.07) |
0.19200 |
| Washout 6 mo |
2015 |
Immediate change |
-0.17 |
(-0.54, 0.19) |
0.35600 |
| Washout 6 mo |
2015 |
Slope difference |
0.09 |
(-0.04, 0.21) |
0.16500 |
| Washout 6 mo |
2020 |
Immediate change |
0.20 |
(-0.18, 0.59) |
0.30300 |
| Washout 6 mo |
2020 |
Slope difference |
-0.03 |
(-0.26, 0.20) |
0.80200 |
| Washout 12 mo |
2005 |
Immediate change |
-1.45 |
(-2.54, -0.35) |
0.00972 |
| Washout 12 mo |
2005 |
Slope difference |
-0.07 |
(-0.36, 0.22) |
0.62000 |
| Washout 12 mo |
2010 |
Immediate change |
-0.18 |
(-0.82, 0.47) |
0.59500 |
| Washout 12 mo |
2010 |
Slope difference |
-0.16 |
(-0.41, 0.09) |
0.21800 |
| Washout 12 mo |
2015 |
Immediate change |
-0.29 |
(-0.69, 0.11) |
0.15400 |
| Washout 12 mo |
2015 |
Slope difference |
0.10 |
(-0.05, 0.25) |
0.18900 |
| Washout 12 mo |
2020 |
Immediate change |
0.22 |
(-0.16, 0.61) |
0.25500 |
| Washout 12 mo |
2020 |
Slope difference |
-0.02 |
(-0.25, 0.21) |
0.86400 |
| Washout 18 mo |
2005 |
Immediate change |
-1.57 |
(-2.81, -0.33) |
0.01300 |
| Washout 18 mo |
2005 |
Slope difference |
0.08 |
(-0.30, 0.45) |
0.69000 |
| Washout 18 mo |
2010 |
Immediate change |
-0.36 |
(-1.09, 0.36) |
0.33000 |
| Washout 18 mo |
2010 |
Slope difference |
-0.11 |
(-0.44, 0.22) |
0.51100 |
| Washout 18 mo |
2015 |
Immediate change |
-0.17 |
(-0.58, 0.24) |
0.41400 |
| Washout 18 mo |
2015 |
Slope difference |
-0.04 |
(-0.22, 0.14) |
0.64600 |
| Washout 18 mo |
2020 |
Immediate change |
0.30 |
(-0.06, 0.67) |
0.09990 |
| Washout 18 mo |
2020 |
Slope difference |
-0.02 |
(-0.23, 0.20) |
0.86600 |