Skip to contents

add_rolling_average() adds a rolling average to an <incidence2> object. If multiple groupings or count variables are present then the average will be calculated for each.

Usage

add_rolling_average(
  x,
  n = 3L,
  complete_dates = TRUE,
  align = c("right", "center"),
  colname = "rolling_average",
  ...
)

Arguments

x

[incidence2] object

n

[integer]

How many date groupings to consider in each window?

double vectors will be converted via as.integer(n).

complete_dates

[bool]

Should incidence2::complete_dates() be called on the data prior to adding the rolling average.

Defaults to TRUE.

align

Character, specifying the "alignment" of the rolling window, defaulting to "right". "right" covers preceding rows (the window ends on the current value); "left" covers following rows (the window starts on the current value); "center" is halfway in between (the window is centered on the current value, biased towards "left" when n is even).

colname

[character]

The name of the column to contain the rolling average.

...

Other arguments passed to incidence2::complete_dates()

Value

The input object with an additional column for the rolling average.

Examples


if (requireNamespace("outbreaks", quietly = TRUE)) {
DONTSHOW({withAutoprint({})

  data(ebola_sim_clean, package = "outbreaks")
  dat <- ebola_sim_clean$linelist
  dat <- subset(dat, date_of_onset <= as.Date("2014-10-05"))

  inci <- incidence2::incidence(
      dat,
      date_index = "date_of_onset",
      groups = "gender",
      interval = "isoweek"
  )

  add_rolling_average(inci, n = 3L)
  inci2 <- incidence2::regroup(inci)
  add_rolling_average(inci2, n = 7L)
DONTSHOW({})})
}
#> Error in srcrefs[[1L]]: subscript out of bounds