@@ -35,43 +35,43 @@ x_lagged
35
35
attributes(x_lagged )$ metadata $ as_of <- testing_as_of
36
36
37
37
test_that(" epi_adjust_latency correctly extends the lags" , {
38
- r5 <- epi_recipe(x ) %> %
38
+ r1 <- epi_recipe(x ) %> %
39
39
step_epi_lag(death_rate , lag = c(0 , 6 , 11 )) %> %
40
40
step_epi_lag(case_rate , lag = c(1 , 5 )) %> %
41
41
step_epi_ahead(death_rate , ahead = ahead ) %> %
42
42
step_adjust_latency(method = " extend_lags" )
43
43
# the as_of on x is today's date, which is >970 days in the future
44
44
# also, there's no data >970 days in the past, so it gets an error trying to
45
45
# 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" )
47
47
48
48
# 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 )
50
50
51
51
expect_equal(
52
- names(fit5 $ pre $ mold $ predictors ),
52
+ names(fit1 $ pre $ mold $ predictors ),
53
53
c(
54
54
" lag_5_death_rate" , " lag_11_death_rate" , " lag_16_death_rate" ,
55
55
" lag_6_case_rate" , " lag_10_case_rate"
56
56
)
57
57
)
58
- latest <- get_test_data(r5 , x )
59
- pred <- predict(fit5 , latest )
58
+ latest <- get_test_data(r1 , x )
59
+ pred <- predict(fit1 , latest )
60
60
point_pred <- pred %> % filter(! is.na(.pred ))
61
61
expect_equal(nrow(point_pred ), 1 )
62
62
expect_equal(point_pred $ time_value , as.Date(testing_as_of ))
63
63
64
64
expect_equal(
65
- names(fit5 $ pre $ mold $ outcomes ),
65
+ names(fit1 $ pre $ mold $ outcomes ),
66
66
glue :: glue(" ahead_{ahead}_death_rate" )
67
67
)
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 ))
71
71
expect_equal(actual_solutions $ time_value , testing_as_of )
72
72
73
73
# 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 )
75
75
76
76
# result should be equivalent to just immediately doing the adjusted lags by
77
77
# hand
@@ -81,38 +81,38 @@ test_that("epi_adjust_latency correctly extends the lags", {
81
81
step_epi_ahead(death_rate , ahead = ahead )
82
82
fit_hand_adj <- slm_fit(hand_adjusted , data = real_x )
83
83
expect_equal(
84
- fit5 $ fit $ fit $ fit $ coefficients ,
84
+ fit1 $ fit $ fit $ fit $ coefficients ,
85
85
fit_hand_adj $ fit $ fit $ fit $ coefficients
86
86
)
87
87
})
88
88
89
89
test_that(" epi_adjust_latency correctly extends the ahead" , {
90
- r5 <- epi_recipe(x ) %> %
90
+ r2 <- epi_recipe(x ) %> %
91
91
step_epi_lag(death_rate , lag = c(0 , 6 , 11 )) %> %
92
92
step_epi_lag(case_rate , lag = c(1 , 5 )) %> %
93
93
step_epi_ahead(death_rate , ahead = ahead ) %> %
94
94
step_adjust_latency(method = " extend_ahead" )
95
95
# the as_of on x is today's date, which is >970 days in the future
96
96
# also, there's no data >970 days in the past, so it gets an error trying to
97
97
# fit on no data
98
- expect_error(expect_warning(fit5 <- slm_fit(r5 )))
98
+ expect_error(expect_warning(fit5 <- slm_fit(r2 )))
99
99
# real date example
100
- fit5 <- slm_fit(r5 , data = real_x )
100
+ fit2 <- slm_fit(r2 , data = real_x )
101
101
expect_equal(
102
- names(fit5 $ pre $ mold $ predictors ),
102
+ names(fit2 $ pre $ mold $ predictors ),
103
103
c(
104
104
" lag_0_death_rate" , " lag_6_death_rate" , " lag_11_death_rate" ,
105
105
" lag_1_case_rate" , " lag_5_case_rate"
106
106
)
107
107
)
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 ))
111
111
# 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 ))
113
113
# target column renamed
114
114
expect_equal(
115
- names(fit5 $ pre $ mold $ outcomes ),
115
+ names(fit2 $ pre $ mold $ outcomes ),
116
116
glue :: glue(" ahead_{ahead + latency}_death_rate" )
117
117
)
118
118
# fit an equivalent forecaster
@@ -123,12 +123,71 @@ test_that("epi_adjust_latency correctly extends the ahead", {
123
123
equiv_fit <- slm_fit(equivalent , data = real_x )
124
124
# adjusting the ahead should do the same thing as directly adjusting the ahead
125
125
expect_equal(
126
- fit5 $ fit $ fit $ fit $ coefficients ,
126
+ fit2 $ fit $ fit $ fit $ coefficients ,
127
127
equiv_fit $ fit $ fit $ fit $ coefficients
128
128
)
129
129
130
130
# 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 )
132
191
})
133
192
134
193
test_that(" epi_adjust_latency fixed_* work" , {})
0 commit comments