Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first version of the tests based on examples #45

Merged
merged 27 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6012843
created test directory and dependency (Suggests) to testthat
Apr 13, 2020
288135e
Start unit tests for group3 functions defined by Nath
andreamrau Apr 14, 2020
ec8f3db
created a test file for "evaluate_model"
Apr 15, 2020
7ed4787
forecast_cases() unit test draft
Apr 18, 2020
cd83999
fixed a typo in tests of "forecast_cases"
Apr 21, 2020
8f99d29
tests(evaluate model): improved verbose
Apr 21, 2020
b008998
added a test file for 'compare_models'
Apr 21, 2020
08e1245
added a test file for 'compare_timeseries'
Apr 21, 2020
6cdf346
added a test file for 'summarise_scores'
Apr 21, 2020
92ceb06
Add forecast_rt tests
andreamrau Apr 22, 2020
7a1d5c6
added the tests for the group 1 functions
achateigner Apr 22, 2020
2d0dd7e
Add iterative_rt_forecast tests
andreamrau Apr 22, 2020
b6b6583
corrected the as_tibble bug
achateigner Apr 22, 2020
60a9757
added a test file for 'plot_forecast'
Apr 22, 2020
087025a
added a test for 'plot_forecast_evaluation'
Apr 22, 2020
43dabb8
Add score_forecast tests
andreamrau Apr 22, 2020
524b2dd
Add score_case_forecast tests
andreamrau Apr 23, 2020
32a92a4
Unit tests group 2
maressyl Apr 26, 2020
f607575
fixed a bug in test_plot_forecast
Apr 27, 2020
a13bbba
changed a file name to avoid possible problems with brackets
Apr 27, 2020
b752f73
split testGroup1 into separated files + translated comments in English
Apr 27, 2020
2425fae
commented lines that possibly make the tests fail depending on random…
Apr 27, 2020
62ecdbd
move (almost) all comments in tests to github issues
Apr 27, 2020
5d10adc
Fix bugs in test_score_forecast and test_score_case_forecast
andreamrau Apr 27, 2020
fa71811
added codecov support
Apr 28, 2020
7c46479
Aurelien: changed the way I tested the results, as long as I cannot f…
Apr 29, 2020
dc236e1
minor additions in tests (testing number of rows in outputs)
Apr 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ Suggests:
rmarkdown,
fable,
feasts,
future.apply
future.apply,
testthat
VignetteBuilder: knitr
3 changes: 3 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ knitr::opts_chunk$set(

[![DOI](https://zenodo.org/badge/248311916.svg)](https://zenodo.org/badge/latestdoi/248311916)
[![Build Status](https://travis-ci.com/epiforecasts/EpiSoon.svg?branch=master)](https://travis-ci.com/epiforecasts/EpiSoon)
[![codecov](https://codecov.io/gh/tuxette/EpiSoon/branch/master/graph/badge.svg)](https://codecov.io/gh/tuxette/EpiSoon)



*Warning: This package is a work in progress and is currently developed solely with the COVID-19 outbreak in mind. Breaking changes may occur and the authors cannot guarantee support.*

Expand Down
152 changes: 76 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

# EpiSoon

[![DOI](https://zenodo.org/badge/248311916.svg)](https://zenodo.org/badge/latestdoi/248311916)
[![Build
Status](https://travis-ci.com/epiforecasts/EpiSoon.svg?branch=master)](https://travis-ci.com/epiforecasts/EpiSoon)
[![codecov](https://codecov.io/gh/tuxette/EpiSoon/branch/master/graph/badge.svg)](https://codecov.io/gh/tuxette/EpiSoon)

*Warning: This package is a work in progress and is currently developed
solely with the COVID-19 outbreak in mind. Breaking changes may occur
Expand Down Expand Up @@ -84,70 +86,72 @@ forecasts <- EpiSoon::compare_timeseries(obs_rts, obs_cases, models,

forecasts
#> $forecast_rts
#> # A tibble: 791 x 12
#> timeseries model forecast_date date horizon median mean sd bottom
#> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 2020-03-04 2020-03-05 1 2.12 2.00 0.599 0.386
#> 2 Region 1 AR 3 2020-03-04 2020-03-06 2 1.92 1.90 0.503 0.733
#> 3 Region 1 AR 3 2020-03-04 2020-03-07 3 1.74 1.67 0.722 0
#> 4 Region 1 AR 3 2020-03-04 2020-03-08 4 1.63 1.57 0.735 0
#> 5 Region 1 AR 3 2020-03-04 2020-03-09 5 1.49 1.59 0.620 0.871
#> 6 Region 1 AR 3 2020-03-04 2020-03-10 6 1.44 1.42 0.881 0
#> 7 Region 1 AR 3 2020-03-04 2020-03-11 7 1.41 1.38 0.879 0
#> 8 Region 1 AR 3 2020-03-05 2020-03-06 1 2.20 2.20 0.00600 2.19
#> 9 Region 1 AR 3 2020-03-05 2020-03-07 2 2.13 2.13 0.0145 2.12
#> 10 Region 1 AR 3 2020-03-05 2020-03-08 3 2.07 2.07 0.0137 2.05
#> # … with 781 more rows, and 3 more variables: lower <dbl>, upper <dbl>,
#> # top <dbl>
#> # A tibble: 763 x 12
#> timeseries model forecast_date date horizon median mean sd
#> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 2020-03-06 2020-03-07 1 2.14 2.13 0.0234
#> 2 Region 1 AR 3 2020-03-06 2020-03-08 2 2.07 2.07 0.0113
#> 3 Region 1 AR 3 2020-03-06 2020-03-09 3 2.01 2.01 0.0197
#> 4 Region 1 AR 3 2020-03-06 2020-03-10 4 1.94 1.93 0.0212
#> 5 Region 1 AR 3 2020-03-06 2020-03-11 5 1.89 1.89 0.0136
#> 6 Region 1 AR 3 2020-03-06 2020-03-12 6 1.84 1.83 0.0171
#> 7 Region 1 AR 3 2020-03-06 2020-03-13 7 1.78 1.78 0.0260
#> 8 Region 1 AR 3 2020-03-07 2020-03-08 1 2.08 2.08 0.0154
#> 9 Region 1 AR 3 2020-03-07 2020-03-09 2 2.01 2.02 0.0204
#> 10 Region 1 AR 3 2020-03-07 2020-03-10 3 1.96 1.96 0.0235
#> # … with 753 more rows, and 4 more variables: bottom <dbl>, lower <dbl>,
#> # upper <dbl>, top <dbl>
#>
#> $rt_scores
#> # A tibble: 629 x 10
#> timeseries model forecast_date date horizon dss crps logs bias
#> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 2020-03-04 2020-03-05 1 -0.914 0.0985 -0.312 0.4
#> 2 Region 1 AR 3 2020-03-04 2020-03-06 2 -1.11 0.148 0.148 0.4
#> 3 Region 1 AR 3 2020-03-04 2020-03-07 3 -0.265 0.223 0.558 0.4
#> 4 Region 1 AR 3 2020-03-04 2020-03-08 4 -0.143 0.259 0.736 0.4
#> 5 Region 1 AR 3 2020-03-04 2020-03-09 5 -0.413 0.297 0.822 0.4
#> 6 Region 1 AR 3 2020-03-04 2020-03-10 6 0.172 0.340 1.08 0.4
#> 7 Region 1 AR 3 2020-03-04 2020-03-11 7 0.217 0.347 1.09 0.4
#> 8 Region 1 AR 3 2020-03-05 2020-03-06 1 -6.97 0.00727 -3.21 1
#> 9 Region 1 AR 3 2020-03-05 2020-03-07 2 -7.38 0.0100 -2.70 0.100
#> 10 Region 1 AR 3 2020-03-05 2020-03-08 3 -1.37 0.0278 -0.281 0
#> # … with 619 more rows, and 1 more variable: sharpness <dbl>
#> # A tibble: 601 x 14
#> timeseries model forecast_date date horizon dss crps logs
#> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 2020-03-06 2020-03-07 1 -7.14 0.00798 -2.59
#> 2 Region 1 AR 3 2020-03-06 2020-03-08 2 3.78 0.0325 4.06
#> 3 Region 1 AR 3 2020-03-06 2020-03-09 3 0.452 0.0438 1.12
#> 4 Region 1 AR 3 2020-03-06 2020-03-10 4 13.5 0.0823 33.8
#> 5 Region 1 AR 3 2020-03-06 2020-03-11 5 84.5 0.118 61.9
#> 6 Region 1 AR 3 2020-03-06 2020-03-12 6 98.6 0.160 289.
#> 7 Region 1 AR 3 2020-03-06 2020-03-13 7 53.3 0.178 59.3
#> 8 Region 1 AR 3 2020-03-07 2020-03-08 1 -6.03 0.0151 -2.43
#> 9 Region 1 AR 3 2020-03-07 2020-03-09 2 -2.68 0.0334 -1.34
#> 10 Region 1 AR 3 2020-03-07 2020-03-10 3 1.65 0.0555 -0.150
#> # … with 591 more rows, and 6 more variables: bias <dbl>, sharpness <dbl>,
#> # calibration <dbl>, median <dbl>, iqr <dbl>, ci <dbl>
#>
#> $forecast_cases
#> # A tibble: 629 x 12
#> timeseries model forecast_date date horizon median mean sd bottom
#> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 2020-03-04 2020-03-05 1 57 52.3 15.4 11
#> 2 Region 1 AR 3 2020-03-04 2020-03-06 2 58 57.9 17.1 22
#> 3 Region 1 AR 3 2020-03-04 2020-03-07 3 70 63.7 28.5 0
#> 4 Region 1 AR 3 2020-03-04 2020-03-08 4 62.5 68.6 36.6 0
#> 5 Region 1 AR 3 2020-03-04 2020-03-09 5 64.5 74.8 41.3 28
#> 6 Region 1 AR 3 2020-03-04 2020-03-10 6 71 80.2 58.4 0
#> 7 Region 1 AR 3 2020-03-04 2020-03-11 7 81.5 91.8 69.5 0
#> 8 Region 1 AR 3 2020-03-05 2020-03-06 1 65 66.6 7.63 57
#> 9 Region 1 AR 3 2020-03-05 2020-03-07 2 83 78.3 9.52 59
#> 10 Region 1 AR 3 2020-03-05 2020-03-08 3 89.5 90.1 8.24 76
#> # … with 619 more rows, and 3 more variables: lower <dbl>, upper <dbl>,
#> # top <dbl>
#> # A tibble: 601 x 12
#> timeseries model forecast_date date horizon median mean sd
#> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 2020-03-06 2020-03-07 1 92 93.9 9.34
#> 2 Region 1 AR 3 2020-03-06 2020-03-08 2 112. 110. 8.92
#> 3 Region 1 AR 3 2020-03-06 2020-03-09 3 131 127. 10.9
#> 4 Region 1 AR 3 2020-03-06 2020-03-10 4 150 150. 11.4
#> 5 Region 1 AR 3 2020-03-06 2020-03-11 5 163 167. 13.5
#> 6 Region 1 AR 3 2020-03-06 2020-03-12 6 194. 194 11.1
#> 7 Region 1 AR 3 2020-03-06 2020-03-13 7 227 230 16.9
#> 8 Region 1 AR 3 2020-03-07 2020-03-08 1 117 118. 10.8
#> 9 Region 1 AR 3 2020-03-07 2020-03-09 2 128 125. 11.2
#> 10 Region 1 AR 3 2020-03-07 2020-03-10 3 151 150. 12.6
#> # … with 591 more rows, and 4 more variables: bottom <dbl>, lower <dbl>,
#> # upper <dbl>, top <dbl>
#>
#> $case_scores
#> # A tibble: 629 x 11
#> timeseries model sample forecast_date date horizon dss crps logs
#> <chr> <chr> <chr> <chr> <date> <int> <dbl> <dbl> <dbl>
#> 1 Region 1 AR 3 1 2020-03-04 2020-03-05 1 5.90 5.07 3.78
#> 2 Region 1 AR 3 1 2020-03-04 2020-03-06 2 6.44 8.47 4.09
#> 3 Region 1 AR 3 1 2020-03-04 2020-03-07 3 7.40 12.8 4.40
#> 4 Region 1 AR 3 1 2020-03-04 2020-03-08 4 8.02 18.8 4.88
#> 5 Region 1 AR 3 1 2020-03-04 2020-03-09 5 8.44 23.6 5.07
#> 6 Region 1 AR 3 1 2020-03-04 2020-03-10 6 9.25 34.7 5.45
#> 7 Region 1 AR 3 1 2020-03-04 2020-03-11 7 9.68 41.5 5.59
#> 8 Region 1 AR 3 1 2020-03-05 2020-03-06 1 4.74 4.38 3.57
#> 9 Region 1 AR 3 1 2020-03-05 2020-03-07 2 5.55 4.89 3.42
#> 10 Region 1 AR 3 1 2020-03-05 2020-03-08 3 6.43 7.49 3.76
#> # … with 619 more rows, and 2 more variables: bias <dbl>, sharpness <dbl>
#> # A tibble: 601 x 15
#> timeseries model sample forecast_date date horizon dss crps
#> <chr> <chr> <chr> <chr> <date> <int> <dbl> <dbl>
#> 1 Region 1 AR 3 1 2020-03-06 2020-03-07 1 4.81 3.39
#> 2 Region 1 AR 3 1 2020-03-06 2020-03-08 2 5.26 5.5
#> 3 Region 1 AR 3 1 2020-03-06 2020-03-09 3 5.87 7.87
#> 4 Region 1 AR 3 1 2020-03-06 2020-03-10 4 5.46 5.33
#> 5 Region 1 AR 3 1 2020-03-06 2020-03-11 5 5.10 3.83
#> 6 Region 1 AR 3 1 2020-03-06 2020-03-12 6 4.71 3.86
#> 7 Region 1 AR 3 1 2020-03-06 2020-03-13 7 7.44 13.5
#> 8 Region 1 AR 3 1 2020-03-07 2020-03-08 1 7.20 10.6
#> 9 Region 1 AR 3 1 2020-03-07 2020-03-09 2 5.51 7.48
#> 10 Region 1 AR 3 1 2020-03-07 2020-03-10 3 5.53 6.19
#> # … with 591 more rows, and 7 more variables: logs <dbl>, bias <dbl>,
#> # sharpness <dbl>, calibration <dbl>, median <dbl>, iqr <dbl>, ci <dbl>
```

- Plot an evaluation of Rt forecasts using iterative
Expand Down Expand Up @@ -182,24 +186,20 @@ EpiSoon::plot_forecast_evaluation(forecasts$forecast_cases, obs_cases, c(7)) +

``` r
EpiSoon::summarise_scores(forecasts$case_scores)
#> # A tibble: 15 x 9
#> score model bottom lower median mean upper top sd
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 bias AR 3 0 0 0.100 0.256 0.4 1 0.354
#> 2 bias ARIMA 0 0 0.0500 0.236 0.4 1 0.342
#> 3 bias Semi-local linear t… 0 0 0.100 0.293 0.6 1 0.354
#> 4 crps AR 3 4.08 10.9 19.7 35.1 37.3 159. 39.9
#> 5 crps ARIMA 3.06 9.12 21.5 35.2 39.4 157. 39.7
#> 6 crps Semi-local linear t… 2.95 9.01 20.0 32.8 35.7 170. 39.9
#> 7 dss AR 3 5.02 6.89 9.48 15.8 16.6 69.1 17.3
#> 8 dss ARIMA 4.57 6.57 9.57 16.9 17.0 78.5 19.9
#> 9 dss Semi-local linear t… 4.53 6.70 8.57 13.3 13.0 64.5 15.1
#> 10 logs AR 3 3.52 4.52 5.22 14.4 9.26 62.7 44.5
#> 11 logs ARIMA 3.35 4.24 5.25 12.4 9.29 76.9 19.1
#> 12 logs Semi-local linear t… 3.20 4.28 5.06 9.59 6.67 60.1 15.7
#> 13 sharpness AR 3 4.60 10.4 15.6 18.8 23.7 51.0 13.1
#> 14 sharpness ARIMA 4.45 9.64 14.1 15.6 19.8 33.2 7.78
#> 15 sharpness Semi-local linear t… 5.36 11.1 16.3 20.4 24.5 55.3 15.1
#> # A tibble: 27 x 9
#> score model bottom lower median mean upper top sd
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 bias AR 3 -1.00e+0 2.00e-1 6.00e-1 4.88e-1 1.00e+0 1.00e+0 5.67e-1
#> 2 bias ARIMA -1.00e+0 2.00e-1 8.00e-1 5.10e-1 1.00e+0 1.00e+0 6.17e-1
#> 3 bias Semi-l… -1.00e+0 2.00e-1 8.00e-1 5.19e-1 1.00e+0 1.00e+0 5.76e-1
#> 4 calib… AR 3 8.57e-5 8.57e-5 8.57e-5 3.22e-2 1.50e-4 5.67e-1 1.13e-1
#> 5 calib… ARIMA 8.57e-5 8.57e-5 8.57e-5 3.29e-2 1.50e-4 3.75e-1 9.60e-2
#> 6 calib… Semi-l… 8.57e-5 8.57e-5 8.57e-5 7.96e-2 2.00e-4 7.80e-1 1.99e-1
#> 7 ci AR 3 2.89e+1 4.71e+1 1.10e+2 1.14e+3 1.92e+3 5.50e+3 1.79e+3
#> 8 ci ARIMA 2.50e+1 4.54e+1 1.13e+2 1.04e+3 1.56e+3 6.01e+3 1.71e+3
#> 9 ci Semi-l… 2.69e+1 5.14e+1 1.14e+2 9.40e+2 1.16e+3 5.93e+3 1.58e+3
#> 10 crps AR 3 2.97e+0 6.26e+0 1.72e+1 4.47e+1 7.62e+1 1.73e+2 5.53e+1
#> # … with 17 more rows
```

## Docker
Expand Down
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
codecov:
token: 3b555487-3534-4ebf-9985-546286a42ed9

4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library("testthat")
library("EpiSoon")

test_check("EpiSoon")
52 changes: 52 additions & 0 deletions tests/testthat/test_compare_models.R
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)
})
58 changes: 58 additions & 0 deletions tests/testthat/test_compare_timeseries.R
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)
})
17 changes: 17 additions & 0 deletions tests/testthat/test_draw_from_si_prob.R
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)
})
60 changes: 60 additions & 0 deletions tests/testthat/test_evaluate_model.R
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"))

})
Loading