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 viaas.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
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)
})
}
#> > 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)
#> # incidence: 52 x 5
#> # count vars: date_of_onset
#> # groups: gender
#> date_index gender count_variable count rolling_average
#> * <isowk> <fct> <chr> <int> <dbl>
#> 1 2014-W15 f date_of_onset 1 NA
#> 2 2014-W15 m date_of_onset 0 NA
#> 3 2014-W16 f date_of_onset 0 NA
#> 4 2014-W16 m date_of_onset 1 NA
#> 5 2014-W17 f date_of_onset 4 1.67
#> 6 2014-W17 m date_of_onset 1 0.667
#> 7 2014-W18 f date_of_onset 4 2.67
#> 8 2014-W18 m date_of_onset 0 0.667
#> 9 2014-W19 f date_of_onset 9 5.67
#> 10 2014-W19 m date_of_onset 3 1.33
#> # … with 42 more rows
#> > inci2 <- incidence2::regroup(inci)
#> > add_rolling_average(inci2, n = 7L)
#> # incidence: 26 x 4
#> # count vars: date_of_onset
#> date_index count_variable count rolling_average
#> * <isowk> <chr> <int> <dbl>
#> 1 2014-W15 date_of_onset 1 NA
#> 2 2014-W16 date_of_onset 1 NA
#> 3 2014-W17 date_of_onset 5 NA
#> 4 2014-W18 date_of_onset 4 NA
#> 5 2014-W19 date_of_onset 12 NA
#> 6 2014-W20 date_of_onset 17 NA
#> 7 2014-W21 date_of_onset 15 7.86
#> 8 2014-W22 date_of_onset 19 10.4
#> 9 2014-W23 date_of_onset 23 13.6
#> 10 2014-W24 date_of_onset 21 15.9
#> # … with 16 more rows