-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from tuxette/testthat
first version of the tests based on examples
- Loading branch information
Showing
21 changed files
with
940 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,5 +62,6 @@ Suggests: | |
rmarkdown, | ||
fable, | ||
feasts, | ||
future.apply | ||
future.apply, | ||
testthat | ||
VignetteBuilder: knitr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
codecov: | ||
token: 3b555487-3534-4ebf-9985-546286a42ed9 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
library("testthat") | ||
library("EpiSoon") | ||
|
||
test_check("EpiSoon") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
##------------------------------------------------------------ | ||
context("Test of the function 'compare_models'.... testing outputs") | ||
|
||
models <- list("AR 3" = function(...) { | ||
EpiSoon::bsts_model(model = function(ss, y){ | ||
bsts::AddAr(ss, y = y, lags = 3) | ||
}, ...)}, | ||
"Semi-local linear trend" = function(...) { | ||
EpiSoon::bsts_model(model = function(ss, y){ | ||
bsts::AddSemilocalLinearTrend(ss, y = y) | ||
}, ...)} | ||
) | ||
|
||
out <- compare_models(EpiSoon::example_obs_rts, | ||
EpiSoon::example_obs_cases, | ||
models, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval) | ||
|
||
test_that("Outputs have proper lenghts and names", { | ||
expect_length(out, 4) | ||
|
||
expect_named(out, c("forecast_rts", "rt_scores", "forecast_cases", "case_scores")) | ||
|
||
# 'return_raw' version | ||
out_raw <- compare_models(EpiSoon::example_obs_rts, | ||
EpiSoon::example_obs_cases, | ||
models, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval, | ||
return_raw = TRUE) | ||
|
||
expect_length(out_raw, 6) | ||
|
||
expect_named(out_raw, c("forecast_rts", "rt_scores", "forecast_cases", | ||
"case_scores", "raw_rt_forecast", "raw_case_forecast")) | ||
|
||
}) | ||
|
||
test_that("Outputs return results for all models", { | ||
expect_identical(names(models), unique(out$forecast_rts$model)) | ||
expect_equal(sum(is.na(out$forecast_rts)), 0) | ||
|
||
expect_identical(names(models), unique(out$rt_scores$model)) | ||
expect_equal(sum(is.na(out$rt_scores)), 0) | ||
|
||
expect_identical(names(models), unique(out$forecast_cases$model)) | ||
expect_equal(sum(is.na(out$forecast_cases)), 0) | ||
|
||
expect_identical(names(models), unique(out$case_scores$model)) | ||
expect_equal(sum(is.na(out$case_scores)), 0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
##------------------------------------------------------------ | ||
context("Test of the function 'compare_timeseries'.... testing outputs") | ||
|
||
obs_rts <- EpiSoon::example_obs_rts %>% | ||
dplyr::mutate(timeseries = "Region 1") %>% | ||
dplyr::bind_rows(EpiSoon::example_obs_rts %>% | ||
dplyr::mutate(timeseries = "Region 2")) | ||
|
||
obs_cases <- EpiSoon::example_obs_cases %>% | ||
dplyr::mutate(timeseries = "Region 1") %>% | ||
dplyr::bind_rows(EpiSoon::example_obs_cases %>% | ||
dplyr::mutate(timeseries = "Region 2")) | ||
|
||
models <- list("AR 3" = function(...){ | ||
EpiSoon::bsts_model(model = function(ss, y){ | ||
bsts::AddAr(ss, y = y, lags = 3) | ||
}, ...)}, | ||
"Semi-local linear trend" = function(...) { | ||
EpiSoon::bsts_model(model = function(ss, y){ | ||
bsts::AddSemilocalLinearTrend(ss, y = y) | ||
}, ...)} | ||
) | ||
|
||
out <- compare_timeseries(obs_rts, obs_cases, models, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval) | ||
|
||
test_that("Outputs have proper lenghts and names", { | ||
expect_length(out, 4) | ||
|
||
expect_named(out, c("forecast_rts", "rt_scores", "forecast_cases", "case_scores")) | ||
|
||
# 'return_raw' version | ||
out_raw <- compare_timeseries(obs_rts, obs_cases, models, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval, | ||
return_raw = TRUE) | ||
|
||
expect_length(out_raw, 6) | ||
|
||
expect_named(out_raw, c("forecast_rts", "rt_scores", "forecast_cases", | ||
"case_scores", "raw_rt_forecast", "raw_case_forecast")) | ||
|
||
}) | ||
|
||
test_that("Outputs return results for all models", { | ||
expect_identical(names(models), unique(out$forecast_rts$model)) | ||
expect_equal(sum(is.na(out$forecast_rts)), 0) | ||
|
||
expect_identical(names(models), unique(out$rt_scores$model)) | ||
expect_equal(sum(is.na(out$rt_scores)), 0) | ||
|
||
expect_identical(names(models), unique(out$forecast_cases$model)) | ||
expect_equal(sum(is.na(out$forecast_cases)), 0) | ||
|
||
expect_identical(names(models), unique(out$case_scores$model)) | ||
expect_equal(sum(is.na(out$case_scores)), 0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# draw_from_si_prob | ||
# This function returns result for requested days. It simply calls for | ||
# serial_interval[days_ago] but checking first the 'days_ago' are in the | ||
# interval. Otherwise, it returns 0. | ||
# Things to test: | ||
## In tests | ||
# - that the function return the proper result | ||
# - that the result is of the proper class | ||
|
||
exampleResult <- draw_from_si_prob(c(1, 2, 4, 10), | ||
EpiSoon::example_serial_interval) | ||
# expectedResult <- EpiSoon::example_serial_interval[c(1, 2, 4, 10)] | ||
test_that("The expected draw is obtained", { | ||
# expect_equal(exampleResult, expectedResult) | ||
expect_true(is.numeric(exampleResult)) | ||
expect_length(exampleResult, 4) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
##------------------------------------------------------------ | ||
context("Test of the function 'evaluate_model'.... testing inputs") | ||
|
||
sampled_obs <- EpiSoon::example_obs_rts %>% | ||
dplyr::mutate(sample = 1) %>% | ||
dplyr::bind_rows(EpiSoon::example_obs_rts %>% | ||
dplyr::mutate(sample = 2)) | ||
|
||
sampled_cases <- EpiSoon::example_obs_cases %>% | ||
dplyr::mutate(sample = 1) %>% | ||
dplyr::bind_rows(EpiSoon::example_obs_cases %>% | ||
dplyr::mutate(sample = 2)) | ||
|
||
a_model <- function(...) { | ||
EpiSoon::bsts_model(model = function(ss, y) { | ||
bsts::AddSemilocalLinearTrend(ss, y = y) | ||
}, ...) | ||
} | ||
|
||
test_that("Inputs with unequal lengths return error", { | ||
sampled_cases_sub <- sampled_cases %>% filter(sample != 2) | ||
|
||
expect_error( | ||
evaluate_model(sampled_obs, | ||
sampled_cases_sub, | ||
model = a_model, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval), | ||
"Must have the same number of Rt and case samples." | ||
) | ||
}) | ||
|
||
context("Test of the function 'evaluate_model'.... testing outputs") | ||
|
||
test_that("Outputs have proper lenghts and names", { | ||
out <- evaluate_model(sampled_obs, | ||
sampled_cases, | ||
model = a_model, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval) | ||
|
||
expect_length(out, 4) | ||
|
||
expect_named(out, c("forecast_rts", "rt_scores", "forecast_cases", "case_scores")) | ||
|
||
# raw outputs added | ||
out <- evaluate_model(sampled_obs, | ||
sampled_cases, | ||
model = a_model, | ||
horizon = 7, samples = 10, | ||
serial_interval = EpiSoon::example_serial_interval, | ||
return_raw = TRUE) | ||
|
||
expect_length(out, 6) | ||
|
||
expect_named(out, | ||
c("forecast_rts", "rt_scores", "forecast_cases", "case_scores", | ||
"raw_rt_forecast", "raw_case_forecast")) | ||
|
||
}) |
Oops, something went wrong.