Skip to content

Commit 74aed7a

Browse files
committed
add checks in functionl; add unit tests
1 parent 40a52f6 commit 74aed7a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

R/get_test_data.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323

2424
get_test_data <- function(recipe, x){
2525
# TO-DO: SOME CHECKS OF THE DATASET
26-
## CHECK geo_value, time_value exists
26+
if (any(!(c('geo_value','time_value') %in% colnames(x)))) {
27+
rlang::abort("`geo_value`, `time_value` does not exist in data")
28+
}
2729
## CHECK if it is epi_df?
2830

31+
2932
# initialize vector to hold max lags for each variable
3033
max_lags <- c()
3134
for(i in c(1:length(recipe$steps))){
@@ -34,9 +37,14 @@ get_test_data <- function(recipe, x){
3437
}
3538
}
3639

40+
# CHECK: Return NA if insufficient training data
41+
if (dplyr::n_distinct(x$time_value)< max(max_lags)) {
42+
stop("insufficient training data")
43+
}
44+
3745
test_data <- x %>%
3846
dplyr::filter(
39-
dplyr::across(
47+
dplyr::if_any(
4048
.cols = recipe$term_info$variable[which(recipe$var_info$role == 'raw')],
4149
.fns = ~ !is.na(.x)
4250
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
test_that("return expected number of rows", {
2+
r <- epi_recipe(case_death_rate_subset) %>%
3+
step_epi_ahead(death_rate, ahead = 7) %>%
4+
step_epi_lag(death_rate, lag = c(0, 7, 14, 21, 28)) %>%
5+
step_epi_lag(case_rate, lag = c(0, 7, 14)) %>%
6+
step_naomit(all_predictors()) %>%
7+
step_naomit(all_outcomes(), skip = TRUE)
8+
9+
test <- get_test_data(recipe = r, x = case_death_rate_subset)
10+
11+
expect_equal(nrow(test),
12+
dplyr::n_distinct(case_death_rate_subset$geo_value)* 29)
13+
})
14+
15+
16+
test_that("expect insufficient training data error", {
17+
r <- epi_recipe(case_death_rate_subset) %>%
18+
step_epi_ahead(death_rate, ahead = 7) %>%
19+
step_epi_lag(death_rate, lag = c(0, 367)) %>%
20+
step_naomit(all_predictors()) %>%
21+
step_naomit(all_outcomes(), skip = TRUE)
22+
23+
expect_error(get_test_data(recipe = r, x = case_death_rate_subset))
24+
})
25+
26+
test_that("expect error that geo_value or time_value does not exist", {
27+
r <- epi_recipe(case_death_rate_subset) %>%
28+
step_epi_ahead(death_rate, ahead = 7) %>%
29+
step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
30+
step_epi_lag(case_rate, lag = c(0, 7, 14)) %>%
31+
step_naomit(all_predictors()) %>%
32+
step_naomit(all_outcomes(), skip = TRUE)
33+
34+
wrong_epi_df <- case_death_rate_subset %>% dplyr::select(-geo_value)
35+
36+
expect_error(get_test_data(recipe = r, x = wrong_epi_df))
37+
})

0 commit comments

Comments
 (0)