Learn R Programming

i2extras (version 0.2.1)

add_rolling_average: Add a rolling average

Description

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",
  ...
)

Value

The input object with an additional column for the 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, define if rolling window covers preceding rows ("right"), following rows ("left") or centered ("center"). Defaults to "right".

colname

[character]

The name of the column to contain the rolling average.

...

Other arguments passed to incidence2::complete_dates()

Examples

Run this code

if (requireNamespace("outbreaks", quietly = TRUE)) {
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)
})
}

Run the code above in your browser using DataLab