Skip to content

Commit 6dcf722

Browse files
committed
WIP epi_slide_opt.epi_archive tests
1 parent 7b4ac23 commit 6dcf722

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
library(dplyr)
2+
3+
test_that("epi_slide_opt_archive_one_epikey works as expected", {
4+
start_date <- as.Date("2020-01-01")
5+
6+
updates <- bind_rows(
7+
tibble(version = 10, time_value = 0:20, value = 0:20),
8+
tibble(version = 12, time_value = 4:5, value = 5:4),
9+
tibble(version = 13, time_value = 8, value = 9),
10+
tibble(version = 14, time_value = 11, value = NA),
11+
tibble(version = 15, time_value = -10, value = -10),
12+
tibble(version = 16, time_value = 50, value = 50)
13+
) %>%
14+
mutate(across(c(version, time_value), ~ start_date - 1 + .x)) %>%
15+
tidyr::nest(.by = version, .key = "subtbl")
16+
17+
expected <- list(
18+
vctrs::vec_cbind(
19+
tibble(version = 10),
20+
updates$subtbl[[1L]] %>%
21+
mutate(time_value = as.numeric(time_value - start_date) + 1) %>%
22+
mutate(slide_value = frollmean(value, 3, algo = "exact"))
23+
),
24+
tibble(
25+
version = 12,
26+
time_value = c(4, 5, 7), # time 6 unchanged, compactified away
27+
# time 7 `value` unchanged, but here because `slide_value` changed:
28+
value = c(5, 4, 7),
29+
slide_value = c(
30+
mean(c(2, 3, 5)),
31+
# time 5 `slide_value` unchanged, but here because `value` changed:
32+
mean(c(3, 5, 4)),
33+
mean(c(4, 6, 7))
34+
)
35+
),
36+
tibble(
37+
version = 13, time_value = 8:10, value = c(9, 9, 10),
38+
slide_value = frollmean(c(6, 7, 9, 9, 10), 3, algo = "exact")[-(1:2)]
39+
),
40+
tibble(
41+
version = 14, time_value = 11:13, value = c(NA, 12, 13), slide_value = rep(NA_real_, 3L)
42+
),
43+
tibble(
44+
version = 15, time_value = -10, value = -10, slide_value = NA_real_
45+
),
46+
tibble(
47+
version = 16, time_value = 50, value = 50, slide_value = NA_real_
48+
)
49+
) %>%
50+
lapply(function(x) {
51+
x %>%
52+
mutate(across(c(version, time_value), ~ start_date - 1 + .x))
53+
})
54+
55+
f <- purrr::partial(data.table::frollmean, algo = "exact")
56+
57+
result <- updates %>%
58+
epi_slide_opt_archive_one_epikey("value", f, "data.table", 2L, 0L, "day", "slide_value")
59+
60+
expect_equal(
61+
result %>% lapply(function(x) {
62+
x %>%
63+
arrange(time_value) %>%
64+
select(version, time_value, everything())
65+
})
66+
,
67+
expected
68+
)
69+
70+
# TODO check about version nesting ordering
71+
72+
})
73+
74+
# TODO tests on example data sets

0 commit comments

Comments
 (0)