Getting started

Introduction

This vignette briefly outlines the functionality of EpiSoon. To get started load the required packages.

  • Load the package (bsts for models, ggplot2 for plotting, and cowplot for theming)
library(EpiSoon)
library(bsts)
library(fable)
library(cowplot)
library(dplyr)

Forecast Rts, score and plot

  • We use an example dataframe built into the package but this could be replaced with your own data.
EpiSoon::example_obs_rts
#>          rt       date
#> 1  2.490547 2020-03-01
#> 2  2.442588 2020-03-02
#> 3  2.402473 2020-03-03
#> 4  2.335572 2020-03-04
#> 5  2.266551 2020-03-05
#> 6  2.192293 2020-03-06
#> 7  2.146429 2020-03-07
#> 8  2.104371 2020-03-08
#> 9  2.059281 2020-03-09
#> 10 2.027134 2020-03-10
#> 11 2.014678 2020-03-11
#> 12 1.998946 2020-03-12
#> 13 1.968350 2020-03-13
#> 14 1.947376 2020-03-14
#> 15 1.906984 2020-03-15
#> 16 1.812842 2020-03-16
#> 17 1.718532 2020-03-17
#> 18 1.665646 2020-03-18
#> 19 1.639927 2020-03-19
#> 20 1.633795 2020-03-20
#> 21 1.682025 2020-03-21
#> 22 1.561653 2020-03-22
  • Fit a bsts model and produce a Rt forecast. Any appropriately wrapped model can be used (see bsts_model and fable_model for an examples).
rt_forecast <- forecast_rt(EpiSoon::example_obs_rts[1:10, ],
  model = function(...) {
    EpiSoon::bsts_model(model = function(ss, y) {
      bsts::AddAutoAr(ss, y = y, lags = 10)
    }, ...)
  },
  horizon = 21, samples = 10
)

rt_forecast
#> # A tibble: 210 × 4
#>    sample date          rt horizon
#>     <int> <date>     <dbl>   <int>
#>  1      1 2020-03-11  1.99       1
#>  2      2 2020-03-11  2.04       1
#>  3      3 2020-03-11  2.07       1
#>  4      4 2020-03-11  2.02       1
#>  5      5 2020-03-11  1.96       1
#>  6      6 2020-03-11  1.96       1
#>  7      7 2020-03-11  2.02       1
#>  8      8 2020-03-11  2.00       1
#>  9      9 2020-03-11  1.83       1
#> 10     10 2020-03-11  1.99       1
#> # ℹ 200 more rows
  • Score the forecast
rt_scores <- score_forecast(rt_forecast, EpiSoon::example_obs_rts)

rt_scores
#>           date horizon  bias       dss       crps overprediction
#>         <Date>   <int> <num>     <num>      <num>          <num>
#>  1: 2020-03-11       1  -0.2 -5.363075 0.01304641    0.000000000
#>  2: 2020-03-12       2  -0.2 -5.162229 0.01827786    0.000000000
#>  3: 2020-03-13       3  -0.2 -4.698277 0.02986434    0.000000000
#>  4: 2020-03-14       4  -0.2 -3.999729 0.03924945    0.000000000
#>  5: 2020-03-15       5  -0.4 -3.572357 0.04060515    0.000000000
#>  6: 2020-03-16       6   0.0 -3.273513 0.03998551    0.000000000
#>  7: 2020-03-17       7   0.4 -3.386261 0.05509806    0.022578835
#>  8: 2020-03-18       8   0.6 -3.160260 0.06194846    0.019207096
#>  9: 2020-03-19       9   0.4 -2.926133 0.04677508    0.008609668
#> 10: 2020-03-20      10   0.4 -2.722950 0.04757243    0.004356772
#> 11: 2020-03-21      11  -0.4 -2.340355 0.06887580    0.000000000
#> 12: 2020-03-22      12   0.0 -2.718748 0.05717023    0.000000000
#>     underprediction dispersion  log_score        mad   ae_median      se_mean
#>               <num>      <num>      <num>      <num>       <num>        <num>
#>  1:     0.002377442 0.01066897 -2.0774358 0.04323000 0.017981603 7.829967e-04
#>  2:     0.004632800 0.01364506 -1.7589100 0.04314459 0.028568192 9.174989e-05
#>  3:     0.004248752 0.02561559 -1.2158659 0.07756382 0.047360957 1.561421e-03
#>  4:     0.008527637 0.03072181 -1.0031550 0.13336061 0.055789766 3.159335e-03
#>  5:     0.002637505 0.03796764 -0.8249299 0.15191385 0.045684509 3.713441e-03
#>  6:     0.000000000 0.03998551 -0.8603096 0.14328018 0.001556137 6.019349e-08
#>  7:     0.000000000 0.03251922 -0.6775674 0.12662370 0.081791662 2.784230e-03
#>  8:     0.000000000 0.04274137 -0.5618038 0.13661001 0.080836530 3.888844e-03
#>  9:     0.000000000 0.03816541 -0.9532092 0.09906242 0.044291705 7.045062e-06
#> 10:     0.000000000 0.04321565 -0.7985499 0.13424429 0.037830408 3.435985e-04
#> 11:     0.022156639 0.04671916 -0.5383332 0.18884677 0.086976854 1.798776e-02
#> 12:     0.000000000 0.05717023 -0.4070032 0.24716202 0.004876765 1.035496e-03
  • Summarise the forecast scores
summarise_scores(rt_scores)
#> # A tibble: 10 × 8
#>    score              bottom    lower   median     mean    upper     top      sd
#>    <chr>               <dbl>    <dbl>    <dbl>    <dbl>    <dbl>   <dbl>   <dbl>
#>  1 ae_median         2.47e-3  2.59e-2  0.0450   0.0445   0.0621   0.0856 0.0287 
#>  2 bias             -4   e-1 -2   e-1 -0.1      0.0167   0.4      0.545  0.346  
#>  3 crps              1.45e-2  3.69e-2  0.0437   0.0432   0.0556   0.0670 0.0168 
#>  4 dispersion        1.15e-2  2.94e-2  0.0381   0.0349   0.0429   0.0543 0.0134 
#>  5 dss              -5.31e+0 -4.17e+0 -3.33    -3.61    -2.88    -2.44   0.993  
#>  6 log_score        -1.99e+0 -1.06e+0 -0.843   -0.973   -0.649   -0.443  0.498  
#>  7 mad               4.32e-2  9.37e-2  0.134    0.127    0.145    0.231  0.0577 
#>  8 overprediction    0        0        0        0.00456  0.00542  0.0217 0.00810
#>  9 se_mean           1.98e-6  2.81e-4  0.00130  0.00295  0.00330  0.0141 0.00496
#> 10 underprediction   0        0        0.00119  0.00372  0.00434  0.0184 0.00640
  • Summarise the forecast
summarised_rt_forecast <- summarise_forecast(rt_forecast)

summarised_rt_forecast
#> # A tibble: 21 × 9
#>    date       horizon median  mean     sd bottom lower upper   top
#>    <date>       <int>  <dbl> <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>
#>  1 2020-03-11       1   2.00  1.99 0.0651  1.83   1.99  2.04  2.07
#>  2 2020-03-12       2   1.97  1.99 0.0791  1.85   1.95  2.01  2.15
#>  3 2020-03-13       3   1.92  1.93 0.0905  1.76   1.87  1.98  2.06
#>  4 2020-03-14       4   1.89  1.89 0.128   1.62   1.83  1.98  2.04
#>  5 2020-03-15       5   1.86  1.85 0.164   1.49   1.75  1.90  2.06
#>  6 2020-03-16       6   1.81  1.81 0.205   1.37   1.76  1.94  2.12
#>  7 2020-03-17       7   1.80  1.77 0.185   1.38   1.68  1.85  2.04
#>  8 2020-03-18       8   1.75  1.73 0.206   1.31   1.68  1.87  1.99
#>  9 2020-03-19       9   1.68  1.64 0.244   1.10   1.63  1.76  1.93
#> 10 2020-03-20      10   1.67  1.62 0.269   0.989  1.57  1.75  1.92
#> # ℹ 11 more rows
  • Plot the forecast against observed data
plot_forecast(summarised_rt_forecast, EpiSoon::example_obs_rts)

Forecast cases, score and plot

  • Forecasting cases requires the observed cases on which the observed Rt estimates were based
EpiSoon::example_obs_cases
#> # A tibble: 63 × 2
#>    cases date      
#>    <dbl> <date>    
#>  1     1 2020-01-20
#>  2     0 2020-01-21
#>  3     1 2020-01-22
#>  4     0 2020-01-23
#>  5     0 2020-01-24
#>  6     0 2020-01-25
#>  7     1 2020-01-26
#>  8     0 2020-01-27
#>  9     0 2020-01-28
#> 10     0 2020-01-29
#> # ℹ 53 more rows
  • It also requires an assumption to be made about the serial interval (defined using probability distribution).
EpiSoon::example_serial_interval
#>         1    2    3    4    5    6    7    8    9   10   11   12   14 
#> 0.00 0.03 0.25 0.17 0.09 0.15 0.13 0.05 0.05 0.03 0.02 0.01 0.01 0.01
  • Forecast cases (using the case data on which the observed Rt estimates were based)
case_forecast <- forecast_cases(EpiSoon::example_obs_cases, rt_forecast,
  serial_interval = EpiSoon::example_serial_interval
)

case_forecast
#>      sample       date cases horizon
#>       <num>     <Date> <int>   <int>
#>   1:      1 2020-03-11   179       1
#>   2:      1 2020-03-12   217       2
#>   3:      1 2020-03-13   224       3
#>   4:      1 2020-03-14   295       4
#>   5:      1 2020-03-15   305       5
#>  ---                                
#> 206:     10 2020-03-27  1068      17
#> 207:     10 2020-03-28  1130      18
#> 208:     10 2020-03-29  1284      19
#> 209:     10 2020-03-30  1307      20
#> 210:     10 2020-03-31  1369      21
  • Score the cases forecast
case_scores <- score_case_forecast(case_forecast, EpiSoon::example_obs_cases)
#> Warning: Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.

case_scores
#>           date horizon  bias       dss   crps overprediction underprediction
#>         <Date>   <int> <num>     <num>  <num>          <num>           <num>
#>  1: 2020-03-11       1   0.4  4.465754   3.19            1.4               0
#>  2: 2020-03-12       2   0.4  4.951843   3.15            1.0               0
#>  3: 2020-03-13       3   0.6  6.958252  10.33            4.4               0
#>  4: 2020-03-14       4   0.0  6.902583  10.14            0.0               0
#>  5: 2020-03-15       5   0.6  8.083712  26.68           19.4               0
#>  6: 2020-03-16       6   0.8 10.195810  59.64           43.6               0
#>  7: 2020-03-17       7   0.8 11.158880  79.68           60.0               0
#>  8: 2020-03-18       8   0.8 10.303438  70.93           47.4               0
#>  9: 2020-03-19       9   0.7 10.244333  62.51           33.5               0
#> 10: 2020-03-20      10   0.6 10.627795  73.74           36.2               0
#> 11: 2020-03-21      11   0.2 10.542731  45.31            3.5               0
#> 12: 2020-03-22      12   0.8 12.672750 212.56          151.2               0
#>     dispersion log_score      mad ae_median   se_mean
#>          <num>     <num>    <num>     <num>     <num>
#>  1:       1.79  3.296469   8.8956       5.0     26.01
#>  2:       2.15  3.304055   9.6369       3.0     28.09
#>  3:       5.93  4.304752  24.4629      13.0    342.25
#>  4:      10.14  4.679147  35.5824       9.5    100.00
#>  5:       7.28  5.650684  37.8063      35.0   1169.64
#>  6:      16.04  6.118107  56.3388      83.0   7157.16
#>  7:      19.68  6.246160  83.0256     118.5  13642.24
#>  8:      23.53  6.349741  80.8017     117.5  10712.25
#>  9:      29.01  6.183688 111.9363     112.0   9721.96
#> 10:      37.54  6.454172 151.9665     134.5  11577.76
#> 11:      41.81  6.277563 181.6185      22.5    144.00
#> 12:      61.36  7.290066 274.2810     320.5 101060.41
  • Summarise the cases scores
summarise_scores(case_scores)
#> # A tibble: 10 × 8
#>    score           bottom  lower  median      mean    upper      top        sd
#>    <chr>            <dbl>  <dbl>   <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
#>  1 ae_median        3.55   12.1    59       81.2     118.     269.      91.0  
#>  2 bias             0.055   0.4     0.6      0.558     0.8      0.8      0.261
#>  3 crps             3.16   10.3    52.5     54.8      71.6    176.      57.5  
#>  4 dispersion       1.89    6.94   17.9     21.4      31.1     56.0     18.3  
#>  5 dss              4.60    6.94   10.2      8.93     10.6     12.3      2.60 
#>  6 log_score        3.30    4.59    6.15     5.51      6.30     7.06     1.30 
#>  7 mad              9.10   32.8    68.6     88.0     122.     249.      80.3  
#>  8 overprediction   0.275   2.98   26.4     33.5      44.6    126.      42.7  
#>  9 se_mean         26.6   133    4163.   12973.    10929.   77020.   28245.   
#> 10 underprediction  0       0       0        0         0        0        0
  • Summarise the cases forecast
summarised_case_forecast <- summarise_case_forecast(case_forecast)

summarised_case_forecast
#> # A tibble: 21 × 9
#>    date       horizon median  mean     sd bottom lower upper   top
#>    <date>       <int>  <dbl> <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>
#>  1 2020-03-11       1   172   172.   7.71    158   166   174   184
#>  2 2020-03-12       2   197   199.  11.0     183   190   201   217
#>  3 2020-03-13       3   221   226.  25.5     187   214   240   269
#>  4 2020-03-14       4   260.  261   31.4     207   228   275   299
#>  5 2020-03-15       5   308   307.  39.9     215   305   334   350
#>  6 2020-03-16       6   349   351.  62.7     226   307   371   438
#>  7 2020-03-17       7   414.  413.  76.5     277   350   448   537
#>  8 2020-03-18       8   460.  446.  98.1     248   397   506   570
#>  9 2020-03-19       9   512.  498. 125.      250   399   537   664
#> 10 2020-03-20      10   588.  562. 173.      240   471   642   836
#> # ℹ 11 more rows
  • Plot the forecast against observed case data
plot_forecast(summarised_case_forecast, EpiSoon::example_obs_cases)

Use iterative fitting to explore a forecast

  • To explore the quality of a models forecast it can help to iteratively forecast from each available data point. This is supported in EpiSoon using the following:
it_rt_forecast <- iterative_rt_forecast(EpiSoon::example_obs_rts,
  model = function(...) {
    EpiSoon::bsts_model(model = function(ss, y) {
      bsts::AddAutoAr(ss, y = y, lags = 10)
    }, ...)
  },
  horizon = 7, samples = 10, min_points = 4
)

it_rt_forecast
#> # A tibble: 1,260 × 5
#>    forecast_date sample date          rt horizon
#>    <chr>          <int> <date>     <dbl>   <int>
#>  1 2020-03-05         1 2020-03-06  2.19       1
#>  2 2020-03-05         2 2020-03-06  2.32       1
#>  3 2020-03-05         3 2020-03-06  2.10       1
#>  4 2020-03-05         4 2020-03-06  2.12       1
#>  5 2020-03-05         5 2020-03-06  2.46       1
#>  6 2020-03-05         6 2020-03-06  2.26       1
#>  7 2020-03-05         7 2020-03-06  2.14       1
#>  8 2020-03-05         8 2020-03-06  2.08       1
#>  9 2020-03-05         9 2020-03-06  2.18       1
#> 10 2020-03-05        10 2020-03-06  2.18       1
#> # ℹ 1,250 more rows
  • We can then iteratively forecast cases using the following:
it_cases_forecast <- iterative_case_forecast(
  it_fit_samples = it_rt_forecast,
  cases = EpiSoon::example_obs_cases,
  serial_interval = EpiSoon::example_serial_interval
)

it_cases_forecast
#>       forecast_date sample       date cases horizon
#>              <char>  <num>     <Date> <int>   <int>
#>    1:    2020-03-05      1 2020-03-06    81       1
#>    2:    2020-03-05      1 2020-03-07    84       2
#>    3:    2020-03-05      1 2020-03-08   111       3
#>    4:    2020-03-05      1 2020-03-09   115       4
#>    5:    2020-03-05      1 2020-03-10   104       5
#>   ---                                              
#> 1256:    2020-03-22     10 2020-03-25   875       3
#> 1257:    2020-03-22     10 2020-03-26   940       4
#> 1258:    2020-03-22     10 2020-03-27  1020       5
#> 1259:    2020-03-22     10 2020-03-28  1086       6
#> 1260:    2020-03-22     10 2020-03-29  1314       7
  • All functionality shown above is also supported for iterative forecasting.

Evaluate a model

In real world use we are likely to want to evaluate a model by iteratively forecasting Rts and cases, summarising these forecasts, scoring them and then returning them in a sensible format. These steps are all contained in the evaluate_model function.

model_eval <- evaluate_model(EpiSoon::example_obs_rts,
  EpiSoon::example_obs_cases,
  model = function(...) {
    EpiSoon::bsts_model(model = function(ss, y) {
      bsts::AddAutoAr(ss, y = y, lags = 10)
    }, ...)
  },
  horizon = 21, samples = 10,
  serial_interval = EpiSoon::example_serial_interval
)
#> Warning: Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.
#> Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#> ℹ See the scoringRules package for alternatives for discrete probability
#>   distributions.

model_eval
#> $forecast_rts
#> # A tibble: 399 × 10
#>    forecast_date date       horizon median  mean    sd bottom lower upper   top
#>    <chr>         <date>       <int>  <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>
#>  1 2020-03-04    2020-03-05       1   2.30  2.26 0.150  1.96   2.25  2.40  2.45
#>  2 2020-03-04    2020-03-06       2   2.34  2.26 0.241  1.70   2.21  2.38  2.61
#>  3 2020-03-04    2020-03-07       3   2.27  2.17 0.310  1.37   2.18  2.34  2.50
#>  4 2020-03-04    2020-03-08       4   2.22  2.13 0.381  1.19   2.16  2.40  2.57
#>  5 2020-03-04    2020-03-09       5   2.20  2.07 0.440  1.03   2.00  2.39  2.65
#>  6 2020-03-04    2020-03-10       6   2.18  2.03 0.554  0.730  1.94  2.39  2.78
#>  7 2020-03-04    2020-03-11       7   2.11  1.99 0.553  0.668  1.79  2.16  2.72
#>  8 2020-03-04    2020-03-12       8   2.06  1.94 0.552  0.638  1.72  2.20  2.66
#>  9 2020-03-04    2020-03-13       9   1.92  1.84 0.618  0.453  1.60  2.23  2.66
#> 10 2020-03-04    2020-03-14      10   1.93  1.82 0.630  0.429  1.72  2.33  2.69
#> # ℹ 389 more rows
#> 
#> $rt_scores
#>      forecast_date       date horizon  bias       dss       crps overprediction
#>             <char>     <Date>   <int> <num>     <num>      <num>          <num>
#>   1:    2020-03-04 2020-03-05       1   0.2 -3.899613 0.03645646    0.004641132
#>   2:    2020-03-04 2020-03-06       2   0.4 -2.853650 0.07057901    0.031934223
#>   3:    2020-03-04 2020-03-07       3   0.4 -2.439952 0.07259105    0.027513815
#>   4:    2020-03-04 2020-03-08       4   0.4 -2.032940 0.08829451    0.025898865
#>   5:    2020-03-04 2020-03-09       5   0.2 -1.745572 0.09950706    0.025119252
#>  ---                                                                           
#> 167:    2020-03-19 2020-03-21       2  -0.6 -3.332179 0.06760736    0.000000000
#> 168:    2020-03-19 2020-03-22       3  -0.2 -4.714376 0.02293235    0.000000000
#> 169:    2020-03-20 2020-03-21       1  -0.8 -3.670419 0.05441239    0.000000000
#> 170:    2020-03-20 2020-03-22       2   0.4 -4.675277 0.02726935    0.007306417
#> 171:    2020-03-21 2020-03-22       1   0.6 -4.556906 0.04252718    0.031925675
#>      underprediction dispersion    log_score        mad  ae_median      se_mean
#>                <num>      <num>        <num>      <num>      <num>        <num>
#>   1:     0.000000000 0.03181533 -0.905477104 0.12016873 0.03564905 6.990503e-06
#>   2:     0.000000000 0.03864478 -0.474110903 0.12956637 0.15104379 4.976814e-03
#>   3:     0.000000000 0.04507724 -0.435069793 0.11723384 0.12758795 4.254525e-04
#>   4:     0.000000000 0.06239565 -0.181485870 0.21004538 0.11126139 4.822036e-04
#>   5:     0.000000000 0.07438781 -0.007252354 0.28752782 0.14115912 1.393715e-04
#>  ---                                                                           
#> 167:     0.051287487 0.01631987 -0.785046177 0.06809830 0.10741523 1.158985e-02
#> 168:     0.001934183 0.02099817 -1.409647654 0.08606891 0.01491014 1.156009e-03
#> 169:     0.040216007 0.01419638 -1.077526812 0.04756661 0.07185922 8.073197e-03
#> 170:     0.000000000 0.01996293 -1.453475485 0.07053866 0.03675753 2.911777e-03
#> 171:     0.000000000 0.01060150 -0.785746088 0.03985636 0.06999913 3.703880e-03
#> 
#> $forecast_cases
#> # A tibble: 171 × 10
#>    forecast_date date       horizon median  mean    sd bottom lower upper   top
#>    <chr>         <date>       <int>  <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>
#>  1 2020-03-04    2020-03-05       1   62.5  67.7  11.4     55    55    64    86
#>  2 2020-03-04    2020-03-06       2   84.5  83.5  16.6     59    72    97   105
#>  3 2020-03-04    2020-03-07       3  102.   99    18.2     55    92   108   119
#>  4 2020-03-04    2020-03-08       4  116.  115.   25.4     64   106   139   149
#>  5 2020-03-04    2020-03-09       5  149   142    41.3     53   133   174   198
#>  6 2020-03-04    2020-03-10       6  202   176.   63.5     35   158   210   266
#>  7 2020-03-04    2020-03-11       7  200.  203.   81.5     31   189   279   325
#>  8 2020-03-04    2020-03-12       8  260   247.  105.      33   185   286   417
#>  9 2020-03-04    2020-03-13       9  279   287.  141.      29   180   336   526
#> 10 2020-03-04    2020-03-14      10  358.  344.  189.      16   229   414   702
#> # ℹ 161 more rows
#> 
#> $case_scores
#>      sample forecast_date       date horizon  bias       dss   crps
#>      <char>        <char>     <Date>   <int> <num>     <num>  <num>
#>   1:      1    2020-03-04 2020-03-05       1   0.0  4.946879   2.99
#>   2:      1    2020-03-04 2020-03-06       2   0.3  5.958993   6.61
#>   3:      1    2020-03-04 2020-03-07       3   0.8  6.104728   8.80
#>   4:      1    2020-03-04 2020-03-08       4   0.4  6.648920   9.12
#>   5:      1    2020-03-04 2020-03-09       5   0.5  7.776458  18.32
#>  ---                                                               
#> 167:      1    2020-03-19 2020-03-21       2  -1.0 19.352214 103.38
#> 168:      1    2020-03-19 2020-03-22       3   1.0 18.992173 125.47
#> 169:      1    2020-03-20 2020-03-21       1  -1.0 22.888105 100.15
#> 170:      1    2020-03-20 2020-03-22       2   1.0 38.324010 164.50
#> 171:      1    2020-03-21 2020-03-22       1   1.0 34.658612 179.89
#>      overprediction underprediction dispersion log_score     mad ae_median
#>               <num>           <num>      <num>     <num>   <num>     <num>
#>   1:            0.0             0.0       2.99  3.469587  9.6369       0.5
#>   2:            0.8             0.0       5.81  4.113038 18.5325      11.5
#>   3:            5.0             0.0       3.80  4.164976 11.8608      13.5
#>   4:            2.8             0.0       6.32  4.463926 27.4281      13.5
#>   5:            9.4             0.0       8.92  5.064662 35.5824      33.0
#>  ---                                                                      
#> 167:            0.0            90.9      12.48  9.639450 40.0302     120.5
#> 168:          115.9             0.0       9.57  8.724621 34.0998     162.0
#> 169:            0.0            91.5       8.65 12.500142 45.2193     117.0
#> 170:          155.3             0.0       9.20 20.801172 36.3237     170.5
#> 171:          167.7             0.0      12.19 21.757924 55.5975     200.0
#>       se_mean
#>         <num>
#>   1:    22.09
#>   2:   110.25
#>   3:   121.00
#>   4:   163.84
#>   5:   676.00
#>  ---         
#> 167: 15153.61
#> 168: 22081.96
#> 169: 13595.56
#> 170: 33306.25
#> 171: 40723.24
  • All functionality outlined above can be applied to this output but a special plotting function (plot_forecast_evaluation) is also provided. First evaluate the Rt forecast against observed values.
plot_forecast_evaluation(model_eval$forecast_rts,
  EpiSoon::example_obs_rts,
  horizon_to_plot = 7
)

  • Then evaluate forecast cases against observed values.
plot_forecast_evaluation(model_eval$forecast_cases,
  EpiSoon::example_obs_cases,
  horizon_to_plot = 7
)

Wrapper functions

EpiSoon provides several wrapper functions (compare_models and compare_timeseries). These both wrap evaluate_model and can be used to rapidly explore several forecasting models (compare_models) against multiple time series (compare_timeseries). All lower level summary and plotting functions can be then used with the output of these wrappers to explore the results. See the function documentation for further details.

Supporting generic modelling packages

EpiSoon supports the use of generic forecasting models if they are used in a wrapper that accepts a standardised set of inputs and outputs its forecast in the form the package expects. Examples of model wrappers are those for the bsts and fable packages (bsts_model and fable_model). See the examples and documentation for fable_model for further details.