@@ -35,43 +35,43 @@ x_lagged
3535attributes(x_lagged )$ metadata $ as_of <- testing_as_of
3636
3737test_that(" epi_adjust_latency correctly extends the lags" , {
38- r5 <- epi_recipe(x ) %> %
38+ r1 <- epi_recipe(x ) %> %
3939 step_epi_lag(death_rate , lag = c(0 , 6 , 11 )) %> %
4040 step_epi_lag(case_rate , lag = c(1 , 5 )) %> %
4141 step_epi_ahead(death_rate , ahead = ahead ) %> %
4242 step_adjust_latency(method = " extend_lags" )
4343 # the as_of on x is today's date, which is >970 days in the future
4444 # also, there's no data >970 days in the past, so it gets an error trying to
4545 # fit on no data
46- expect_error(expect_warning(fit5 <- slm_fit(r5 ), regexp = " The shift has been adjusted by 1033" ), class = " simpleError" )
46+ expect_error(expect_warning(fit1 <- slm_fit(r1 ), regexp = " The shift has been adjusted by 1033" ), class = " simpleError" )
4747
4848 # now trying with the as_of a reasonable distance in the future
49- fit5 <- slm_fit(r5 , data = real_x )
49+ fit1 <- slm_fit(r1 , data = real_x )
5050
5151 expect_equal(
52- names(fit5 $ pre $ mold $ predictors ),
52+ names(fit1 $ pre $ mold $ predictors ),
5353 c(
5454 " lag_5_death_rate" , " lag_11_death_rate" , " lag_16_death_rate" ,
5555 " lag_6_case_rate" , " lag_10_case_rate"
5656 )
5757 )
58- latest <- get_test_data(r5 , x )
59- pred <- predict(fit5 , latest )
58+ latest <- get_test_data(r1 , x )
59+ pred <- predict(fit1 , latest )
6060 point_pred <- pred %> % filter(! is.na(.pred ))
6161 expect_equal(nrow(point_pred ), 1 )
6262 expect_equal(point_pred $ time_value , as.Date(testing_as_of ))
6363
6464 expect_equal(
65- names(fit5 $ pre $ mold $ outcomes ),
65+ names(fit1 $ pre $ mold $ outcomes ),
6666 glue :: glue(" ahead_{ahead}_death_rate" )
6767 )
68- latest <- get_test_data(r5 , x )
69- pred <- predict(fit5 , latest )
70- actual_solutions <- pred %> % filter(! is.na(.pred ))
68+ latest <- get_test_data(r1 , x )
69+ pred1 <- predict(fit1 , latest )
70+ actual_solutions <- pred1 %> % filter(! is.na(.pred ))
7171 expect_equal(actual_solutions $ time_value , testing_as_of )
7272
7373 # should have four predictors, including the intercept
74- expect_equal(length(fit5 $ fit $ fit $ fit $ coefficients ), 6 )
74+ expect_equal(length(fit1 $ fit $ fit $ fit $ coefficients ), 6 )
7575
7676 # result should be equivalent to just immediately doing the adjusted lags by
7777 # hand
@@ -81,38 +81,38 @@ test_that("epi_adjust_latency correctly extends the lags", {
8181 step_epi_ahead(death_rate , ahead = ahead )
8282 fit_hand_adj <- slm_fit(hand_adjusted , data = real_x )
8383 expect_equal(
84- fit5 $ fit $ fit $ fit $ coefficients ,
84+ fit1 $ fit $ fit $ fit $ coefficients ,
8585 fit_hand_adj $ fit $ fit $ fit $ coefficients
8686 )
8787})
8888
8989test_that(" epi_adjust_latency correctly extends the ahead" , {
90- r5 <- epi_recipe(x ) %> %
90+ r2 <- epi_recipe(x ) %> %
9191 step_epi_lag(death_rate , lag = c(0 , 6 , 11 )) %> %
9292 step_epi_lag(case_rate , lag = c(1 , 5 )) %> %
9393 step_epi_ahead(death_rate , ahead = ahead ) %> %
9494 step_adjust_latency(method = " extend_ahead" )
9595 # the as_of on x is today's date, which is >970 days in the future
9696 # also, there's no data >970 days in the past, so it gets an error trying to
9797 # fit on no data
98- expect_error(expect_warning(fit5 <- slm_fit(r5 )))
98+ expect_error(expect_warning(fit5 <- slm_fit(r2 )))
9999 # real date example
100- fit5 <- slm_fit(r5 , data = real_x )
100+ fit2 <- slm_fit(r2 , data = real_x )
101101 expect_equal(
102- names(fit5 $ pre $ mold $ predictors ),
102+ names(fit2 $ pre $ mold $ predictors ),
103103 c(
104104 " lag_0_death_rate" , " lag_6_death_rate" , " lag_11_death_rate" ,
105105 " lag_1_case_rate" , " lag_5_case_rate"
106106 )
107107 )
108- latest <- get_test_data(r5 , x )
109- pred <- predict(fit5 , latest )
110- point_pred <- pred %> % filter(! is.na(.pred ))
108+ latest <- get_test_data(r2 , x )
109+ pred2 <- predict(fit2 , latest )
110+ point_pred2 <- pred2 %> % filter(! is.na(.pred ))
111111 # max time is still the forecast date
112- expect_equal(point_pred $ time_value , as.Date(max_time ))
112+ expect_equal(point_pred2 $ time_value , as.Date(max_time ))
113113 # target column renamed
114114 expect_equal(
115- names(fit5 $ pre $ mold $ outcomes ),
115+ names(fit2 $ pre $ mold $ outcomes ),
116116 glue :: glue(" ahead_{ahead + latency}_death_rate" )
117117 )
118118 # fit an equivalent forecaster
@@ -123,12 +123,71 @@ test_that("epi_adjust_latency correctly extends the ahead", {
123123 equiv_fit <- slm_fit(equivalent , data = real_x )
124124 # adjusting the ahead should do the same thing as directly adjusting the ahead
125125 expect_equal(
126- fit5 $ fit $ fit $ fit $ coefficients ,
126+ fit2 $ fit $ fit $ fit $ coefficients ,
127127 equiv_fit $ fit $ fit $ fit $ coefficients
128128 )
129129
130130 # should have four predictors, including the intercept
131- expect_equal(length(fit5 $ fit $ fit $ fit $ coefficients ), 6 )
131+ expect_equal(length(fit2 $ fit $ fit $ fit $ coefficients ), 6 )
132+ })
133+
134+ test_that(" epi_adjust_latency extends multiple aheads" , {
135+ aheads <- 1 : 3
136+ r3 <- epi_recipe(x ) %> %
137+ step_epi_lag(death_rate , lag = c(0 , 6 , 11 )) %> %
138+ step_epi_lag(case_rate , lag = c(1 , 5 )) %> %
139+ step_epi_ahead(death_rate , ahead = aheads ) %> %
140+ step_adjust_latency(method = " extend_ahead" )
141+ fitter <- smooth_quantile_reg(
142+ quantile_levels = 0.5 ,
143+ outcome_locations = aheads ,
144+ degree = 1L
145+ )
146+ epi_wf <- epi_workflow(r3 , fitter )
147+ # the as_of on x is today's date, which is >970 days in the future
148+ # also, there's no data >970 days in the past, so it gets an error trying to
149+ # fit on no data
150+ expect_error(expect_warning(fit3 <- fit(epi_wf , data = x )))
151+ # real date example
152+ fit3 <- fit(epi_wf , data = real_x )
153+ expect_equal(
154+ names(fit3 $ pre $ mold $ outcomes ),
155+ c(
156+ " ahead_6_death_rate" , " ahead_7_death_rate" , " ahead_8_death_rate"
157+ )
158+ )
159+ expect_equal(
160+ names(fit3 $ pre $ mold $ predictors ),
161+ c(
162+ " lag_0_death_rate" , " lag_6_death_rate" , " lag_11_death_rate" ,
163+ " lag_1_case_rate" , " lag_5_case_rate"
164+ )
165+ )
166+ latest <- get_test_data(r3 , real_x )
167+ pred3 <- predict(fit3 , latest )
168+ point_pred <- pred3 %> %
169+ unnest(.pred ) %> %
170+ filter(! is.na(distn ))
171+ # max time is still the forecast date
172+ expect_equal(
173+ point_pred $ time_value ,
174+ rep(as.Date(max_time ), 3 )
175+ )
176+ # fit an equivalent forecaster
177+ equivalent <- epi_recipe(x ) %> %
178+ step_epi_lag(death_rate , lag = c(0 , 6 , 11 )) %> %
179+ step_epi_lag(case_rate , lag = c(1 , 5 )) %> %
180+ step_epi_ahead(death_rate , ahead = ahead + latency )
181+ equiv_fit <- fit(epi_wf , data = real_x )
182+ # adjusting the ahead should do the same thing as directly adjusting the ahead
183+ equiv_fit
184+ expect_equal(
185+ fit3 $ fit $ fit $ fit $ rqfit ,
186+ equiv_fit $ fit $ fit $ fit $ rqfit
187+ )
188+
189+ # should have four predictors, including the intercept
190+ expect_equal(length(fit3 $ fit $ fit $ fit $ rqfit $ coefficients ), 6 )
132191})
133192
134193test_that(" epi_adjust_latency fixed_* work" , {})
0 commit comments