[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 4.361257e+04 5.179637e+04 3.842355e+04
#>  2: 2020-04-10 case          case 4.480164e+04 5.284711e+04 3.900482e+04
#>  3: 2020-04-11 case          case 4.378107e+04 5.091242e+04 3.785541e+04
#>  4: 2020-04-12 case          case 4.704245e+04 5.268693e+04 3.809546e+04
#>  5: 2020-04-13 case          case 4.496785e+04 5.081469e+04 3.762873e+04
#>  6: 2020-04-14 case          case 4.163278e+04 5.002142e+04 3.827178e+04
#>  7: 2020-04-15 case          case 4.336286e+04 5.025689e+04 3.782066e+04
#>  8: 2020-04-16 case          case 4.736932e+04 5.419490e+04 3.938636e+04
#>  9: 2020-04-17 case          case 4.713655e+04 5.382560e+04 3.781845e+04
#> 10: 2020-04-18 case          case 4.544226e+04 5.404890e+04 4.055475e+04
#> 11: 2020-04-19 case          case 4.299618e+04 5.201827e+04 3.928332e+04
#> 12: 2020-04-20 case          case 4.735210e+04 5.639164e+04 4.097216e+04
#> 13: 2020-04-21 case          case 4.677755e+04 5.409947e+04 3.851058e+04
#> 14: 2020-04-22 case          case 4.540849e+04 5.260037e+04 3.867794e+04
#> 15: 2020-04-09 case      ensemble 4.747432e+04 5.100040e+04 2.675306e+04
#> 16: 2020-04-10 case      ensemble 4.781060e+04 5.195134e+04 2.814292e+04
#> 17: 2020-04-11 case      ensemble 4.510429e+04 5.027671e+04 2.621818e+04
#> 18: 2020-04-12 case      ensemble 4.930932e+04 5.175284e+04 2.616195e+04
#> 19: 2020-04-13 case      ensemble 4.689482e+04 5.016650e+04 2.615128e+04
#> 20: 2020-04-14 case      ensemble 4.696660e+04 5.062006e+04 2.709657e+04
#> 21: 2020-04-15 case      ensemble 4.715109e+04 5.092776e+04 2.687568e+04
#> 22: 2020-04-16 case      ensemble 4.911730e+04 5.188285e+04 2.678934e+04
#> 23: 2020-04-17 case      ensemble 4.877508e+04 5.206228e+04 2.732457e+04
#> 24: 2020-04-18 case      ensemble 4.818205e+04 5.259961e+04 2.926484e+04
#> 25: 2020-04-19 case      ensemble 4.706834e+04 5.064188e+04 2.723562e+04
#> 26: 2020-04-20 case      ensemble 5.085733e+04 5.360783e+04 2.718269e+04
#> 27: 2020-04-21 case      ensemble 4.980664e+04 5.273912e+04 2.786295e+04
#> 28: 2020-04-22 case      ensemble 4.857368e+04 5.131235e+04 2.665171e+04
#> 29: 2020-04-09 case            rt 4.251600e+04 5.020442e+04 3.695308e+04
#> 30: 2020-04-10 case            rt 4.116250e+04 5.105558e+04 3.927718e+04
#> 31: 2020-04-11 case            rt 4.350750e+04 4.964101e+04 3.582798e+04
#> 32: 2020-04-12 case            rt 4.402150e+04 5.081875e+04 3.840132e+04
#> 33: 2020-04-13 case            rt 4.265750e+04 4.951830e+04 3.543359e+04
#> 34: 2020-04-14 case            rt 4.366500e+04 5.121870e+04 3.799832e+04
#> 35: 2020-04-15 case            rt 4.490400e+04 5.159863e+04 3.839141e+04
#> 36: 2020-04-16 case            rt 4.391300e+04 4.957079e+04 3.687296e+04
#> 37: 2020-04-17 case            rt 4.175000e+04 5.029896e+04 3.891627e+04
#> 38: 2020-04-18 case            rt 4.272900e+04 5.115032e+04 3.965696e+04
#> 39: 2020-04-19 case            rt 4.147850e+04 4.926549e+04 3.859816e+04
#> 40: 2020-04-20 case            rt 4.447100e+04 5.082401e+04 3.692910e+04
#> 41: 2020-04-21 case            rt 4.498350e+04 5.137877e+04 3.820308e+04
#> 42: 2020-04-22 case            rt 4.283200e+04 5.002434e+04 3.616813e+04
#> 43: 2020-04-09   rt          <NA> 7.346717e-01 7.180234e-01 2.933906e-01
#> 44: 2020-04-10   rt          <NA> 7.171033e-01 7.253447e-01 2.930715e-01
#> 45: 2020-04-11   rt          <NA> 7.099495e-01 7.147906e-01 3.026072e-01
#> 46: 2020-04-12   rt          <NA> 7.223387e-01 7.380598e-01 3.051750e-01
#> 47: 2020-04-13   rt          <NA> 7.288442e-01 7.260374e-01 3.056252e-01
#> 48: 2020-04-14   rt          <NA> 7.081314e-01 7.154773e-01 3.028979e-01
#> 49: 2020-04-15   rt          <NA> 7.163385e-01 7.265538e-01 3.110362e-01
#> 50: 2020-04-16   rt          <NA> 7.159376e-01 7.163027e-01 2.989394e-01
#> 51: 2020-04-17   rt          <NA> 7.302236e-01 7.241897e-01 3.081601e-01
#> 52: 2020-04-18   rt          <NA> 6.993316e-01 7.120140e-01 3.122708e-01
#> 53: 2020-04-19   rt          <NA> 7.032378e-01 7.096514e-01 3.104571e-01
#> 54: 2020-04-20   rt          <NA> 7.348019e-01 7.389002e-01 3.163051e-01
#> 55: 2020-04-21   rt          <NA> 7.040942e-01 7.072294e-01 3.194599e-01
#> 56: 2020-04-22   rt          <NA> 7.063611e-01 7.139628e-01 2.875781e-01
#>           date type forecast_type       median         mean           sd
#>         lower_90     lower_50     lower_20     upper_20     upper_50
#>  1: 5.129176e+03 2.236827e+04 3.528216e+04 5.468599e+04 7.311445e+04
#>  2: 5.119948e+03 2.189066e+04 3.524490e+04 5.596248e+04 7.477928e+04
#>  3: 4.886382e+03 2.031358e+04 3.341203e+04 5.418889e+04 7.395538e+04
#>  4: 5.114885e+03 2.290748e+04 3.647069e+04 5.598601e+04 7.504978e+04
#>  5: 4.497218e+03 2.004449e+04 3.400409e+04 5.551702e+04 7.279119e+04
#>  6: 4.720321e+03 2.022811e+04 3.333523e+04 5.353067e+04 7.180133e+04
#>  7: 3.430744e+03 1.982409e+04 3.288970e+04 5.321976e+04 7.350174e+04
#>  8: 5.310500e+03 2.245703e+04 3.655429e+04 5.821979e+04 7.845383e+04
#>  9: 3.939762e+03 2.432756e+04 3.726695e+04 5.780278e+04 7.731172e+04
#> 10: 4.066561e+03 2.265234e+04 3.536220e+04 5.721156e+04 7.609471e+04
#> 11: 4.629057e+03 2.199569e+04 3.416892e+04 5.252650e+04 7.215512e+04
#> 12: 4.606789e+03 2.370075e+04 3.909501e+04 5.996162e+04 8.026918e+04
#> 13: 5.788834e+03 2.329423e+04 3.668288e+04 5.723181e+04 7.856556e+04
#> 14: 4.431385e+03 2.167032e+04 3.526818e+04 5.617901e+04 7.525352e+04
#> 15: 1.297745e+04 3.113864e+04 4.096077e+04 5.470448e+04 6.801334e+04
#> 16: 1.357227e+04 3.183623e+04 4.115922e+04 5.457116e+04 6.913294e+04
#> 17: 1.366846e+04 3.115814e+04 3.941232e+04 5.310812e+04 6.721022e+04
#> 18: 1.393966e+04 3.288385e+04 4.200644e+04 5.578969e+04 6.846140e+04
#> 19: 1.313930e+04 3.069082e+04 4.064896e+04 5.297504e+04 6.767319e+04
#> 20: 1.288592e+04 3.055990e+04 4.089123e+04 5.441642e+04 6.637021e+04
#> 21: 1.318944e+04 3.051531e+04 4.033115e+04 5.372112e+04 6.836773e+04
#> 22: 1.342721e+04 3.135147e+04 4.240787e+04 5.648501e+04 6.815526e+04
#> 23: 1.408098e+04 3.193701e+04 4.190982e+04 5.613876e+04 6.813042e+04
#> 24: 1.214271e+04 3.124942e+04 4.139826e+04 5.590830e+04 6.998525e+04
#> 25: 1.273793e+04 3.087451e+04 4.097213e+04 5.285712e+04 6.467573e+04
#> 26: 1.439047e+04 3.313735e+04 4.404855e+04 5.731895e+04 7.048284e+04
#> 27: 1.317511e+04 3.132226e+04 4.141118e+04 5.717922e+04 7.122244e+04
#> 28: 1.452419e+04 3.103731e+04 4.112941e+04 5.529128e+04 6.836662e+04
#> 29: 4.135050e+03 2.130650e+04 3.371480e+04 5.518300e+04 7.156575e+04
#> 30: 4.539800e+03 2.030025e+04 3.370280e+04 5.331880e+04 7.363075e+04
#> 31: 3.924750e+03 2.109800e+04 3.360540e+04 5.303700e+04 7.029975e+04
#> 32: 3.423550e+03 1.872025e+04 3.325080e+04 5.496920e+04 7.413125e+04
#> 33: 4.293600e+03 2.237275e+04 3.464500e+04 5.229920e+04 6.966650e+04
#> 34: 4.249750e+03 2.076000e+04 3.311660e+04 5.525700e+04 7.660125e+04
#> 35: 4.536250e+03 2.158000e+04 3.432860e+04 5.394360e+04 7.396150e+04
#> 36: 4.112700e+03 1.875775e+04 3.378120e+04 5.420300e+04 7.134300e+04
#> 37: 3.786650e+03 1.951000e+04 3.361880e+04 5.155320e+04 7.122975e+04
#> 38: 4.365200e+03 1.966450e+04 3.261240e+04 5.371360e+04 7.403325e+04
#> 39: 3.345400e+03 1.790500e+04 3.077200e+04 5.181640e+04 7.181175e+04
#> 40: 4.486600e+03 2.235775e+04 3.546080e+04 5.390320e+04 7.264475e+04
#> 41: 3.373400e+03 2.035550e+04 3.501260e+04 5.608780e+04 7.232250e+04
#> 42: 5.725450e+03 2.248850e+04 3.401180e+04 5.143880e+04 7.071550e+04
#> 43: 2.213965e-01 5.206046e-01 6.410478e-01 8.048943e-01 9.106870e-01
#> 44: 2.471009e-01 5.167841e-01 6.506494e-01 7.927585e-01 9.239287e-01
#> 45: 2.361419e-01 5.016504e-01 6.275936e-01 7.886030e-01 9.305690e-01
#> 46: 2.449260e-01 5.305145e-01 6.629040e-01 8.142274e-01 9.351464e-01
#> 47: 2.361393e-01 5.030309e-01 6.413610e-01 8.060481e-01 9.367482e-01
#> 48: 2.336488e-01 4.976808e-01 6.390799e-01 7.808313e-01 9.066795e-01
#> 49: 2.196549e-01 5.038446e-01 6.393382e-01 8.137787e-01 9.382221e-01
#> 50: 2.118239e-01 5.026028e-01 6.384293e-01 7.907604e-01 9.259808e-01
#> 51: 2.237361e-01 5.060775e-01 6.488362e-01 7.986359e-01 9.225689e-01
#> 52: 2.026497e-01 4.893287e-01 6.352176e-01 7.789750e-01 9.102513e-01
#> 53: 2.097345e-01 4.818580e-01 6.312687e-01 7.872157e-01 9.037933e-01
#> 54: 2.366761e-01 5.257139e-01 6.502566e-01 8.144813e-01 9.514994e-01
#> 55: 1.884562e-01 4.829311e-01 6.220819e-01 7.927253e-01 9.313974e-01
#> 56: 2.536168e-01 5.111523e-01 6.267246e-01 7.873295e-01 9.172038e-01
#>         lower_90     lower_50     lower_20     upper_20     upper_50
#>         upper_90
#>  1: 1.257240e+05
#>  2: 1.313576e+05
#>  3: 1.240567e+05
#>  4: 1.268394e+05
#>  5: 1.224297e+05
#>  6: 1.237614e+05
#>  7: 1.209838e+05
#>  8: 1.264614e+05
#>  9: 1.246358e+05
#> 10: 1.337441e+05
#> 11: 1.268132e+05
#> 12: 1.360518e+05
#> 13: 1.271142e+05
#> 14: 1.239280e+05
#> 15: 9.883630e+04
#> 16: 1.039497e+05
#> 17: 9.531901e+04
#> 18: 9.977934e+04
#> 19: 9.865056e+04
#> 20: 9.961695e+04
#> 21: 9.989504e+04
#> 22: 1.018635e+05
#> 23: 1.010113e+05
#> 24: 1.059634e+05
#> 25: 1.017310e+05
#> 26: 1.058011e+05
#> 27: 1.017387e+05
#> 28: 9.949105e+04
#> 29: 1.183995e+05
#> 30: 1.299536e+05
#> 31: 1.156523e+05
#> 32: 1.264861e+05
#> 33: 1.173846e+05
#> 34: 1.229697e+05
#> 35: 1.236902e+05
#> 36: 1.201061e+05
#> 37: 1.252907e+05
#> 38: 1.240740e+05
#> 39: 1.247325e+05
#> 40: 1.228692e+05
#> 41: 1.265890e+05
#> 42: 1.185848e+05
#> 43: 1.179072e+00
#> 44: 1.207291e+00
#> 45: 1.208875e+00
#> 46: 1.259279e+00
#> 47: 1.235606e+00
#> 48: 1.254393e+00
#> 49: 1.265936e+00
#> 50: 1.205821e+00
#> 51: 1.241248e+00
#> 52: 1.232185e+00
#> 53: 1.261422e+00
#> 54: 1.292689e+00
#> 55: 1.242198e+00
#> 56: 1.176025e+00
#>         upper_90
# }