plot_rt: Plot time-varying reproduction rates

Description Usage Arguments Value See Also Examples

View source: R/plots_epi.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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,
  ...
)

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.

draws

The number of sample paths to plot.

alpha

Sets transparency of sample paths.

Value

A ggplot object which can be further modified.

See Also

plot_obs, plot_infections, plot_infectious

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)

# 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) 

epidemia documentation built on Oct. 25, 2021, 9:09 a.m.