Plots credible intervals and the median from the posterior distribution for the time-varying reproduction rates. The user can control the interval levels (i.e. 30%, 50% etc.) and which groups/regions to plot for. This is a generic function.

plot_rt(object, ...)

# S3 method for class 'epimodel'
plot_rt(
  object,
  groups = NULL,
  step = FALSE,
  dates = NULL,
  date_breaks = "2 weeks",
  date_format = "%Y-%m-%d",
  levels = c(30, 60, 90),
  log = FALSE,
  smooth = 1,
  seeding = FALSE,
  ...
)

spaghetti_rt(
  object,
  draws = min(500, posterior_sample_size(object)),
  alpha = 1/sqrt(draws),
  groups = NULL,
  step = FALSE,
  dates = NULL,
  date_breaks = "2 weeks",
  date_format = "%Y-%m-%d",
  log = FALSE,
  smooth = 1,
  ...
)

Arguments

object

A fitted model object returned by epim. See epimodel-objects.

...

Additional unnamed arguments to be passed to posterior_rt. Examples include newdata, which allows predictions or counterfactuals. adjusted=FALSE prevents application of the population adjustment to the reproduction number.

groups

Either NULL or a character vector specifying the groups to plot for. Default is NULL, which plots all modeled groups.

step

If TRUE, plot the median and credible intervals as a step function.

dates

A length 2 vector of Date objects. This defines the start and end dates of the date-range to be plotted. Must be coercible to Date if not NA. If an element of the vector is NA then the default lower/upper limit is used. See examples.

date_breaks

A string giving the distance between date tick labels. Default is "2 weeks". This is passed as the date_breaks argument to scale_x_date. Please see here for details.

date_format

This function attempts to coerce the dates argument to a vector of Date objects. date_format is passed as the format argument to as.Date. Default is "%Y-%m-%d".

levels

A numeric vector defining the levels of the plotted credible intervals.

log

If TRUE, plot quantities on a log10-scale. This argument must be logical, and defaults to FALSE.

smooth

An integer specifying the window used to smooth the reproduction rates. The default is 1, which corresponds to no smoothing.

seeding

If FALSE (the default), the reproduction rates are not plotted for the seeding period at the start of each group (seed_days in epiinf). Infections are seeded there rather than generated by the renewal equation, so the reproduction rate does not enter the model and its values are draws from the prior.

draws

The number of sample paths to plot.

alpha

Sets transparency of sample paths.

Value

A ggplot object which can be further modified.

Examples

# \donttest{
data("EuropeCovid2")
data <- EuropeCovid2$data
data <- dplyr::filter(data, date > date[which(cumsum(deaths) > 10)[1] - 30])
data <- dplyr::filter(data, date < as.Date("2020-05-05"))

rt <- epirt(
  formula = R(country, date) ~ 0 + (1 + public_events + schools_universities + 
     self_isolating_if_ill + social_distancing_encouraged + lockdown || country) + 
     public_events + schools_universities + self_isolating_if_ill + 
     social_distancing_encouraged + lockdown,
  prior = shifted_gamma(shape=1/6, scale = 1, shift = log(1.05)/6),
  prior_covariance = rstanarm::decov(shape = c(2, rep(0.5, 5)),scale=0.25),
  link = scaled_logit(6.5)
)

inf <- epiinf(gen = EuropeCovid$si, seed_days = 6)

deaths <- epiobs(
  formula = deaths ~ 1,
  i2o = EuropeCovid2$inf2death,
  prior_intercept = rstanarm::normal(0,0.2),
  link = scaled_logit(0.02)
)

args <- list(rt=rt, inf=inf, obs=deaths, data=data, seed=12345)
args$group_subset <- c("Italy", "Austria", "Germany")
args$algorithm <- "fullrank"
args$iter <- 1e4
args$tol_rel_obj <- 1e-3

fm <- do.call(epim, args)
#> Chain 1: ------------------------------------------------------------
#> Chain 1: EXPERIMENTAL ALGORITHM:
#> Chain 1:   This procedure has not been thoroughly tested and may be unstable
#> Chain 1:   or buggy. The interface is subject to change.
#> Chain 1: ------------------------------------------------------------
#> Chain 1: 
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.000311 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.11 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Begin eta adaptation.
#> Chain 1: Iteration:   1 / 250 [  0%]  (Adaptation)
#> Chain 1: Iteration:  50 / 250 [ 20%]  (Adaptation)
#> Chain 1: Iteration: 100 / 250 [ 40%]  (Adaptation)
#> Chain 1: Iteration: 150 / 250 [ 60%]  (Adaptation)
#> Chain 1: Iteration: 200 / 250 [ 80%]  (Adaptation)
#> Chain 1: Iteration: 250 / 250 [100%]  (Adaptation)
#> Chain 1: Success! Found best value [eta = 0.1].
#> Chain 1: 
#> Chain 1: Begin stochastic gradient ascent.
#> Chain 1:   iter             ELBO   delta_ELBO_mean   delta_ELBO_med   notes 
#> Chain 1:    100        -9289.848             1.000            1.000
#> Chain 1:    200        -4831.116             0.961            1.000
#> Chain 1:    300        -3182.639             0.814            0.923
#> Chain 1:    400        -3031.382             0.623            0.923
#> Chain 1:    500        -2417.435             0.549            0.518
#> Chain 1:    600        -2056.742             0.487            0.518
#> Chain 1:    700        -1766.021             0.441            0.254
#> Chain 1:    800        -1760.138             0.386            0.254
#> Chain 1:    900        -1500.056             0.362            0.175
#> Chain 1:   1000        -1499.773             0.326            0.175
#> Chain 1:   1100        -1319.236             0.240            0.173
#> Chain 1:   1200        -1273.658             0.151            0.165
#> Chain 1:   1300        -1205.571             0.105            0.137
#> Chain 1:   1400        -1089.441             0.111            0.137
#> Chain 1:   1500        -1083.336             0.086            0.107
#> Chain 1:   1600        -1035.097             0.073            0.056
#> Chain 1:   1700        -1058.923             0.059            0.047
#> Chain 1:   1800        -1024.462             0.062            0.047
#> Chain 1:   1900        -1003.228             0.047            0.036
#> Chain 1:   2000        -1015.190             0.048            0.036
#> Chain 1:   2100         -973.161             0.038            0.036
#> Chain 1:   2200         -983.108             0.036            0.034
#> Chain 1:   2300         -967.012             0.032            0.022
#> Chain 1:   2400         -960.738             0.022            0.021
#> Chain 1:   2500         -950.279             0.022            0.021
#> Chain 1:   2600         -976.192             0.020            0.021
#> Chain 1:   2700         -953.460             0.020            0.021
#> Chain 1:   2800         -951.972             0.017            0.017
#> Chain 1:   2900         -944.193             0.016            0.012
#> Chain 1:   3000         -934.065             0.016            0.011
#> Chain 1:   3100         -940.595             0.012            0.011
#> Chain 1:   3200         -933.836             0.012            0.011
#> Chain 1:   3300         -939.789             0.011            0.008
#> Chain 1:   3400         -932.943             0.011            0.008
#> Chain 1:   3500         -930.526             0.010            0.007
#> Chain 1:   3600         -929.951             0.008            0.007
#> Chain 1:   3700         -929.015             0.005            0.007
#> Chain 1:   3800         -932.341             0.005            0.007
#> Chain 1:   3900         -933.906             0.005            0.006
#> Chain 1:   4000         -926.055             0.005            0.006
#> Chain 1:   4100         -925.786             0.004            0.004
#> Chain 1:   4200         -928.978             0.004            0.003
#> Chain 1:   4300         -920.898             0.004            0.003
#> Chain 1:   4400         -923.297             0.003            0.003
#> Chain 1:   4500         -918.559             0.004            0.003
#> Chain 1:   4600         -920.989             0.004            0.003
#> Chain 1:   4700         -920.740             0.004            0.003
#> Chain 1:   4800         -915.933             0.004            0.003
#> Chain 1:   4900         -917.094             0.004            0.003
#> Chain 1:   5000         -917.760             0.003            0.003
#> Chain 1:   5100         -916.050             0.003            0.003
#> Chain 1:   5200         -914.898             0.003            0.003
#> Chain 1:   5300         -913.448             0.002            0.002
#> Chain 1:   5400         -915.261             0.002            0.002
#> Chain 1:   5500         -914.105             0.002            0.002
#> Chain 1:   5600         -912.231             0.002            0.002
#> Chain 1:   5700         -916.383             0.002            0.002
#> Chain 1:   5800         -914.184             0.002            0.002
#> Chain 1:   5900         -912.611             0.002            0.002
#> Chain 1:   6000         -915.000             0.002            0.002
#> Chain 1:   6100         -910.008             0.002            0.002
#> Chain 1:   6200         -914.797             0.003            0.002
#> Chain 1:   6300         -909.992             0.003            0.003
#> Chain 1:   6400         -916.667             0.004            0.005
#> Chain 1:   6500         -910.288             0.004            0.005
#> Chain 1:   6600         -909.392             0.004            0.005
#> Chain 1:   6700         -910.499             0.004            0.005
#> Chain 1:   6800         -908.823             0.004            0.005
#> Chain 1:   6900         -910.218             0.004            0.005
#> Chain 1:   7000         -908.797             0.004            0.005
#> Chain 1:   7100         -904.306             0.004            0.005
#> Chain 1:   7200         -912.069             0.004            0.005
#> Chain 1:   7300         -908.387             0.004            0.004
#> Chain 1:   7400         -908.598             0.003            0.002
#> Chain 1:   7500         -908.854             0.003            0.002
#> Chain 1:   7600         -908.946             0.002            0.002
#> Chain 1:   7700         -910.145             0.002            0.002
#> Chain 1:   7800         -904.468             0.003            0.002
#> Chain 1:   7900         -905.938             0.003            0.002
#> Chain 1:   8000         -904.746             0.003            0.002
#> Chain 1:   8100         -902.790             0.003            0.002
#> Chain 1:   8200         -904.814             0.002            0.002
#> Chain 1:   8300         -902.218             0.002            0.002
#> Chain 1:   8400         -902.227             0.002            0.002
#> Chain 1:   8500         -906.528             0.002            0.002
#> Chain 1:   8600         -902.514             0.003            0.002
#> Chain 1:   8700         -904.076             0.003            0.002
#> Chain 1:   8800         -903.459             0.002            0.002
#> Chain 1:   8900         -906.140             0.002            0.002
#> Chain 1:   9000         -901.402             0.003            0.003
#> Chain 1:   9100         -901.195             0.003            0.003
#> Chain 1:   9200         -899.112             0.003            0.003
#> Chain 1:   9300         -900.827             0.002            0.002
#> Chain 1:   9400         -900.696             0.002            0.002
#> Chain 1:   9500         -904.781             0.002            0.002
#> Chain 1:   9600         -904.669             0.002            0.002
#> Chain 1:   9700         -897.376             0.003            0.002
#> Chain 1:   9800         -898.121             0.003            0.002
#> Chain 1:   9900         -899.535             0.003            0.002
#> Chain 1:   10000         -899.509             0.002            0.002
#> Chain 1: Informational Message: The maximum number of iterations is reached! The algorithm may not have converged.
#> Chain 1: This variational approximation is not guaranteed to be meaningful.
#> Chain 1: 
#> Chain 1: Drawing a sample of size 1000 from the approximate posterior... 
#> Chain 1: COMPLETED.
#> Warning: Pareto k diagnostic value is 2.46. Resampling is disabled. Decreasing tol_rel_obj may help if variational algorithm has terminated prematurely. Otherwise consider using sampling instead.

# different ways of using plot_rt
p <- plot_rt(fm) # default, plots all groups and dates
p <- plot_rt(fm, dates=c("2020-03-21", NA)) # plot 21 March 2020 onwards
p <- plot_rt(fm, dates=c(NA, "2020-03-20")) # plot up to  20 March 2020
p <- plot_rt(fm, dates=c("2020-03-20", "2020-04-20"))
p <- plot_rt(fm,
         dates=c("2020-03-20", "2020-04-20"),
        date_breaks="1 day") # ticks every day
p <- plot_rt(fm,
       dates=c("2020-20-03", "2020-20-04"),
       date_format="%Y-%d-%m") # (different date format)

# other plotting functions
p <- plot_obs(fm, type = "deaths")
p <- plot_infections(fm)
p <- plot_infectious(fm) 
# }