growth {timeplyr} | R Documentation |
Rolling basic growth
Description
Calculate basic growth calculations on a rolling basis.
growth()
calculates the percent change between the totals of two numeric vectors
when they're of equal length, otherwise the percent change between the means.
rolling_growth()
does the same calculation on 1 numeric vector, on a rolling basis.
Pairs of windows of length n
, lagged by the value specified by lag
are compared in
a similar manner.
When lag = n
then data.table::frollsum()
is used,
otherwise data.table::frollmean()
is used.
Usage
growth(x, y, na.rm = FALSE, log = FALSE, inf_fill = NULL)
rolling_growth(
x,
n = 1,
lag = n,
na.rm = FALSE,
partial = TRUE,
offset = NULL,
weights = NULL,
inf_fill = NULL,
log = FALSE,
...
)
Arguments
x |
Numeric vector. |
y |
numeric vector |
na.rm |
Should missing values be removed when calculating window? Defaults to |
log |
If |
inf_fill |
Numeric value to replace |
n |
Rolling window size, default is 1. |
lag |
Lag of basic growth comparison, default is the rolling window size. |
partial |
Should rates be calculated outwith the window using partial windows?
If |
offset |
Numeric vector of values to use as offset, e.g. population sizes or exposure times. |
weights |
Importance weights. These can either be length 1 or the same length as x. Currently, no normalisation of weights occurs. |
... |
Further arguments to be passed on to |
Value
growth
returns a numeric(1)
and rolling_growth
returns a numeric(length(x))
.
Examples
library(timeplyr)
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)