library(timeplyr)
# \dontshow{
.n_dt_threads <- data.table::getDTthreads()
.n_collapse_threads <- collapse::get_collapse()$nthreads
data.table::setDTthreads(threads = 1L)
collapse::set_collapse(nthreads = 1L)
# }
set.seed(42)
# Growth rate is 6% per day
x <- 10 * (1.06)^(0:25)
# Simple growth from one day to the next
rolling_growth(x, n = 1)
# Growth comparing rolling 3 day cumulative
rolling_growth(x, n = 3)
# Growth comparing rolling 3 day cumulative, lagged by 1 day
rolling_growth(x, n = 3, lag = 1)
# Growth comparing windows of equal size
rolling_growth(x, n = 3, partial = FALSE)
# Seven day moving average growth
roll_mean(rolling_growth(x), window = 7, partial = FALSE)
# \dontshow{
data.table::setDTthreads(threads = .n_dt_threads)
collapse::set_collapse(nthreads = .n_collapse_threads)
# }
Run the code above in your browser using DataLab