|
| 1 | +library(dplyr) |
| 2 | +library(epiprocess) |
| 3 | +library(parsnip) |
| 4 | +library(workflows) |
| 5 | + |
| 6 | +# Random generated dataset |
| 7 | +set.seed(100) |
| 8 | +x <- tibble(geo_value = rep("nowhere",200), |
| 9 | + time_value = as.Date("2021-01-01") + 0:199, |
| 10 | + case_rate = rpois(100,20) + 1:200, |
| 11 | + death_rate = rpois(100,10) + 1:200) %>% |
| 12 | + as_epi_df() |
| 13 | + |
| 14 | +slm_fit <- function(recipe, data = x) { |
| 15 | + workflow() %>% |
| 16 | + add_recipe(recipe) %>% |
| 17 | + add_model(linear_reg()) %>% |
| 18 | + fit(data = data) |
| 19 | +} |
| 20 | + |
| 21 | +# Tests |
| 22 | +test_that("Check that epi_ahead shifts properly", { |
| 23 | + r1 <- epi_recipe(x) %>% |
| 24 | + step_epi_ahead(death_rate, ahead = 7) %>% |
| 25 | + step_epi_lag(death_rate, lag = -7) %>% |
| 26 | + step_naomit(all_predictors()) %>% |
| 27 | + step_naomit(all_outcomes(), skip = TRUE) |
| 28 | + |
| 29 | + slm_fit1 <- slm_fit(r1) |
| 30 | + |
| 31 | + slope_ahead <- slm_fit1$fit$fit$fit$coefficients[[2]] |
| 32 | + expect_equal(slope_ahead,1) |
| 33 | +}) |
| 34 | + |
| 35 | +test_that("Check that epi_lag shifts properly", { |
| 36 | + r2 <- epi_recipe(x) %>% |
| 37 | + step_epi_ahead(death_rate, ahead = -7) %>% |
| 38 | + step_epi_lag(death_rate, lag = 7) %>% |
| 39 | + step_naomit(all_predictors()) %>% |
| 40 | + step_naomit(all_outcomes(), skip = TRUE) |
| 41 | + |
| 42 | + slm_fit2 <- slm_fit(r2) |
| 43 | + |
| 44 | + slope_lag <- slm_fit2$fit$fit$fit$coefficients[[2]] |
| 45 | + expect_equal(slope_lag,1) |
| 46 | +}) |
| 47 | + |
| 48 | +test_that("Check for non-integer values", { |
| 49 | + r3 <- epi_recipe(x) %>% |
| 50 | + step_epi_ahead(death_rate, ahead = 3.6) %>% |
| 51 | + step_epi_lag(death_rate, lag = 1.9) |
| 52 | + expect_error( |
| 53 | + slm_fit(r3) |
| 54 | + ) |
| 55 | +}) |
| 56 | + |
| 57 | +test_that("Check for duplicate values", { |
| 58 | + r4 <- epi_recipe(x) %>% |
| 59 | + step_epi_ahead(death_rate, ahead = 7) %>% |
| 60 | + step_epi_lag(death_rate, lag = 7) %>% |
| 61 | + step_epi_lag(death_rate, lag = 7) |
| 62 | + expect_error( |
| 63 | + slm_fit(r4) |
| 64 | + ) |
| 65 | +}) |
0 commit comments