|
| 1 | +library(covidcast) |
| 2 | +library(dplyr) |
| 3 | + |
| 4 | +test_that("aggregate and shift", { |
| 5 | + foo <- structure(data.frame( |
| 6 | + data_source = "foo", |
| 7 | + signal = "foo", |
| 8 | + geo_value = "a", |
| 9 | + value = 1:5, |
| 10 | + time_value = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-05"), "day"), |
| 11 | + issue = as.Date("2020-01-06"), |
| 12 | + stderr = 0.1, |
| 13 | + sample_size = 10, |
| 14 | + lag = 1), |
| 15 | + class = c("covidcast_signal", "data.frame")) |
| 16 | + |
| 17 | + agg <- aggregate_signals(foo, dt = c(-1, 1, 2)) |
| 18 | + |
| 19 | + expect_equal(arrange(agg, time_value), |
| 20 | + structure(data.frame( |
| 21 | + geo_value = "a", |
| 22 | + time_value = foo$time_value, |
| 23 | + "value-1:foo_foo" = c(NA, 1:4), |
| 24 | + "value+1:foo_foo" = c(2:5, NA), |
| 25 | + "value+2:foo_foo" = c(3:5, NA, NA), |
| 26 | + check.names = FALSE), |
| 27 | + class = c("covidcast_signal_wide", "data.frame"))) |
| 28 | +}) |
| 29 | + |
| 30 | +test_that("widen and lengthen", { |
| 31 | + foo <- structure(data.frame( |
| 32 | + data_source = "foo", |
| 33 | + signal = "foo", |
| 34 | + geo_value = c("pa", "tx", "ri"), |
| 35 | + value = 1:3, |
| 36 | + time_value = as.Date("2020-01-01"), |
| 37 | + issue = as.Date("2020-01-02"), |
| 38 | + stderr = 0.5, |
| 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 = c("pa", "tx", "ri"), |
| 47 | + value = 4:6, |
| 48 | + time_value = as.Date("2020-01-01"), |
| 49 | + issue = as.Date("2020-01-02"), |
| 50 | + stderr = 0.5, |
| 51 | + sample_size = 10, |
| 52 | + lag = 1), |
| 53 | + class = c("covidcast_signal", "data.frame")) |
| 54 | + |
| 55 | + agg_wide <- aggregate_signals(list(foo, bar), format = "wide") |
| 56 | + |
| 57 | + expect_equal(arrange(agg_wide, geo_value), |
| 58 | + structure(data.frame( |
| 59 | + geo_value = c("pa", "ri", "tx"), |
| 60 | + time_value = as.Date("2020-01-01"), |
| 61 | + "value+0:foo_foo" = c(1, 3, 2), |
| 62 | + "value+0:bar_bar" = c(4, 6, 5), |
| 63 | + check.names = FALSE), |
| 64 | + class = c("covidcast_signal_wide", "data.frame"))) |
| 65 | + |
| 66 | + long <- covidcast_longer(agg_wide) |
| 67 | + |
| 68 | + expect_equal(arrange(long, data_source, geo_value), |
| 69 | + structure(data.frame( |
| 70 | + data_source = c(rep("bar", 3), rep("foo", 3)), |
| 71 | + signal = c(rep("bar", 3), rep("foo", 3)), |
| 72 | + geo_value = rep(c("pa", "ri", "tx"), 2), |
| 73 | + time_value = as.Date("2020-01-01"), |
| 74 | + dt = 0, |
| 75 | + value = c(4, 6, 5, 1, 3, 2)), |
| 76 | + class = c("covidcast_signal_long", "data.frame"))) |
| 77 | + |
| 78 | + # Now try it long in the first place. Currently fails because wide format does |
| 79 | + # not preserve issue, stderr, sample_size, or lag. |
| 80 | + ## agg_long <- aggregate_signals(list(foo, bar), format = "long") |
| 81 | + ## expect_equal(long, agg_long) |
| 82 | +}) |
0 commit comments