Skip to content

Commit 9bd976b

Browse files
authored
Merge pull request #329 from cmu-delphi/add-agg-tests
Add additional aggregation tests to R covidcast package
2 parents 06ef5fc + be0f016 commit 9bd976b

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

R-packages/covidcast/tests/testthat/test-wrangle.R

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,101 @@ test_that("aggregated signals have times shifted correctly", {
2727
class = c("covidcast_signal_wide", "data.frame")))
2828
})
2929

30+
test_that("list of aggregated signals have times shifted correctly", {
31+
foo <- structure(data.frame(
32+
data_source = "foo",
33+
signal = "foo",
34+
geo_value = "a",
35+
value = 1:5,
36+
time_value = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-05"), "day"),
37+
issue = as.Date("2020-01-06"),
38+
stderr = 0.1,
39+
sample_size = 10,
40+
lag = 1),
41+
class = c("covidcast_signal", "data.frame"))
42+
43+
bar <- structure(data.frame(
44+
data_source = "bar",
45+
signal = "bar",
46+
geo_value = "a",
47+
value = 6:10,
48+
time_value = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-05"), "day"),
49+
issue = as.Date("2020-01-06"),
50+
stderr = 0.1,
51+
sample_size = 10,
52+
lag = 1),
53+
class = c("covidcast_signal", "data.frame"))
54+
55+
# list of lags for each input signal
56+
agg <- aggregate_signals(list(foo, bar), dt = list(0, c(-1, 1, 2)))
57+
58+
expect_equal(arrange(agg, time_value),
59+
structure(data.frame(
60+
geo_value = "a",
61+
time_value = foo$time_value,
62+
"value+0:foo_foo" = c(1:5),
63+
"value-1:bar_bar" = c(NA, 6:9),
64+
"value+1:bar_bar" = c(7:10, NA),
65+
"value+2:bar_bar" = c(8:10, NA, NA),
66+
check.names = FALSE),
67+
class = c("covidcast_signal_wide", "data.frame")))
68+
69+
# single vector of lags for all input signals
70+
agg <- aggregate_signals(list(foo, bar), dt = c(-1, 1, 2))
71+
72+
expect_equal(arrange(agg, time_value),
73+
structure(data.frame(
74+
geo_value = "a",
75+
time_value = foo$time_value,
76+
"value-1:foo_foo" = c(NA, 1:4),
77+
"value+1:foo_foo" = c(2:5, NA),
78+
"value+2:foo_foo" = c(3:5, NA, NA),
79+
"value-1:bar_bar" = c(NA, 6:9),
80+
"value+1:bar_bar" = c(7:10, NA),
81+
"value+2:bar_bar" = c(8:10, NA, NA),
82+
check.names = FALSE),
83+
class = c("covidcast_signal_wide", "data.frame")))
84+
})
85+
86+
test_that("signals are joined with full join", {
87+
foo <- structure(data.frame(
88+
data_source = "foo",
89+
signal = "foo",
90+
geo_value = "a",
91+
value = 1:5,
92+
time_value = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-05"), "day"),
93+
issue = as.Date("2020-01-06"),
94+
stderr = 0.1,
95+
sample_size = 10,
96+
lag = 1),
97+
class = c("covidcast_signal", "data.frame"))
98+
99+
bar <- structure(data.frame(
100+
data_source = "bar",
101+
signal = "bar",
102+
geo_value = "a",
103+
value = 6:10,
104+
time_value = seq.Date(as.Date("2020-01-03"), as.Date("2020-01-07"), "day"),
105+
issue = as.Date("2020-01-06"),
106+
stderr = 0.1,
107+
sample_size = 10,
108+
lag = 1),
109+
class = c("covidcast_signal", "data.frame"))
110+
111+
112+
agg <- aggregate_signals(list(foo, bar))
113+
114+
expect_equal(arrange(agg, time_value),
115+
structure(data.frame(
116+
geo_value = "a",
117+
time_value = seq.Date(as.Date("2020-01-01"),
118+
as.Date("2020-01-07"), "day"),
119+
"value+0:foo_foo" = c(1:5, NA, NA),
120+
"value+0:bar_bar" = c(NA, NA, 6:10),
121+
check.names = FALSE),
122+
class = c("covidcast_signal_wide", "data.frame")))
123+
})
124+
30125
test_that("aggregated data can be made longer", {
31126
foo <- structure(data.frame(
32127
data_source = "foo",

0 commit comments

Comments
 (0)