[Experimental] Provides optional tools for forecasting cases and Rt estimates using the timeseries methods (via the EpiSoon package). It requires the Episoon package. Installation instructions for the EpiSoon package are available here.

forecast_infections(
  infections,
  rts,
  gt_mean,
  gt_sd,
  gt_max = 30,
  ensemble_type = "mean",
  forecast_model,
  CrIs = c(0.2, 0.5, 0.9),
  horizon = 14,
  samples = 1000
)

Arguments

infections

A data frame of cases by date of infection containing the following variables: date, mean, sd

rts

A data frame of Rt estimates by date of infection containing the following variables: date, mean, sd

gt_mean

Numeric, the mean of the gamma distributed generation time.

gt_sd

Numeric, the standard deviation of the gamma distributed generation time.

gt_max

Numeric, the maximum allowed value of the gamma distributed generation time.

ensemble_type

Character string indicating the type of ensemble to use. By default this is an unweighted ensemble ("mean") with no other types currently supported.

forecast_model

An uninitialised forecast model function to be passed to EpiSoon::forecast_rt. Used for forecasting future Rt and case co An example of the required structure is: function(ss, y){bsts::AddSemilocalLinearTrend(ss, y = y)}.

CrIs

Numeric vector of credible intervals to calculate.

horizon

Numeric, defaults to 14. The horizon over which to forecast Rts and cases.

samples

Numeric, the number of forecast samples to take.

Value

A list of data.tables. The first entry ("samples") contains raw forecast samples and the second entry ("summarised") contains summarised forecasts.

Examples

# \donttest{
if (requireNamespace("EpiSoon")) {
  if (requireNamespace("forecastHybrid")) {
    # example case data
    reported_cases <- example_confirmed[1:40]

    generation_time <- get_generation_time(disease = "SARS-CoV-2", source = "ganyani")
    incubation_period <- get_incubation_period(disease = "SARS-CoV-2", source = "lauer")
    reporting_delay <- estimate_delay(rlnorm(100, log(6), 1), max_value = 15)

    # estimate Rt and infections from data
    out <- estimate_infections(reported_cases,
      generation_time = generation_time,
      delays = delay_opts(incubation_period, reporting_delay),
      rt = rt_opts(prior = list(mean = 2, sd = 0.1))
    )

    # forecast Rt and infections from estimates
    forecast <- forecast_infections(
      infections = out$summarised[variable == "infections"],
      rts = out$summarised[variable == "R"],
      gt_mean = out$summarised[variable == "gt_mean"]$mean,
      gt_sd = out$summarised[variable == "gt_sd"]$mean,
      gt_max = 30,
      forecast_model = function(y, ...) {
        EpiSoon::forecastHybrid_model(
          y = y[max(1, length(y) - 21):length(y)],
          model_params = list(models = "aefz", weights = "equal"),
          forecast_params = list(PI.combination = "mean"), ...
        )
      },
      horizon = 14,
      samples = 1000
    )

    forecast$summarised
  }
}
#> Loading required namespace: EpiSoon
#> Loading required namespace: forecastHybrid
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo 
#>           date type forecast_type       median         mean           sd
#>  1: 2020-04-09 case          case 1.328623e+04 1.526362e+04 1.100492e+04
#>  2: 2020-04-10 case          case 1.312148e+04 1.547470e+04 1.150316e+04
#>  3: 2020-04-11 case          case 1.328058e+04 1.512500e+04 1.105934e+04
#>  4: 2020-04-12 case          case 1.343311e+04 1.532035e+04 1.098462e+04
#>  5: 2020-04-13 case          case 1.400122e+04 1.551647e+04 1.113765e+04
#>  6: 2020-04-14 case          case 1.253930e+04 1.472787e+04 1.099979e+04
#>  7: 2020-04-15 case          case 1.347697e+04 1.549162e+04 1.115481e+04
#>  8: 2020-04-16 case          case 1.322231e+04 1.505397e+04 1.070763e+04
#>  9: 2020-04-17 case          case 1.411057e+04 1.623448e+04 1.156023e+04
#> 10: 2020-04-18 case          case 1.424938e+04 1.585553e+04 1.108275e+04
#> 11: 2020-04-19 case          case 1.363458e+04 1.603851e+04 1.163387e+04
#> 12: 2020-04-20 case          case 1.350764e+04 1.577107e+04 1.152231e+04
#> 13: 2020-04-21 case          case 1.371250e+04 1.625611e+04 1.212157e+04
#> 14: 2020-04-22 case          case 1.500980e+04 1.658038e+04 1.118628e+04
#> 15: 2020-04-09 case      ensemble 1.409139e+04 1.494517e+04 8.008943e+03
#> 16: 2020-04-10 case      ensemble 1.426737e+04 1.525648e+04 7.917786e+03
#> 17: 2020-04-11 case      ensemble 1.406606e+04 1.503757e+04 7.938347e+03
#> 18: 2020-04-12 case      ensemble 1.398349e+04 1.475814e+04 7.642117e+03
#> 19: 2020-04-13 case      ensemble 1.405001e+04 1.492695e+04 7.933566e+03
#> 20: 2020-04-14 case      ensemble 1.361979e+04 1.457397e+04 7.607159e+03
#> 21: 2020-04-15 case      ensemble 1.450680e+04 1.508057e+04 7.692125e+03
#> 22: 2020-04-16 case      ensemble 1.336789e+04 1.437338e+04 7.367395e+03
#> 23: 2020-04-17 case      ensemble 1.431087e+04 1.527439e+04 7.760982e+03
#> 24: 2020-04-18 case      ensemble 1.405070e+04 1.493163e+04 7.992529e+03
#> 25: 2020-04-19 case      ensemble 1.432878e+04 1.543773e+04 8.309848e+03
#> 26: 2020-04-20 case      ensemble 1.414389e+04 1.507946e+04 7.664543e+03
#> 27: 2020-04-21 case      ensemble 1.388524e+04 1.509339e+04 8.080626e+03
#> 28: 2020-04-22 case      ensemble 1.428272e+04 1.519292e+04 7.612363e+03
#> 29: 2020-04-09 case            rt 1.219250e+04 1.462671e+04 1.118486e+04
#> 30: 2020-04-10 case            rt 1.295250e+04 1.503826e+04 1.100527e+04
#> 31: 2020-04-11 case            rt 1.355500e+04 1.495014e+04 1.081560e+04
#> 32: 2020-04-12 case            rt 1.162700e+04 1.419592e+04 1.079643e+04
#> 33: 2020-04-13 case            rt 1.202400e+04 1.433742e+04 1.101630e+04
#> 34: 2020-04-14 case            rt 1.249500e+04 1.442007e+04 1.063496e+04
#> 35: 2020-04-15 case            rt 1.308650e+04 1.466952e+04 1.071707e+04
#> 36: 2020-04-16 case            rt 1.156950e+04 1.369279e+04 1.037995e+04
#> 37: 2020-04-17 case            rt 1.218050e+04 1.431430e+04 1.058480e+04
#> 38: 2020-04-18 case            rt 1.141800e+04 1.400772e+04 1.093234e+04
#> 39: 2020-04-19 case            rt 1.229100e+04 1.483695e+04 1.140112e+04
#> 40: 2020-04-20 case            rt 1.251850e+04 1.438784e+04 1.066527e+04
#> 41: 2020-04-21 case            rt 1.178650e+04 1.393068e+04 1.041327e+04
#> 42: 2020-04-22 case            rt 1.166300e+04 1.380547e+04 1.028071e+04
#> 43: 2020-04-09   rt          <NA> 7.473043e-01 7.344477e-01 2.575054e-01
#> 44: 2020-04-10   rt          <NA> 7.321335e-01 7.395055e-01 2.566455e-01
#> 45: 2020-04-11   rt          <NA> 7.331446e-01 7.328683e-01 2.669062e-01
#> 46: 2020-04-12   rt          <NA> 7.397460e-01 7.461538e-01 2.719641e-01
#> 47: 2020-04-13   rt          <NA> 7.480055e-01 7.440355e-01 2.700960e-01
#> 48: 2020-04-14   rt          <NA> 7.197499e-01 7.247891e-01 2.655436e-01
#> 49: 2020-04-15   rt          <NA> 7.418096e-01 7.393449e-01 2.711927e-01
#> 50: 2020-04-16   rt          <NA> 7.413629e-01 7.348353e-01 2.633692e-01
#> 51: 2020-04-17   rt          <NA> 7.388113e-01 7.375265e-01 2.771288e-01
#> 52: 2020-04-18   rt          <NA> 7.092785e-01 7.210759e-01 2.758233e-01
#> 53: 2020-04-19   rt          <NA> 7.271863e-01 7.269151e-01 2.778706e-01
#> 54: 2020-04-20   rt          <NA> 7.442567e-01 7.550453e-01 2.738690e-01
#> 55: 2020-04-21   rt          <NA> 7.088201e-01 7.247661e-01 2.807893e-01
#> 56: 2020-04-22   rt          <NA> 7.271995e-01 7.287053e-01 2.594739e-01
#>           date type forecast_type       median         mean           sd
#>         lower_90     lower_50     lower_20     upper_20     upper_50
#>  1: 1297.3150912 6886.3515690 1.028996e+04 1.644349e+04 2.150408e+04
#>  2: 1260.1193237 6265.0726245 1.048790e+04 1.671558e+04 2.218797e+04
#>  3: 1049.7810363 6501.9547170 1.017096e+04 1.648995e+04 2.160317e+04
#>  4: 1145.5171516 6376.0704197 1.063171e+04 1.617480e+04 2.248762e+04
#>  5: 1330.0651076 6140.2032086 1.081210e+04 1.705161e+04 2.177426e+04
#>  6: 1294.4180771 5843.1980531 1.010858e+04 1.548013e+04 2.132209e+04
#>  7: 1574.9383624 6663.6721688 1.070671e+04 1.657503e+04 2.219918e+04
#>  8: 1252.6322438 6462.8657260 1.009573e+04 1.631495e+04 2.216587e+04
#>  9: 1667.2311446 6993.5045693 1.132675e+04 1.745105e+04 2.297921e+04
#> 10: 1154.8963044 7079.1293597 1.136676e+04 1.747424e+04 2.325732e+04
#> 11: 1163.7722045 6937.5959814 1.115362e+04 1.713342e+04 2.294341e+04
#> 12: 1456.4103832 6800.2421247 1.109033e+04 1.638732e+04 2.194362e+04
#> 13: 1202.0103210 6778.3886122 1.075262e+04 1.663139e+04 2.375126e+04
#> 14: 1397.9761795 7617.0336778 1.217033e+04 1.819869e+04 2.364073e+04
#> 15: 4143.4419250 8909.1607665 1.178654e+04 1.623663e+04 1.946279e+04
#> 16: 3961.7276504 9405.2992514 1.231969e+04 1.640274e+04 2.017053e+04
#> 17: 3784.0822949 9360.7038698 1.206306e+04 1.647222e+04 2.002560e+04
#> 18: 4164.3926087 8707.0680341 1.203610e+04 1.589124e+04 1.971002e+04
#> 19: 3568.6237619 9083.4388266 1.193957e+04 1.606410e+04 1.972236e+04
#> 20: 3943.6224643 8952.9211529 1.169765e+04 1.581420e+04 1.904998e+04
#> 21: 4028.8423725 9291.1820627 1.228444e+04 1.640105e+04 1.978598e+04
#> 22: 3870.4963159 8886.0344687 1.144706e+04 1.543985e+04 1.903750e+04
#> 23: 4171.1707059 9237.2600616 1.241169e+04 1.616283e+04 2.047005e+04
#> 24: 3237.3961752 9061.8886962 1.226723e+04 1.609628e+04 1.976532e+04
#> 25: 3543.7328963 9361.5553170 1.203402e+04 1.675108e+04 2.075025e+04
#> 26: 4040.5583087 9315.2543823 1.219772e+04 1.634553e+04 1.985039e+04
#> 27: 3884.6062518 9026.8481283 1.165672e+04 1.610845e+04 2.060312e+04
#> 28: 4231.3638567 9733.3942692 1.254444e+04 1.611323e+04 2.003883e+04
#> 29: 1173.2000000 5738.5000000 9.471000e+03 1.562880e+04 2.143750e+04
#> 30: 1379.0000000 6347.0000000 1.010380e+04 1.586620e+04 2.170400e+04
#> 31: 1457.3500000 6168.2500000 1.001320e+04 1.607720e+04 2.149425e+04
#> 32: 1201.8000000 5539.2500000 9.209400e+03 1.510460e+04 2.050725e+04
#> 33: 1343.3500000 5779.0000000 9.176600e+03 1.496240e+04 2.043975e+04
#> 34: 1009.5500000 5692.2500000 9.667200e+03 1.569200e+04 2.107325e+04
#> 35: 1076.7000000 5824.0000000 9.824000e+03 1.589660e+04 2.130275e+04
#> 36:  872.7500000 5369.2500000 9.140800e+03 1.436000e+04 1.987250e+04
#> 37: 1449.5000000 6120.7500000 9.487600e+03 1.471980e+04 2.025125e+04
#> 38: 1094.4500000 5213.2500000 8.942000e+03 1.454480e+04 2.041450e+04
#> 39: 1097.7000000 5920.0000000 9.432400e+03 1.580320e+04 2.166350e+04
#> 40: 1160.9000000 5546.0000000 9.573800e+03 1.538840e+04 2.110725e+04
#> 41:  884.6000000 5617.2500000 9.195800e+03 1.477280e+04 1.998575e+04
#> 42: 1230.4500000 5861.2500000 8.959800e+03 1.452720e+04 2.006775e+04
#> 43:    0.3019334    0.5636144 6.717747e-01 8.151083e-01 9.040821e-01
#> 44:    0.3228518    0.5539938 6.804535e-01 8.064716e-01 9.209535e-01
#> 45:    0.2987483    0.5442388 6.613951e-01 7.942065e-01 9.131480e-01
#> 46:    0.2998241    0.5701844 6.780270e-01 8.155874e-01 9.239514e-01
#> 47:    0.3070867    0.5460630 6.759275e-01 8.121537e-01 9.252419e-01
#> 48:    0.2975344    0.5569696 6.605900e-01 7.775236e-01 8.978023e-01
#> 49:    0.2925935    0.5473660 6.684159e-01 8.062630e-01 9.257432e-01
#> 50:    0.2960070    0.5561942 6.631178e-01 8.002053e-01 9.078762e-01
#> 51:    0.2719902    0.5574348 6.735121e-01 8.000335e-01 9.134790e-01
#> 52:    0.2792061    0.5287583 6.441608e-01 7.789840e-01 9.082833e-01
#> 53:    0.2814312    0.5362845 6.515061e-01 7.959780e-01 9.192286e-01
#> 54:    0.3144522    0.5631935 6.818329e-01 8.259631e-01 9.399155e-01
#> 55:    0.2817957    0.5275822 6.431938e-01 7.915297e-01 9.237099e-01
#> 56:    0.2919006    0.5490641 6.596224e-01 7.873443e-01 8.994139e-01
#>         lower_90     lower_50     lower_20     upper_20     upper_50
#>         upper_90
#>  1: 36561.704018
#>  2: 38529.676889
#>  3: 36335.670973
#>  4: 37298.147551
#>  5: 36514.685415
#>  6: 36085.091765
#>  7: 35983.611931
#>  8: 34707.524719
#>  9: 37802.434457
#> 10: 36557.186086
#> 11: 38189.700481
#> 12: 37738.743125
#> 13: 39946.172191
#> 14: 37920.944498
#> 15: 29303.778433
#> 16: 29208.332359
#> 17: 29518.804207
#> 18: 28640.775127
#> 19: 29239.763131
#> 20: 28490.322841
#> 21: 29744.025768
#> 22: 27753.033418
#> 23: 29049.360919
#> 24: 29034.721833
#> 25: 30626.250907
#> 26: 28828.658071
#> 27: 30339.004551
#> 28: 28551.520547
#> 29: 34351.850000
#> 30: 36670.400000
#> 31: 34666.700000
#> 32: 35296.100000
#> 33: 35558.600000
#> 34: 33220.400000
#> 35: 34546.950000
#> 36: 32937.450000
#> 37: 35044.800000
#> 38: 34357.700000
#> 39: 36403.750000
#> 40: 34182.800000
#> 41: 34236.400000
#> 42: 34537.700000
#> 43:     1.134892
#> 44:     1.149815
#> 45:     1.158166
#> 46:     1.191315
#> 47:     1.187739
#> 48:     1.184786
#> 49:     1.174586
#> 50:     1.162415
#> 51:     1.200608
#> 52:     1.183425
#> 53:     1.181375
#> 54:     1.211690
#> 55:     1.214342
#> 56:     1.155207
#>         upper_90
# }